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   * Associations betweeen Instances of X and Y can be grouped. To join a group  you have to give an assoc you want to share the group. You can assign a group Value to a group
28   * Created by christoph on 09.02.14.
29   */
30  public class GroupingAssociation<X, Y, A> {
31  
32  
33      private final Map<X, Set<Y>> mapA = new HashMap<X, Set<Y>>();
34  
35      private final Map<Y, Set<X>> mapB = new HashMap<Y, Set<X>>();
36  
37  
38      public void associate(X x, Y y) {
39          assertSet(mapA, x).add(y);
40          assertSet(mapB, y).add(x);
41      }
42  
43  
44      public Set<Y> getX(X x) {
45          return getEndpoints(x, mapA);
46      }
47  
48      public Set<X> getY(Y y) {
49          return getEndpoints(y, mapB);
50      }
51  
52  
53      public void removeX(X x) {
54          removeDependents(x, mapA, mapB);
55          mapA.remove(x);
56      }
57  
58  
59      public void removeY(Y y) {
60          removeDependents(y, mapB, mapA);
61          mapB.remove(y);
62      }
63  
64  
65      public void disassociate(X x, Y y) {
66          removeDependent(x, y, mapA);
67          removeDependent(y, x, mapB);
68      }
69  
70  
71      static <K, V> Set<V> getEndpoints(K k, Map<K, Set<V>> map) {
72          Set<V> value = map.get(k);
73          if (value == null) {
74              return Collections.emptySet();
75          }
76          return Collections.unmodifiableSet(value);
77      }
78  
79  
80      static <K, V> void removeDependents(K key, Map<K, Set<V>> map, Map<V, Set<K>> inverseMap) {
81          Set<V> value = map.get(key);
82          if (value != null && !value.isEmpty()) {
83              for (V v : value) {
84                  Set<K> keys = inverseMap.get(v);
85                  if (keys != null && !keys.isEmpty()) {
86                      keys.remove(key);
87                  }
88              }
89          }
90      }
91  
92      static <K, V> void removeDependent(K k, V v, Map<K, Set<V>> map) {
93          Set<V> value = map.get(k);
94          if (value != null && !value.isEmpty()) {
95              value.remove(v);
96          }
97      }
98  
99  
100     static <K, V> Set<V> assertSet(Map<K, Set<V>> map, K key) {
101         Set<V> value = map.get(key);
102         if (value == null) {
103             value = new HashSet<V>();
104             map.put(key, value);
105         }
106         return value;
107     }
108 
109 
110 }