X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scenegraph%2Fsrc%2Forg%2Fsimantics%2Fscenegraph%2FLookupService.java;h=2fd35bb5b6161076e825e8c87ace12ff75f32b25;hp=548feca39a8680a1e75ffb080957bba5ed80a9a2;hb=452670c58399d8054872655841ebb6e66d9c6b6e;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/LookupService.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/LookupService.java index 548feca39..2fd35bb5b 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/LookupService.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/LookupService.java @@ -1,122 +1,122 @@ -/******************************************************************************* - * 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 - *******************************************************************************/ -package org.simantics.scenegraph; - -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * A synchronized reference implementation of {@link ILookupService}. - * - * @author Tuukka Lehtonen - */ -public class LookupService implements ILookupService { - - private final Object lookupLock = new Object(); - private final Map toNode = new HashMap(); - private final Map toId = new HashMap(); - - Logger logger = Logger.getLogger(getClass().getName()); - - @Override - public INode map(String id, INode node) { - if (id == null) - throw new NullPointerException("null id"); - if (node == null) - throw new NullPointerException("null node"); - - INode oldNode; - String oldId; - synchronized (lookupLock) { - oldNode = toNode.put(id, node); - oldId = toId.put(node, id); - - // Keep the mapping a consistent bijection: - // If ID => INode mapping is removed, the INode => ID mappings must - // removed also. - - if (oldNode != null && !oldNode.equals(node)) { - String removedId = toId.remove(oldNode); - if (!id.equals(removedId)) - toNode.remove(removedId); - } - if (oldId != null && !oldId.equals(id)) { - INode removedNode = toNode.remove(oldId); - if (removedNode != node) - toId.remove(removedNode); - } - } - if (logger.isLoggable(Level.FINE)) - logger.fine("map(" + id + ", " + node + ")"); - if (oldNode != null || oldId != null) { - if (logger.isLoggable(Level.INFO)) { - logger.info("replaced mappings for ID " + oldId + " and node " + oldNode); - } - } - return oldNode; - } - - @Override - public INode unmap(String id) { - INode node; - String mappedId; - synchronized (lookupLock) { - node = toNode.remove(id); - if (node == null) - return null; - mappedId = toId.remove(node); - } - if (logger.isLoggable(Level.FINE)) - logger.fine("unmap(" + id + "): " + node); - if (mappedId != null && !mappedId.equals(id)) { - if (logger.isLoggable(Level.WARNING)) - logger.log(Level.WARNING, "mapping was out-of-sync: " + id + " => " + node + " & " + mappedId + " => " + node, new Exception("trace")); - } - return node; - } - - @Override - public String unmap(INode node) { - String id; - INode mappedNode; - synchronized (lookupLock) { - id = toId.remove(node); - if (node == null) - return null; - mappedNode = toNode.remove(id); - } - if (logger.isLoggable(Level.FINE)) - logger.fine("unmap(" + node + "): " + id); - if (mappedNode != null && node != mappedNode) { - if (logger.isLoggable(Level.WARNING)) - logger.log(Level.WARNING, "mapping was out-of-sync: " + node + " => " + id + " & " + id + " => " + mappedNode, new Exception("trace")); - } - return id; - } - - @Override - public INode lookupNode(String id) { - synchronized (lookupLock) { - return toNode.get(id); - } - } - - @Override - public String lookupId(INode node) { - synchronized (lookupLock) { - return toId.get(node); - } - } - -} +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.simantics.scenegraph; + +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * A synchronized reference implementation of {@link ILookupService}. + * + * @author Tuukka Lehtonen + */ +public class LookupService implements ILookupService { + + private final Object lookupLock = new Object(); + private final Map toNode = new HashMap(); + private final Map toId = new HashMap(); + + Logger logger = Logger.getLogger(getClass().getName()); + + @Override + public INode map(String id, INode node) { + if (id == null) + throw new NullPointerException("null id"); + if (node == null) + throw new NullPointerException("null node"); + + INode oldNode; + String oldId; + synchronized (lookupLock) { + oldNode = toNode.put(id, node); + oldId = toId.put(node, id); + + // Keep the mapping a consistent bijection: + // If ID => INode mapping is removed, the INode => ID mappings must + // removed also. + + if (oldNode != null && !oldNode.equals(node)) { + String removedId = toId.remove(oldNode); + if (!id.equals(removedId)) + toNode.remove(removedId); + } + if (oldId != null && !oldId.equals(id)) { + INode removedNode = toNode.remove(oldId); + if (removedNode != node) + toId.remove(removedNode); + } + } + if (logger.isLoggable(Level.FINE)) + logger.fine("map(" + id + ", " + node + ")"); + if (oldNode != null || oldId != null) { + if (logger.isLoggable(Level.INFO)) { + logger.info("replaced mappings for ID " + oldId + " and node " + oldNode); + } + } + return oldNode; + } + + @Override + public INode unmap(String id) { + INode node; + String mappedId; + synchronized (lookupLock) { + node = toNode.remove(id); + if (node == null) + return null; + mappedId = toId.remove(node); + } + if (logger.isLoggable(Level.FINE)) + logger.fine("unmap(" + id + "): " + node); + if (mappedId != null && !mappedId.equals(id)) { + if (logger.isLoggable(Level.WARNING)) + logger.log(Level.WARNING, "mapping was out-of-sync: " + id + " => " + node + " & " + mappedId + " => " + node, new Exception("trace")); + } + return node; + } + + @Override + public String unmap(INode node) { + String id; + INode mappedNode; + synchronized (lookupLock) { + id = toId.remove(node); + if (node == null) + return null; + mappedNode = toNode.remove(id); + } + if (logger.isLoggable(Level.FINE)) + logger.fine("unmap(" + node + "): " + id); + if (mappedNode != null && node != mappedNode) { + if (logger.isLoggable(Level.WARNING)) + logger.log(Level.WARNING, "mapping was out-of-sync: " + node + " => " + id + " & " + id + " => " + mappedNode, new Exception("trace")); + } + return id; + } + + @Override + public INode lookupNode(String id) { + synchronized (lookupLock) { + return toNode.get(id); + } + } + + @Override + public String lookupId(INode node) { + synchronized (lookupLock) { + return toId.get(node); + } + } + +}