001    /*--------------------------------------------------------------------------+
002    $Id: IdentityPairMap.java 26283 2010-02-18 11:18:57Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.collections;
019    
020    import java.util.IdentityHashMap;
021    import java.util.Map;
022    
023    /**
024     * A map using (unordered) pairs as key which are compared by reference.
025     * 
026     * @author hummelb
027     * @author $Author: juergens $
028     * @version $Rev: 26283 $
029     * @levd.rating GREEN Hash: 048446DAEE51D1CCA53E05BB3D4283F1
030     */
031    public class IdentityPairMap<T, V> {
032    
033            /** Internal storage. */
034            private final Map<T, Map<T, V>> map = new IdentityHashMap<T, Map<T, V>>();
035    
036            /** Adds the given value for the given pair. */
037            public void put(ImmutablePair<T, T> pair, V value) {
038                    put(pair.getFirst(), pair.getSecond(), value);
039            }
040    
041            /** Adds the given value for the given pair. */
042            public void put(T t1, T t2, V value) {
043                    insert(t1, t2, value);
044                    insert(t2, t1, value);
045            }
046    
047            /** Helper method for insertion */
048            private void insert(T t1, T t2, V value) {
049                    Map<T, V> m = map.get(t1);
050                    if (m == null) {
051                            m = new IdentityHashMap<T, V>();
052                            map.put(t1, m);
053                    }
054                    m.put(t2, value);
055            }
056    
057            /** Returns whether the pair is contained. */
058            public boolean contains(ImmutablePair<T, T> pair) {
059                    return contains(pair.getFirst(), pair.getSecond());
060            }
061    
062            /** Returns whether the pair (t1, t2) is contained. */
063            public boolean contains(T t1, T t2) {
064                    Map<T, V> m = map.get(t1);
065                    if (m == null) {
066                            return false;
067                    }
068                    return m.containsKey(t2);
069            }
070    
071            /**
072             * Returns the element stored at the pair or <code>null</code> if not
073             * stored.
074             */
075            public V get(ImmutablePair<T, T> pair) {
076                    return get(pair.getFirst(), pair.getSecond());
077            }
078    
079            /**
080             * Returns the element stored at pair (t1, t2) or <code>null</code> if not
081             * stored.
082             */
083            public V get(T t1, T t2) {
084                    Map<T, V> m = map.get(t1);
085                    if (m == null) {
086                            return null;
087                    }
088                    return m.get(t2);
089            }
090    
091            /** Clears the map. */
092            public void clear() {
093                    map.clear();
094            }
095    }