|
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.Map; |
|
38 |
| import java.io.Serializable; |
|
39 |
| import edu.rice.cs.plt.lambda.Thunk; |
|
40 |
| |
|
41 |
| |
|
42 |
| public class IndexedOneToOneRelation<T1, T2> extends AbstractOneToOneRelation<T1, T2> implements Serializable { |
|
43 |
| |
|
44 |
| private Map<T1, T2> _firstMap; |
|
45 |
| private LambdaMap<T1, T2> _functionMap; |
|
46 |
| private Map<T2, T1> _secondMap; |
|
47 |
| private LambdaMap<T2, T1> _injectionMap; |
|
48 |
| |
|
49 |
| |
|
50 |
0
| public IndexedOneToOneRelation() {
|
|
51 |
0
| this(CollectUtil.<T1, T2>hashMapFactory(), CollectUtil.<T2, T1>hashMapFactory());
|
|
52 |
| } |
|
53 |
| |
|
54 |
| |
|
55 |
0
| public IndexedOneToOneRelation(Thunk<Map<T1, T2>> firstIndexFactory,
|
|
56 |
| Thunk<Map<T2, T1>> secondIndexFactory) { |
|
57 |
0
| _firstMap = firstIndexFactory.value();
|
|
58 |
| |
|
59 |
0
| _functionMap = new ImmutableMap<T1, T2>(_firstMap);
|
|
60 |
0
| _secondMap = secondIndexFactory.value();
|
|
61 |
| |
|
62 |
0
| _injectionMap = new ImmutableMap<T2, T1>(_secondMap);
|
|
63 |
| } |
|
64 |
| |
|
65 |
0
| public boolean isStatic() { return false; }
|
|
66 |
0
| public LambdaMap<T1, T2> functionMap() { return _functionMap; }
|
|
67 |
0
| public LambdaMap<T2, T1> injectionMap() { return _injectionMap; }
|
|
68 |
| |
|
69 |
0
| @Override public boolean add(T1 first, T2 second) {
|
|
70 |
0
| boolean result = validateAdd(first, second);
|
|
71 |
0
| if (result) {
|
|
72 |
0
| _firstMap.put(first, second);
|
|
73 |
0
| _secondMap.put(second, first);
|
|
74 |
| } |
|
75 |
0
| return result;
|
|
76 |
| } |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
0
| private boolean validateAdd(T1 first, T2 second) {
|
|
83 |
0
| if (_firstMap.containsKey(first)) {
|
|
84 |
0
| T2 current = _firstMap.get(first);
|
|
85 |
0
| if ((current == null) ? (second == null) : current.equals(second)) {
|
|
86 |
0
| return false;
|
|
87 |
| } |
|
88 |
| else { |
|
89 |
0
| throw new IllegalArgumentException("Relation already contains an entry for " + first);
|
|
90 |
| } |
|
91 |
| } |
|
92 |
0
| else if (_secondMap.containsKey(second)) {
|
|
93 |
0
| throw new IllegalArgumentException("Relation already contains an entry for " + second);
|
|
94 |
| } |
|
95 |
0
| else { return true; }
|
|
96 |
| } |
|
97 |
| |
|
98 |
0
| @Override public boolean remove(T1 first, T2 second) {
|
|
99 |
0
| boolean result = contains(first, second);
|
|
100 |
0
| if (result) {
|
|
101 |
0
| _firstMap.remove(first);
|
|
102 |
0
| _secondMap.remove(second);
|
|
103 |
| } |
|
104 |
0
| return result;
|
|
105 |
| } |
|
106 |
| |
|
107 |
0
| @Override public void clear() {
|
|
108 |
0
| _firstMap.clear();
|
|
109 |
0
| _secondMap.clear();
|
|
110 |
| } |
|
111 |
| |
|
112 |
| |
|
113 |
0
| public static <T1, T2> IndexedOneToOneRelation<T1, T2> makeHashBased() {
|
|
114 |
0
| return new IndexedOneToOneRelation<T1, T2>(CollectUtil.<T1, T2>hashMapFactory(),
|
|
115 |
| CollectUtil.<T2, T1>hashMapFactory()); |
|
116 |
| } |
|
117 |
| |
|
118 |
| |
|
119 |
0
| public static <T1, T2> IndexedOneToOneRelation<T1, T2> makeLinkedHashBased() {
|
|
120 |
0
| return new IndexedOneToOneRelation<T1, T2>(CollectUtil.<T1, T2>linkedHashMapFactory(),
|
|
121 |
| CollectUtil.<T2, T1>linkedHashMapFactory()); |
|
122 |
| } |
|
123 |
| |
|
124 |
| |
|
125 |
0
| public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>>
|
|
126 |
| IndexedOneToOneRelation<T1, T2> makeTreeBased() { |
|
127 |
0
| return new IndexedOneToOneRelation<T1, T2>(CollectUtil.<T1, T2>treeMapFactory(),
|
|
128 |
| CollectUtil.<T2, T1>treeMapFactory()); |
|
129 |
| } |
|
130 |
| |
|
131 |
| } |