1 package org.bouncycastle.tsp.ers; 2 3 import java.util.ArrayList; 4 import java.util.Arrays; 5 import java.util.Collections; 6 import java.util.List; 7 8 import org.bouncycastle.operator.DigestCalculator; 9 10 /** 11 * Representation of data groups with more than 1 members according to the description provided in RFC4998. 12 * <p> 13 * Such data groups represent a set of one or more data objects (e.g. electronic documents) for 14 * which an Evidence Record should be generated. Data groups will be encapsulated in a single 15 * PartialHashtree so that the presence of the group can be checked for, as well as the 16 * individual items that make it up. 17 */ 18 public class ERSDataGroup 19 extends ERSCachingData 20 { 21 protected List<ERSData> dataObjects; 22 23 /** 24 * Base constructor for an "array" of data objects. 25 * 26 * @param dataObjects an array of data objects. 27 */ ERSDataGroup(ERSData... dataObjects)28 public ERSDataGroup(ERSData... dataObjects) 29 { 30 this.dataObjects = new ArrayList<ERSData>(dataObjects.length); 31 this.dataObjects.addAll(Arrays.asList(dataObjects)); 32 } 33 34 /** 35 * Base constructor using a list of data objects. 36 * 37 * @param dataObjects a list of data objects. 38 */ ERSDataGroup(List<ERSData> dataObjects)39 public ERSDataGroup(List<ERSData> dataObjects) 40 { 41 this.dataObjects = new ArrayList<ERSData>(dataObjects.size()); 42 this.dataObjects.addAll(dataObjects); 43 } 44 45 /** 46 * Constructor for a group with a single object. 47 * 48 * @param dataObject the data object to go in the group. 49 */ ERSDataGroup(ERSData dataObject)50 public ERSDataGroup(ERSData dataObject) 51 { 52 this.dataObjects = Collections.singletonList(dataObject); 53 } 54 55 /** 56 * Generates hashes for all the data objects included in the data group. 57 * 58 * @param digestCalculator the {@link DigestCalculator} to use for computing the hashes 59 * @return the set of hashes, in ascending order 60 */ getHashes( final DigestCalculator digestCalculator)61 public List<byte[]> getHashes( 62 final DigestCalculator digestCalculator) 63 { 64 return ERSUtil.buildHashList(digestCalculator, dataObjects); 65 } 66 67 /** 68 * Generates a hash for the whole DataGroup. 69 * 70 * @param digestCalculator the {@link DigestCalculator} to use for computing the hash 71 * @return a hash that is representative of the whole DataGroup 72 */ calculateHash(DigestCalculator digestCalculator)73 protected byte[] calculateHash(DigestCalculator digestCalculator) 74 { 75 List<byte[]> hashes = getHashes(digestCalculator); 76 77 if (hashes.size() > 1) 78 { 79 return ERSUtil.calculateDigest(digestCalculator, hashes.iterator()); 80 } 81 else 82 { 83 return (byte[])hashes.get(0); 84 } 85 } 86 87 /** 88 * Return the number of data objects present in the group. 89 * 90 * @return membership count of the group. 91 */ size()92 public int size() 93 { 94 return dataObjects.size(); 95 } 96 } 97