|
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.Iterator; |
|
38 |
| import java.io.Serializable; |
|
39 |
| import edu.rice.cs.plt.tuple.Pair; |
|
40 |
| import edu.rice.cs.plt.tuple.Option; |
|
41 |
| import edu.rice.cs.plt.iter.MappedIterator; |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| public abstract class AbstractRelation<T1, T2> extends AbstractPredicateSet<Pair<T1, T2>> |
|
53 |
| implements Relation<T1, T2> { |
|
54 |
| |
|
55 |
| public abstract boolean isInfinite(); |
|
56 |
| public abstract boolean hasFixedSize(); |
|
57 |
| public abstract boolean isStatic(); |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| public abstract boolean contains(T1 first, T2 second); |
|
65 |
| public abstract boolean contains(Object obj); |
|
66 |
| public abstract Iterator<Pair<T1, T2>> iterator(); |
|
67 |
| public abstract PredicateSet<T1> firstSet(); |
|
68 |
| public abstract PredicateSet<T2> matchFirst(T1 first); |
|
69 |
| public abstract PredicateSet<T2> secondSet(); |
|
70 |
| public abstract PredicateSet<T1> matchSecond(T2 second); |
|
71 |
| |
|
72 |
0
| public boolean add(T1 first, T2 second) { throw new UnsupportedOperationException(); }
|
|
73 |
0
| public boolean remove(T1 first, T2 second) { throw new UnsupportedOperationException(); }
|
|
74 |
| |
|
75 |
| |
|
76 |
0
| public boolean add(Pair<T1, T2> p) { return add(p.first(), p.second()); }
|
|
77 |
| |
|
78 |
| |
|
79 |
0
| public boolean remove(Object o) {
|
|
80 |
0
| Option<Pair<T1, T2>> cast = CollectUtil.castIfContains(this, o);
|
|
81 |
0
| if (cast.isSome()) { return remove(cast.unwrap().first(), cast.unwrap().second()); }
|
|
82 |
0
| else { return false; }
|
|
83 |
| } |
|
84 |
| |
|
85 |
| |
|
86 |
3
| public Relation<T2, T1> inverse() { return new InverseRelation(); }
|
|
87 |
| |
|
88 |
| |
|
89 |
36
| public boolean containsFirst(T1 first) { return firstSet().contains(first); }
|
|
90 |
| |
|
91 |
| |
|
92 |
30
| public PredicateSet<T2> excludeFirsts() { return secondSet(); }
|
|
93 |
| |
|
94 |
| |
|
95 |
66
| public boolean containsSecond(T2 second) { return secondSet().contains(second); }
|
|
96 |
| |
|
97 |
| |
|
98 |
30
| public PredicateSet<T1> excludeSeconds() { return firstSet(); }
|
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| protected class InverseRelation extends AbstractPredicateSet<Pair<T2, T1>> |
|
111 |
| implements Relation<T2, T1>, Serializable { |
|
112 |
| |
|
113 |
24
| public int size() { return AbstractRelation.this.size(); }
|
|
114 |
0
| public int size(int bound) { return AbstractRelation.this.size(bound); }
|
|
115 |
6
| @Override public boolean isEmpty() { return AbstractRelation.this.isEmpty(); }
|
|
116 |
0
| public boolean isInfinite() { return AbstractRelation.this.isInfinite(); }
|
|
117 |
0
| public boolean hasFixedSize() { return AbstractRelation.this.hasFixedSize(); }
|
|
118 |
0
| public boolean isStatic() { return AbstractRelation.this.isStatic(); }
|
|
119 |
| |
|
120 |
0
| @Override public boolean contains(Object o) {
|
|
121 |
0
| return (o instanceof Pair<?, ?>) &&
|
|
122 |
| AbstractRelation.this.contains(((Pair<?, ?>) o).inverse()); |
|
123 |
| } |
|
124 |
| |
|
125 |
9
| public Iterator<Pair<T2, T1>> iterator() {
|
|
126 |
9
| return new MappedIterator<Pair<T1, T2>, Pair<T2, T1>>(AbstractRelation.this.iterator(),
|
|
127 |
| Pair.<T1, T2>inverter()); |
|
128 |
| } |
|
129 |
| |
|
130 |
0
| @Override public boolean add(Pair<T2, T1> pair) {
|
|
131 |
0
| return AbstractRelation.this.add(pair.inverse());
|
|
132 |
| } |
|
133 |
| |
|
134 |
0
| @Override public boolean remove(Object o) {
|
|
135 |
0
| return (o instanceof Pair<?, ?>) &&
|
|
136 |
| AbstractRelation.this.remove(((Pair<?, ?>) o).inverse()); |
|
137 |
| } |
|
138 |
| |
|
139 |
0
| @Override public void clear() { AbstractRelation.this.clear(); }
|
|
140 |
| |
|
141 |
0
| public boolean contains(T2 f, T1 s) { return AbstractRelation.this.contains(s, f); }
|
|
142 |
0
| public boolean add(T2 f, T1 s) { return AbstractRelation.this.add(s, f); }
|
|
143 |
0
| public boolean remove(T2 f, T1 s) { return AbstractRelation.this.remove(s, f); }
|
|
144 |
0
| public Relation<T1, T2> inverse() { return AbstractRelation.this; }
|
|
145 |
| |
|
146 |
15
| public PredicateSet<T2> firstSet() { return AbstractRelation.this.secondSet(); }
|
|
147 |
33
| public boolean containsFirst(T2 f) { return AbstractRelation.this.containsSecond(f); }
|
|
148 |
33
| public PredicateSet<T1> matchFirst(T2 f) { return AbstractRelation.this.matchSecond(f); }
|
|
149 |
15
| public PredicateSet<T1> excludeFirsts() { return AbstractRelation.this.excludeSeconds(); }
|
|
150 |
| |
|
151 |
15
| public PredicateSet<T1> secondSet() { return AbstractRelation.this.firstSet(); }
|
|
152 |
18
| public boolean containsSecond(T1 s) { return AbstractRelation.this.containsFirst(s); }
|
|
153 |
18
| public PredicateSet<T2> matchSecond(T1 s) { return AbstractRelation.this.matchFirst(s); }
|
|
154 |
15
| public PredicateSet<T2> excludeSeconds() { return AbstractRelation.this.excludeFirsts(); }
|
|
155 |
| } |
|
156 |
| |
|
157 |
| } |