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.Set;
021import java.util.TreeMap;
022
023/**
024 * The database branches class is a convenience class to bundle multiple
025 * {@link DatabaseBranch}s into one object, and map it to its corresponding
026 * owner machine name.
027 * 
028 * <p>The class is mainly used by the {@link DatabaseReconciliator} when comparing
029 * and reconciling changes between the clients' database branches. 
030 *    
031 * @author Philipp C. Heckel (philipp.heckel@gmail.com)
032 */
033public class DatabaseBranches {
034        private TreeMap<String, DatabaseBranch> branches;
035        
036        public DatabaseBranches() {
037                this.branches = new TreeMap<String, DatabaseBranch>();
038        }
039        
040        public Set<String> getClients() {
041                return branches.keySet();
042        }
043        
044        public DatabaseBranch getBranch(String client) {
045                return getBranch(client, false);
046        }
047        
048        public DatabaseBranch getBranch(String client, boolean createIfNotExistant) {
049                DatabaseBranch branch = branches.get(client);
050                
051                if (branch == null && createIfNotExistant) {
052                        branch = new DatabaseBranch();
053                        branches.put(client, branch);
054                }
055                
056                return branch;
057        }
058
059        public void put(String machineName, DatabaseBranch branch) {
060                branches.put(machineName, branch);              
061        }       
062
063        public void remove(String machineName) {
064                branches.remove(machineName);
065        }       
066        
067        @Override
068        public String toString() {
069                return branches.toString();
070        }
071        
072        @Override
073        public DatabaseBranches clone() {
074                DatabaseBranches clonedBranches = new DatabaseBranches();
075                clonedBranches.branches.putAll(branches);
076                
077                return clonedBranches;
078        }
079}