1 /* 2 * #%L 3 * Bitrepository Command Line 4 * %% 5 * Copyright (C) 2010 - 2013 The State and University Library, The Royal Library and The State Archives, Denmark 6 * %% 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser General Public License as 9 * published by the Free Software Foundation, either version 2.1 of the 10 * License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Lesser Public License for more details. 16 * 17 * You should have received a copy of the GNU General Lesser Public 18 * License along with this program. If not, see 19 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 20 * #L% 21 */ 22 package org.bitrepository.commandline.resultmodel; 23 24 import java.util.ArrayList; 25 import java.util.HashMap; 26 import java.util.List; 27 import java.util.Map; 28 29 public class ChecksumResult { 30 31 /** FileID */ 32 private final String id; 33 /** Mapping from pillar/contributorid to returned checksum */ 34 private final Map<String, String> pillarChecksumMap; 35 /** Indication if there's checksum disagreement */ 36 private boolean dirty; 37 38 public ChecksumResult(String id, String contributor, String checksum) { 39 pillarChecksumMap = new HashMap<String, String>(); 40 this.id = id; 41 dirty = false; 42 pillarChecksumMap.put(contributor, checksum); 43 } 44 45 /** 46 * Add a contributor with it's checksum to the result 47 * @param contributor, the ID of the contributor 48 * @param checksum, the checksum that the contributor delivered 49 */ 50 public void addContributor(String contributor, String checksum) { 51 if(!dirty && !pillarChecksumMap.containsValue(checksum)) { 52 dirty = true; 53 } 54 pillarChecksumMap.put(contributor, checksum); 55 } 56 57 /** 58 * Is the result 'dirty', i.e. is there checksum disagreement among the answered contributors. 59 * @return false if all contributors have agreed on the checksum. 60 */ 61 public boolean isDirty() { 62 return dirty; 63 } 64 65 /** 66 * Get the list of contributors. 67 * @return Set<String>, the set of contributors which have delivered a checksum 68 */ 69 public List<String> getContributors() { 70 return new ArrayList<String>(pillarChecksumMap.keySet()); 71 } 72 73 /** 74 * Get the checksum from a given contributor 75 */ 76 public String getChecksum(String contributor) { 77 return pillarChecksumMap.get(contributor); 78 } 79 80 /** 81 * Get the fileID of the file for which the checksum is for. 82 * @return String, the fileID 83 */ 84 public String getID() { 85 return id; 86 } 87 88 /** 89 * Determine if we have enough answers to consider the result complete 90 * @param expectedNumberOfContributors, the expected number of contributors. 91 * @return true, if there's registered expectedNumberOfContributors of contributors. 92 */ 93 public boolean isComplete(int expectedNumberOfContributors) { 94 return (pillarChecksumMap.size() == expectedNumberOfContributors); 95 } 96 }