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