package org.simantics.interop.utils; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import org.simantics.utils.datastructures.BijectionMap; public class LinkedBijectionMap { /** The keys of tableLeft are left-side-values and * values are right-side-values */ private final Map tableLeft = new LinkedHashMap(); /** The keys of tableRight are right-side-values and * values on it are left-side-values */ private final Map tableRight = new LinkedHashMap(); /** The keys of tableLeft are left-side-values and * values are right-side-values */ private final Map imTableLeft = Collections.unmodifiableMap(tableLeft); /** The keys of tableRight are right-side-values and * values on it are left-side-values */ private final Map imTableRight = Collections.unmodifiableMap(tableRight); private final Set imLeftSet = Collections.unmodifiableSet(tableLeft.keySet()); private final Set imRightSet = Collections.unmodifiableSet(tableRight.keySet()); public LinkedBijectionMap() { } public LinkedBijectionMap(LinkedBijectionMap copyFrom) { this.tableLeft.putAll(copyFrom.tableLeft); this.tableRight.putAll(copyFrom.tableRight); } public void addAll(BijectionMap map) { for (Entry e : map.getEntries()) map(e.getKey(), e.getValue()); } public boolean retainAllLeft(Collection values) { boolean result = false; Collection remove = new ArrayList(size()); for (L lValue : imLeftSet) { if (!values.contains(lValue)) { remove.add(lValue); result = true; } } if (!remove.isEmpty()) { for (L lValue : remove) removeWithLeft(lValue); } return result; } public boolean retainAllRight(Collection values) { boolean result = false; Collection remove = new ArrayList(size()); for (R rValue : imRightSet) { if (!values.contains(rValue)) { remove.add(rValue); result = true; } } if (!remove.isEmpty()) { for (R rValue : remove) removeWithRight(rValue); } return result; } public Set> getEntries() { return tableLeft.entrySet(); } public boolean containsLeft(L leftValue) { return tableLeft.containsKey(leftValue); } public boolean containsRight(R rightValue) { return tableRight.containsKey(rightValue); } public boolean contains(L leftValue, R rightValue) { if (leftValue==null || rightValue==null) return false; return rightValue.equals(tableLeft.get(leftValue)); } public void map(L leftValue, R rightValue) { if (leftValue == null || rightValue == null) throw new NullPointerException(); // Remove possible old mapping R oldRight = tableLeft.remove(leftValue); if (oldRight != null) { tableRight.remove(oldRight); } else { L oldLeft = tableRight.remove(rightValue); if (oldLeft != null) { tableLeft.remove(oldLeft); } } tableLeft.put(leftValue, rightValue); tableRight.put(rightValue, leftValue); } public boolean isEmpty() { return tableLeft.isEmpty(); } public int size() { return tableLeft.size(); } public L getLeft(R rightValue) { return tableRight.get(rightValue); } public R getRight(L leftValue) { return tableLeft.get(leftValue); } public R removeWithLeft(L leftValue) { R rightValue = tableLeft.remove(leftValue); if (rightValue!=null) tableRight.remove(rightValue); return rightValue; } public L removeWithRight(R rightValue) { L leftValue = tableRight.remove(rightValue); if (leftValue!=null) tableLeft.remove(leftValue); return leftValue; } /** * Get set of left values * * @return read-only set */ public Set getLeftSet() { return imLeftSet; } /** * Get set of right values * * @return read-only set */ public Set getRightSet() { return imRightSet; } /** * Get left-to-right map * * @return read only map */ public Map getLeftToRightMap() { return imTableLeft; } /** * Get right-to-left map * * @return read only map */ public Map getRightToLeftMap() { return imTableRight; } public void clear() { tableLeft.clear(); tableRight.clear(); } @Override public String toString() { int count = 0; StringBuilder sb = new StringBuilder(); sb.append("["); for (Entry e : tableLeft.entrySet()) { if (count++>0) sb.append(", "); sb.append(e.getKey().toString()); sb.append("="); sb.append(e.getValue().toString()); } sb.append("]"); return sb.toString(); } @Override public LinkedBijectionMap clone() { return new LinkedBijectionMap(this); } }