001: package org.bouncycastle.cms;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Collections;
006: import java.util.HashMap;
007: import java.util.Map;
008: import java.util.Iterator;
009: import java.util.List;
010:
011: public class SignerInformationStore {
012: private Map table = new HashMap();
013:
014: public SignerInformationStore(Collection signerInfos) {
015: Iterator it = signerInfos.iterator();
016:
017: while (it.hasNext()) {
018: SignerInformation signer = (SignerInformation) it.next();
019: SignerId sid = signer.getSID();
020:
021: if (table.get(sid) == null) {
022: table.put(sid, signer);
023: } else {
024: Object o = table.get(sid);
025:
026: if (o instanceof List) {
027: ((List) o).add(signer);
028: } else {
029: List l = new ArrayList();
030:
031: l.add(o);
032: l.add(signer);
033:
034: table.put(sid, l);
035: }
036: }
037: }
038: }
039:
040: /**
041: * Return the first SignerInformation object that matches the
042: * passed in selector. Null if there are no matches.
043: *
044: * @param selector to identify a signer
045: * @return a single SignerInformation object. Null if none matches.
046: */
047: public SignerInformation get(SignerId selector) {
048: Object o = table.get(selector);
049:
050: if (o instanceof List) {
051: return (SignerInformation) ((List) o).get(0);
052: } else {
053: return (SignerInformation) o;
054: }
055: }
056:
057: /**
058: * Return the number of signers in the collection.
059: *
060: * @return number of signers identified.
061: */
062: public int size() {
063: Iterator it = table.values().iterator();
064: int count = 0;
065:
066: while (it.hasNext()) {
067: Object o = it.next();
068:
069: if (o instanceof List) {
070: count += ((List) o).size();
071: } else {
072: count++;
073: }
074: }
075:
076: return count;
077: }
078:
079: /**
080: * Return all signers in the collection
081: *
082: * @return a collection of signers.
083: */
084: public Collection getSigners() {
085: List list = new ArrayList(table.size());
086: Iterator it = table.values().iterator();
087:
088: while (it.hasNext()) {
089: Object o = it.next();
090:
091: if (o instanceof List) {
092: list.addAll((List) o);
093: } else {
094: list.add(o);
095: }
096: }
097:
098: return list;
099: }
100:
101: /**
102: * Return possible empty collection with signers matching the passed in SignerId
103: *
104: * @param selector a signer id to select against.
105: * @return a collection of SignerInformation objects.
106: */
107: public Collection getSigners(SignerId selector) {
108: Object o = table.get(selector);
109:
110: if (o instanceof List) {
111: return new ArrayList((List) o);
112: } else if (o != null) {
113: return Collections.singletonList(o);
114: }
115:
116: return new ArrayList();
117: }
118: }
|