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<T>
12    implements Store<T>, Iterable<T>
13{
14    private Collection<T> _local;
15
16    /**
17     * Basic constructor.
18     *
19     * @param collection - initial contents for the store, this is copied.
20     */
21    public CollectionStore(
22        Collection<T> collection)
23    {
24        _local = new ArrayList<T>(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<T> getMatches(Selector<T> selector)
34    {
35        if (selector == null)
36        {
37            return new ArrayList<T>(_local);
38        }
39        else
40        {
41            List<T> col = new ArrayList<T>();
42            Iterator<T> iter = _local.iterator();
43
44            while (iter.hasNext())
45            {
46                T obj = iter.next();
47
48                if (selector.match(obj))
49                {
50                    col.add(obj);
51                }
52            }
53
54            return col;
55        }
56    }
57
58    public Iterator<T> iterator()
59    {
60        return getMatches(null).iterator();
61    }
62}
63