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