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.database;
019
020import java.util.Date;
021
022/**
023 * The database version header identifies a {@link DatabaseVersion} using
024 * a {@link VectorClock}, a local timestamp and the client's machine name.
025 *
026 * <p>The database version header plays a significant role in the
027 * reconciliation process, in particular when trying to find a winning
028 * branch.
029 *
030 * @author Philipp C. Heckel (philipp.heckel@gmail.com)
031 */
032public class DatabaseVersionHeader {
033        private Date date;
034        private VectorClock vectorClock;
035        private String client;
036
037        public DatabaseVersionHeader() {
038                this.date = new Date();
039                this.vectorClock = new VectorClock();
040                this.client = "UnknownMachine";
041        }
042
043        public Date getDate() {
044                return date;
045        }
046
047        public void setDate(Date timestamp) {
048                date = timestamp;
049        }
050
051        public VectorClock getVectorClock() {
052                return vectorClock;
053        }
054
055        public void setVectorClock(VectorClock vectorClock) {
056                this.vectorClock = vectorClock;
057        }
058
059        public String getClient() {
060                return client;
061        }
062
063        public void setClient(String client) {
064                this.client = client;
065        }
066
067        @Override
068        public int hashCode() {
069                final int prime = 31;
070                int result = 1;
071                result = prime * result + ((date == null) ? 0 : date.hashCode());
072                result = prime * result + ((client == null) ? 0 : client.hashCode());
073                result = prime * result + ((vectorClock == null) ? 0 : vectorClock.hashCode());
074                return result;
075        }
076
077        @Override
078        public boolean equals(Object obj) {
079                if (this == obj) {
080                        return true;
081                }
082                if (obj == null) {
083                        return false;
084                }
085                if (!(obj instanceof DatabaseVersionHeader)) {
086                        return false;
087                }
088                DatabaseVersionHeader other = (DatabaseVersionHeader) obj;
089                if (date == null) {
090                        if (other.date != null) {
091                                return false;
092                        }
093                }
094                else if (!date.equals(other.date)) {
095                        return false;
096                }
097                if (client == null) {
098                        if (other.client != null) {
099                                return false;
100                        }
101                }
102                else if (!client.equals(other.client)) {
103                        return false;
104                }
105                if (vectorClock == null) {
106                        if (other.vectorClock != null) {
107                                return false;
108                        }
109                }
110                else if (!vectorClock.equals(other.vectorClock)) {
111                        return false;
112                }
113                return true;
114        }
115
116        @Override
117        public String toString() {
118                StringBuffer sb = new StringBuffer();
119
120                sb.append(client);
121                sb.append('/');
122                sb.append(vectorClock.toString());
123                sb.append("/T=");
124                sb.append(date.getTime());
125
126                return sb.toString();
127        }
128}