View Javadoc

1   package org.csc.phynixx.common.utils;
2   
3   /*
4    * #%L
5    * phynixx-common
6    * %%
7    * Copyright (C) 2014 Christoph Schmidt-Casdorff
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  
24  import java.util.*;
25  
26  /**
27   * Created by christoph on 09.02.14.
28   */
29  public class UnorderedMany2ManyAssociation<X, Y> {
30  
31      final Map<X, Set<Y>> mapA = new HashMap<X, Set<Y>>();
32  
33      final Map<Y, Set<X>> mapB = new HashMap<Y, Set<X>>();
34  
35  
36      public void associate(X x, Y y) {
37          assertSet(mapA, x).add(y);
38          assertSet(mapB, y).add(x);
39      }
40  
41  
42      public Set<Y> getX(X x) {
43          return getEndpoints(x, mapA);
44      }
45  
46      public Set<X> getY(Y y) {
47          return getEndpoints(y, mapB);
48      }
49  
50  
51      public void removeX(X x) {
52          removeDependents(x, mapA, mapB);
53          mapA.remove(x);
54      }
55  
56  
57      public void removeY(Y y) {
58          removeDependents(y, mapB, mapA);
59          mapB.remove(y);
60      }
61  
62  
63      public void disassociate(X x, Y y) {
64          removeDependent(x, y, mapA);
65          removeDependent(y, x, mapB);
66      }
67  
68  
69      static <K, V> Set<V> getEndpoints(K k, Map<K, Set<V>> map) {
70          Set<V> value = map.get(k);
71          if (value == null) {
72              return Collections.emptySet();
73          }
74          return Collections.unmodifiableSet(value);
75      }
76  
77  
78      static <K, V> void removeDependents(K key, Map<K, Set<V>> map, Map<V, Set<K>> inverseMap) {
79          Set<V> value = map.get(key);
80          if (value != null && !value.isEmpty()) {
81              for (V v : value) {
82                  Set<K> keys = inverseMap.get(v);
83                  if (keys != null && !keys.isEmpty()) {
84                      keys.remove(key);
85                  }
86              }
87          }
88      }
89  
90      static <K, V> void removeDependent(K k, V v, Map<K, Set<V>> map) {
91          Set<V> value = map.get(k);
92          if (value != null && !value.isEmpty()) {
93              value.remove(v);
94          }
95      }
96  
97  
98      static <K, V> Set<V> assertSet(Map<K, Set<V>> map, K key) {
99          Set<V> value = map.get(key);
100         if (value == null) {
101             value = new HashSet<V>();
102             map.put(key, value);
103         }
104         return value;
105     }
106 
107 
108 }