1package org.bouncycastle.cms;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.HashMap;
6import java.util.Iterator;
7import java.util.List;
8import java.util.Map;
9
10public class SignerInformationStore
11{
12    private List all = new ArrayList();
13    private Map table = new HashMap();
14
15    public SignerInformationStore(
16        Collection  signerInfos)
17    {
18        Iterator    it = signerInfos.iterator();
19
20        while (it.hasNext())
21        {
22            SignerInformation   signer = (SignerInformation)it.next();
23            SignerId            sid = signer.getSID();
24
25            List list = (ArrayList)table.get(sid);
26            if (list == null)
27            {
28                list = new ArrayList(1);
29                table.put(sid, list);
30            }
31
32            list.add(signer);
33        }
34
35        this.all = new ArrayList(signerInfos);
36    }
37
38    /**
39     * Return the first SignerInformation object that matches the
40     * passed in selector. Null if there are no matches.
41     *
42     * @param selector to identify a signer
43     * @return a single SignerInformation object. Null if none matches.
44     */
45    public SignerInformation get(
46        SignerId        selector)
47    {
48        Collection list = getSigners(selector);
49
50        return list.size() == 0 ? null : (SignerInformation) list.iterator().next();
51    }
52
53    /**
54     * Return the number of signers in the collection.
55     *
56     * @return number of signers identified.
57     */
58    public int size()
59    {
60        return all.size();
61    }
62
63    /**
64     * Return all signers in the collection
65     *
66     * @return a collection of signers.
67     */
68    public Collection getSigners()
69    {
70        return new ArrayList(all);
71    }
72
73    /**
74     * Return possible empty collection with signers matching the passed in SignerId
75     *
76     * @param selector a signer id to select against.
77     * @return a collection of SignerInformation objects.
78     */
79    public Collection getSigners(
80        SignerId selector)
81    {
82        if (selector.getIssuer() != null && selector.getSubjectKeyIdentifier() != null)
83        {
84            List results = new ArrayList();
85
86            Collection match1 = getSigners(new SignerId(selector.getIssuer(), selector.getSerialNumber()));
87
88            if (match1 != null)
89            {
90                results.addAll(match1);
91            }
92
93            Collection match2 = getSigners(new SignerId(selector.getSubjectKeyIdentifier()));
94
95            if (match2 != null)
96            {
97                results.addAll(match2);
98            }
99
100            return results;
101        }
102        else
103        {
104            List list = (ArrayList)table.get(selector);
105
106            return list == null ? new ArrayList() : new ArrayList(list);
107        }
108    }
109}
110