1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
13 * Created on Jan 21, 2005
15 * Copyright Toni Kalajainen
17 * Licensed under the Apache License, Version 2.0 (the "License");
18 * you may not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
21 * http://www.apache.org/licenses/LICENSE-2.0
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS,
25 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
29 package org.simantics.acorn.internal;
31 import java.util.HashMap;
36 * Bijection map is a Map that has no values or keys, only 1:1 mappings
37 * of values. These value/keys will be called with left and right side
40 * Each value can exist only once on a side
42 * @author Toni Kalajainen
44 public class BijectionMap<L, R> {
46 /** The keys of tableLeft are left-side-values and
47 * values are right-side-values */
48 private final Map<L, R> tableLeft = new HashMap<L, R>();
49 /** The keys of tableRight are right-side-values and
50 * values on it are left-side-values */
51 private final Map<R, L> tableRight = new HashMap<R, L>();
53 public boolean containsLeft(L leftValue)
55 return tableLeft.containsKey(leftValue);
58 public boolean containsRight(R rightValue)
60 return tableRight.containsKey(rightValue);
63 public void map(L leftValue, R rightValue)
65 // Remove possible old mapping
66 R oldRight = tableLeft.remove(leftValue);
67 if (oldRight != null) {
68 tableRight.remove(oldRight);
70 L oldLeft = tableRight.remove(rightValue);
71 if (oldLeft != null) {
72 tableLeft.remove(oldLeft);
76 tableLeft.put(leftValue, rightValue);
77 tableRight.put(rightValue, leftValue);
82 return tableLeft.size();
85 public L getLeft(R rightValue) {
86 return tableRight.get(rightValue);
89 public R getRight(L leftValue) {
90 return tableLeft.get(leftValue);
93 public R removeWithLeft(L leftValue) {
94 R rightValue = tableLeft.remove(leftValue);
96 tableRight.remove(rightValue);
100 public L removeWithRight(R rightValue) {
101 L leftValue = tableRight.remove(rightValue);
103 tableLeft.remove(leftValue);
107 public Set<L> getLeftSet() {
108 return tableLeft.keySet();
111 public Set<R> getRightSet() {
112 return tableRight.keySet();
115 public void clear() {