1package org.bouncycastle.util;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Iterator;
6import java.util.List;
7
8/**
9 * A simple collection backed store.
10 */
11public class CollectionStore
12    implements Store
13{
14    private Collection _local;
15
16    /**
17     * Basic constructor.
18     *
19     * @param collection - initial contents for the store, this is copied.
20     */
21    public CollectionStore(
22        Collection collection)
23    {
24        _local = new ArrayList(collection);
25    }
26
27    /**
28     * Return the matches in the collection for the passed in selector.
29     *
30     * @param selector the selector to match against.
31     * @return a possibly empty collection of matching objects.
32     */
33    public Collection getMatches(Selector selector)
34    {
35        if (selector == null)
36        {
37            return new ArrayList(_local);
38        }
39        else
40        {
41            List col = new ArrayList();
42            Iterator iter = _local.iterator();
43
44            while (iter.hasNext())
45            {
46                Object obj = iter.next();
47
48                if (selector.match(obj))
49                {
50                    col.add(obj);
51                }
52            }
53
54            return col;
55        }
56    }
57}
58