|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| package edu.rice.cs.plt.collect; |
|
36 |
| |
|
37 |
| import java.util.Collection; |
|
38 |
| import java.io.Serializable; |
|
39 |
| import java.util.Iterator; |
|
40 |
| import edu.rice.cs.plt.lambda.Lambda; |
|
41 |
| import edu.rice.cs.plt.iter.SingletonIterator; |
|
42 |
| |
|
43 |
| |
|
44 |
| public class SingletonSet<E> extends AbstractPredicateSet<E> implements Serializable { |
|
45 |
| |
|
46 |
| private final E _elt; |
|
47 |
| |
|
48 |
84
| public SingletonSet(E elt) { _elt = elt; }
|
|
49 |
| |
|
50 |
84
| public boolean contains(Object o) { return (_elt == null) ? (o == null) : _elt.equals(o); }
|
|
51 |
0
| public Iterator<E> iterator() { return new SingletonIterator<E>(_elt); }
|
|
52 |
0
| public boolean isInfinite() { return false; }
|
|
53 |
0
| public boolean hasFixedSize() { return true; }
|
|
54 |
0
| public boolean isStatic() { return true; }
|
|
55 |
| |
|
56 |
84
| @Override public int size() { return 1; }
|
|
57 |
0
| @Override public int size(int bound) { return (bound < 1) ? bound : 1; }
|
|
58 |
0
| @Override public boolean isEmpty() { return false; }
|
|
59 |
0
| @Override public Object[] toArray() { return new Object[]{ _elt }; }
|
|
60 |
| |
|
61 |
0
| @Override public boolean add(E o) { throw new UnsupportedOperationException(); }
|
|
62 |
0
| @Override public boolean addAll(Collection<? extends E> c) { throw new UnsupportedOperationException(); }
|
|
63 |
0
| @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); }
|
|
64 |
0
| @Override public boolean retainAll(Collection<?> c) { throw new UnsupportedOperationException(); }
|
|
65 |
0
| @Override public boolean removeAll(Collection<?> c) { throw new UnsupportedOperationException(); }
|
|
66 |
0
| @Override public void clear() { throw new UnsupportedOperationException(); }
|
|
67 |
| |
|
68 |
| |
|
69 |
0
| public static <E> SingletonSet<E> make(E element) {
|
|
70 |
0
| return new SingletonSet<E>(element);
|
|
71 |
| } |
|
72 |
| |
|
73 |
0
| @SuppressWarnings("unchecked") public static <T> Lambda<T, SingletonSet<T>> factory() {
|
|
74 |
0
| return (Factory<T>) Factory.INSTANCE;
|
|
75 |
| } |
|
76 |
| |
|
77 |
| private static final class Factory<T> implements Lambda<T, SingletonSet<T>>, Serializable { |
|
78 |
| public static final Factory<Object> INSTANCE = new Factory<Object>(); |
|
79 |
0
| private Factory() {}
|
|
80 |
0
| public SingletonSet<T> value(T arg) { return new SingletonSet<T>(arg); }
|
|
81 |
| } |
|
82 |
| |
|
83 |
| } |