001/* 002 * Syncany, www.syncany.org 003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package org.syncany.plugins.transfer.files; 019 020import java.util.regex.Matcher; 021import java.util.regex.Pattern; 022 023import org.syncany.plugins.transfer.StorageException; 024 025/** 026 * The database file represents a delta database. 027 * 028 * <p><b>Name pattern:</b> The name pattern of a database file is 029 * <b>db-<clientname>-<clientversion></b>. Initializing an 030 * instance with a non-matching name will throw an exception. 031 * 032 * <p><b>Note:</b> The class implements a {@link Comparable} interface and 033 * can be sorted by name and client version. 034 * 035 * @author Philipp C. Heckel (philipp.heckel@gmail.com) 036 */ 037public class DatabaseRemoteFile extends RemoteFile implements Comparable<DatabaseRemoteFile> { 038 private static final Pattern NAME_PATTERN = Pattern.compile("database-([^-]+)-(\\d+)"); 039 private static final String NAME_FORMAT = "database-%s-%010d"; 040 041 private String clientName; 042 private long clientVersion; 043 044 /** 045 * Initializes a new database file, given a name. This constructor might 046 * be called by the {@link RemoteFile#createRemoteFile(String, Class) createRemoteFile()} 047 * method of the {@link RemoteFile}. 048 * 049 * <p>If the pattern matches, the client name and the client version are set, and can be 050 * queried by {@link #getClientName()} and {@link #getClientVersion()}. 051 * 052 * @param name Database file name; <b>must</b> always match the {@link #NAME_PATTERN} 053 * @throws StorageException If the name is not match the name pattern 054 */ 055 public DatabaseRemoteFile(String name) throws StorageException { 056 super(name); 057 } 058 059 /** 060 * Initializes a new database file, given a client name and version 061 * 062 * @param clientName The name of the client/machine for this delta database file 063 * @param version The client version for this delta database file 064 * @throws StorageException Never throws an exception 065 */ 066 public DatabaseRemoteFile(String clientName, long version) throws StorageException { 067 super(String.format(NAME_FORMAT, clientName, version)); 068 } 069 070 /** 071 * Returns the client name 072 */ 073 public String getClientName() { 074 return clientName; 075 } 076 077 /** 078 * Returns the client version 079 */ 080 public long getClientVersion() { 081 return clientVersion; 082 } 083 084 @Override 085 protected String validateName(String name) throws StorageException { 086 Matcher matcher = NAME_PATTERN.matcher(name); 087 088 if (!matcher.matches()) { 089 throw new StorageException(name + ": remote database filename pattern does not match: " + NAME_PATTERN.pattern() + " expected."); 090 } 091 092 clientName = matcher.group(1); 093 clientVersion = Long.parseLong(matcher.group(2)); 094 095 return name; 096 } 097 098 @Override 099 public int compareTo(DatabaseRemoteFile r2) { 100 int clientNameCompare = getClientName().compareTo(r2.getClientName()); 101 102 if (clientNameCompare != 0) { 103 return clientNameCompare; 104 } 105 else { 106 return (int) (getClientVersion() - r2.getClientVersion()); 107 } 108 } 109 110 @Override 111 public int hashCode() { 112 final int prime = 31; 113 int result = super.hashCode(); 114 result = prime * result + ((clientName == null) ? 0 : clientName.hashCode()); 115 result = prime * result + (int) (clientVersion ^ (clientVersion >>> 32)); 116 return result; 117 } 118 119 @Override 120 public boolean equals(Object obj) { 121 if (this == obj) { 122 return true; 123 } 124 if (!super.equals(obj)) { 125 return false; 126 } 127 if (!(obj instanceof DatabaseRemoteFile)) { 128 return false; 129 } 130 DatabaseRemoteFile other = (DatabaseRemoteFile) obj; 131 if (clientName == null) { 132 if (other.clientName != null) { 133 return false; 134 } 135 } 136 else if (!clientName.equals(other.clientName)) { 137 return false; 138 } 139 if (clientVersion != other.clientVersion) { 140 return false; 141 } 142 return true; 143 } 144 145}