X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.procore%2Fsrc%2Ffi%2Fvtt%2Fsimantics%2Fprocore%2Finternal%2FBijectionMap.java;h=9a84788710266f5a1b5597f8fd4bbcd6164a7d28;hp=97c3a10477774399d408d7a8d04ed553a7c15865;hb=6f11a60dee43d620d500c0cf5af34a1d91c80a8b;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/BijectionMap.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/BijectionMap.java index 97c3a1047..9a8478871 100644 --- a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/BijectionMap.java +++ b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/BijectionMap.java @@ -1,119 +1,119 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -/* - * Created on Jan 21, 2005 - * - * Copyright Toni Kalajainen - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package fi.vtt.simantics.procore.internal; - -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -/** - * Bijection map is a Map that has no values or keys, only 1:1 mappings - * of values. These value/keys will be called with left and right side - * values. - * - * Each value can exist only once on a side - * - * @author Toni Kalajainen - */ -public class BijectionMap { - - /** The keys of tableLeft are left-side-values and - * values are right-side-values */ - private final Map tableLeft = new HashMap(); - /** The keys of tableRight are right-side-values and - * values on it are left-side-values */ - private final Map tableRight = new HashMap(); - - public boolean containsLeft(L leftValue) - { - return tableLeft.containsKey(leftValue); - } - - public boolean containsRight(R rightValue) - { - return tableRight.containsKey(rightValue); - } - - public void map(L leftValue, R rightValue) - { - // 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 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; - } - - public Set getLeftSet() { - return tableLeft.keySet(); - } - - public Set getRightSet() { - return tableRight.keySet(); - } - - public void clear() { - tableLeft.clear(); - tableRight.clear(); - } -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +/* + * Created on Jan 21, 2005 + * + * Copyright Toni Kalajainen + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package fi.vtt.simantics.procore.internal; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * Bijection map is a Map that has no values or keys, only 1:1 mappings + * of values. These value/keys will be called with left and right side + * values. + * + * Each value can exist only once on a side + * + * @author Toni Kalajainen + */ +public class BijectionMap { + + /** The keys of tableLeft are left-side-values and + * values are right-side-values */ + private final Map tableLeft = new HashMap(); + /** The keys of tableRight are right-side-values and + * values on it are left-side-values */ + private final Map tableRight = new HashMap(); + + public boolean containsLeft(L leftValue) + { + return tableLeft.containsKey(leftValue); + } + + public boolean containsRight(R rightValue) + { + return tableRight.containsKey(rightValue); + } + + public void map(L leftValue, R rightValue) + { + // 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 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; + } + + public Set getLeftSet() { + return tableLeft.keySet(); + } + + public Set getRightSet() { + return tableRight.keySet(); + } + + public void clear() { + tableLeft.clear(); + tableRight.clear(); + } +}