|
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.io.Serializable; |
|
38 |
| import java.util.Iterator; |
|
39 |
| import edu.rice.cs.plt.tuple.Pair; |
|
40 |
| import edu.rice.cs.plt.iter.SingletonIterator; |
|
41 |
| import edu.rice.cs.plt.object.ObjectUtil; |
|
42 |
| |
|
43 |
| |
|
44 |
| public class SingletonRelation<T1, T2> extends AbstractOneToOneRelation<T1, T2> implements Serializable { |
|
45 |
| |
|
46 |
| private final T1 _first; |
|
47 |
| private final T2 _second; |
|
48 |
| |
|
49 |
0
| public SingletonRelation(T1 first, T2 second) { _first = first; _second = second; }
|
|
50 |
| |
|
51 |
0
| public SingletonRelation(Pair<? extends T1, ? extends T2> pair) {
|
|
52 |
0
| _first = pair.first();
|
|
53 |
0
| _second = pair.second();
|
|
54 |
| } |
|
55 |
| |
|
56 |
0
| @Override public boolean isEmpty() { return false; }
|
|
57 |
0
| @Override public int size() { return 1; }
|
|
58 |
0
| @Override public int size(int bound) { return (bound < 1) ? bound : 1; }
|
|
59 |
0
| public boolean isInfinite() { return false; }
|
|
60 |
0
| public boolean hasFixedSize() { return true; }
|
|
61 |
0
| public boolean isStatic() { return true; }
|
|
62 |
| |
|
63 |
0
| public boolean contains(T1 candidate1, T2 candidate2) {
|
|
64 |
0
| return ObjectUtil.equal(candidate1, _first) && ObjectUtil.equal(candidate2, _second);
|
|
65 |
| } |
|
66 |
| |
|
67 |
0
| public boolean contains(Object obj) {
|
|
68 |
0
| if (obj instanceof Pair<?, ?>) {
|
|
69 |
0
| Pair<?, ?> p = (Pair<?, ?>) obj;
|
|
70 |
0
| return ObjectUtil.equal(p.first(), _first) && ObjectUtil.equal(p.second(), _second);
|
|
71 |
| } |
|
72 |
0
| else { return false; }
|
|
73 |
| } |
|
74 |
| |
|
75 |
0
| @Override public Iterator<Pair<T1, T2>> iterator() {
|
|
76 |
0
| return new SingletonIterator<Pair<T1, T2>>(Pair.make(_first, _second));
|
|
77 |
| } |
|
78 |
| |
|
79 |
0
| public LambdaMap<T1, T2> functionMap() { return new SingletonMap<T1, T2>(_first, _second); }
|
|
80 |
0
| public LambdaMap<T2, T1> injectionMap() { return new SingletonMap<T2, T1>(_second, _first); }
|
|
81 |
| |
|
82 |
0
| @Override public PredicateSet<T1> firstSet() { return new SingletonSet<T1>(_first); }
|
|
83 |
0
| @Override public PredicateSet<T2> matchFirst(T1 match) {
|
|
84 |
0
| if ((_first == null) ? (match == null) : _first.equals(match)) {
|
|
85 |
0
| return new SingletonSet<T2>(_second);
|
|
86 |
| } |
|
87 |
0
| else { return EmptySet.make(); }
|
|
88 |
| } |
|
89 |
| |
|
90 |
0
| @Override public PredicateSet<T2> secondSet() { return new SingletonSet<T2>(_second); }
|
|
91 |
0
| @Override public PredicateSet<T1> matchSecond(T2 match) {
|
|
92 |
0
| if ((_second == null) ? (match == null) : _second.equals(match)) {
|
|
93 |
0
| return new SingletonSet<T1>(_first);
|
|
94 |
| } |
|
95 |
0
| else { return EmptySet.make(); }
|
|
96 |
| } |
|
97 |
| |
|
98 |
0
| @Override public OneToOneRelation<T2, T1> inverse() {
|
|
99 |
0
| return new SingletonRelation<T2, T1>(_second, _first);
|
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
0
| public static <T1, T2> SingletonRelation<T1, T2> make(T1 first, T2 second) {
|
|
104 |
0
| return new SingletonRelation<T1, T2>(first, second);
|
|
105 |
| } |
|
106 |
| |
|
107 |
| |
|
108 |
0
| public static <T1, T2> SingletonRelation<T1, T2> make(Pair<? extends T1, ? extends T2> pair) {
|
|
109 |
0
| return new SingletonRelation<T1, T2>(pair);
|
|
110 |
| } |
|
111 |
| |
|
112 |
| } |