~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/cms/RecipientInformationStore.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.cms;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Collection;
 
5
import java.util.HashMap;
 
6
import java.util.Iterator;
 
7
import java.util.List;
 
8
import java.util.Map;
 
9
 
 
10
public class RecipientInformationStore
 
11
{
 
12
    private final List      all; //ArrayList[RecipientInformation]
 
13
    private final Map       table = new HashMap(); // HashMap[RecipientID, ArrayList[RecipientInformation]]
 
14
 
 
15
    public RecipientInformationStore(
 
16
        Collection  recipientInfos)
 
17
    {
 
18
        Iterator it = recipientInfos.iterator();
 
19
 
 
20
        while (it.hasNext())
 
21
        {
 
22
            RecipientInformation recipientInformation = (RecipientInformation)it.next();
 
23
            RecipientId rid = recipientInformation.getRID();
 
24
 
 
25
            List list = (ArrayList)table.get(rid);
 
26
            if (list == null)
 
27
            {
 
28
                list = new ArrayList(1);
 
29
                table.put(rid, list);
 
30
            }
 
31
 
 
32
            list.add(recipientInformation);
 
33
        }
 
34
 
 
35
        this.all = new ArrayList(recipientInfos);
 
36
    }
 
37
 
 
38
    /**
 
39
     * Return the first RecipientInformation object that matches the
 
40
     * passed in selector. Null if there are no matches.
 
41
     *
 
42
     * @param selector to identify a recipient
 
43
     * @return a single RecipientInformation object. Null if none matches.
 
44
     */
 
45
    public RecipientInformation get(
 
46
        RecipientId selector)
 
47
    {
 
48
        List list = (ArrayList)table.get(selector);
 
49
 
 
50
        return list == null ? null : (RecipientInformation) list.get(0);
 
51
    }
 
52
 
 
53
    /**
 
54
     * Return the number of recipients in the collection.
 
55
     *
 
56
     * @return number of recipients identified.
 
57
     */
 
58
    public int size()
 
59
    {
 
60
        return all.size();
 
61
    }
 
62
 
 
63
    /**
 
64
     * Return all recipients in the collection
 
65
     *
 
66
     * @return a collection of recipients.
 
67
     */
 
68
    public Collection getRecipients()
 
69
    {
 
70
        return new ArrayList(all);
 
71
    }
 
72
 
 
73
    /**
 
74
     * Return possible empty collection with recipients matching the passed in RecipientId
 
75
     *
 
76
     * @param selector a recipient id to select against.
 
77
     * @return a collection of RecipientInformation objects.
 
78
     */
 
79
    public Collection getRecipients(
 
80
        RecipientId selector)
 
81
    {
 
82
        List list = (ArrayList)table.get(selector);
 
83
 
 
84
        return list == null ? new ArrayList() : new ArrayList(list);
 
85
    }
 
86
}