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.operations.down;
019
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.List;
023
024import org.syncany.database.DatabaseVersion;
025import org.syncany.database.DatabaseVersionHeader;
026
027/**
028 * A branch represents a list of {@link DatabaseVersionHeader}s, thereby identifying a
029 * the history of a client's database. It can be compared to a sorted list of
030 * {@link DatabaseVersion} pointers.
031 *
032 * <p>Branches are used mainly in the {@link DatabaseReconciliator} to compare database
033 * versions and reconcile conflicts.
034 *
035 * @author Philipp C. Heckel (philipp.heckel@gmail.com)
036 */
037public class DatabaseBranch {
038        private ArrayList<DatabaseVersionHeader> branch;
039
040        public DatabaseBranch() {
041                this.branch = new ArrayList<DatabaseVersionHeader>();
042        }
043
044        public void add(DatabaseVersionHeader header) {
045                branch.add(header);
046        }
047
048        public void addAll(List<DatabaseVersionHeader> headers) {
049                branch.addAll(headers);
050        }
051
052        public int size() {
053                return branch.size();
054        }
055
056        public DatabaseVersionHeader get(int index) {
057                if (index >= 0 && index < branch.size()) {
058                        return branch.get(index);
059                }
060                return null;
061        }
062
063        public List<DatabaseVersionHeader> getAll() {
064                return Collections.unmodifiableList(branch);
065        }
066
067        public DatabaseVersionHeader getLast() {
068                if (branch.size() == 0) {
069                        return null;
070                }
071                return branch.get(branch.size() - 1);
072        }
073
074        @Override
075        public String toString() {
076                return branch.toString();
077        }
078
079        @Override
080        public DatabaseBranch clone() {
081                DatabaseBranch clonedBranch = new DatabaseBranch();
082                clonedBranch.addAll(getAll());
083
084                return clonedBranch;
085        }
086
087        @Override
088        public int hashCode() {
089                final int prime = 31;
090                int result = 1;
091                result = prime * result + ((branch == null) ? 0 : branch.hashCode());
092                return result;
093        }
094
095        @Override
096        public boolean equals(Object obj) {
097                if (this == obj) {
098                        return true;
099                }
100                if (obj == null) {
101                        return false;
102                }
103                if (!(obj instanceof DatabaseBranch)) {
104                        return false;
105                }
106                DatabaseBranch other = (DatabaseBranch) obj;
107                if (branch == null) {
108                        if (other.branch != null) {
109                                return false;
110                        }
111                }
112                else if (!branch.equals(other.branch)) {
113                        return false;
114                }
115                return true;
116        }
117}