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 *******************************************************************************/
12 package org.simantics.scenegraph;
14 import java.util.HashMap;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
20 * A synchronized reference implementation of {@link ILookupService}.
22 * @author Tuukka Lehtonen
24 public class LookupService implements ILookupService {
26 private final Object lookupLock = new Object();
27 private final Map<String, INode> toNode = new HashMap<String, INode>();
28 private final Map<INode, String> toId = new HashMap<INode, String>();
30 Logger logger = Logger.getLogger(getClass().getName());
33 public INode map(String id, INode node) {
35 throw new NullPointerException("null id");
37 throw new NullPointerException("null node");
41 synchronized (lookupLock) {
42 oldNode = toNode.put(id, node);
43 oldId = toId.put(node, id);
45 // Keep the mapping a consistent bijection:
46 // If ID => INode mapping is removed, the INode => ID mappings must
49 if (oldNode != null && !oldNode.equals(node)) {
50 String removedId = toId.remove(oldNode);
51 if (!id.equals(removedId))
52 toNode.remove(removedId);
54 if (oldId != null && !oldId.equals(id)) {
55 INode removedNode = toNode.remove(oldId);
56 if (removedNode != node)
57 toId.remove(removedNode);
60 if (logger.isLoggable(Level.FINE))
61 logger.fine("map(" + id + ", " + node + ")");
62 if (oldNode != null || oldId != null) {
63 if (logger.isLoggable(Level.INFO)) {
64 logger.info("replaced mappings for ID " + oldId + " and node " + oldNode);
71 public INode unmap(String id) {
74 synchronized (lookupLock) {
75 node = toNode.remove(id);
78 mappedId = toId.remove(node);
80 if (logger.isLoggable(Level.FINE))
81 logger.fine("unmap(" + id + "): " + node);
82 if (mappedId != null && !mappedId.equals(id)) {
83 if (logger.isLoggable(Level.WARNING))
84 logger.log(Level.WARNING, "mapping was out-of-sync: " + id + " => " + node + " & " + mappedId + " => " + node, new Exception("trace"));
90 public String unmap(INode node) {
93 synchronized (lookupLock) {
94 id = toId.remove(node);
97 mappedNode = toNode.remove(id);
99 if (logger.isLoggable(Level.FINE))
100 logger.fine("unmap(" + node + "): " + id);
101 if (mappedNode != null && node != mappedNode) {
102 if (logger.isLoggable(Level.WARNING))
103 logger.log(Level.WARNING, "mapping was out-of-sync: " + node + " => " + id + " & " + id + " => " + mappedNode, new Exception("trace"));
109 public INode lookupNode(String id) {
110 synchronized (lookupLock) {
111 return toNode.get(id);
116 public String lookupId(INode node) {
117 synchronized (lookupLock) {
118 return toId.get(node);