X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scenegraph%2Fsrc%2Forg%2Fsimantics%2Fscenegraph%2FParentNode.java;h=510bbabb096ff7534f32ad56cfd37b5549058e4b;hb=e8b96542ea305825ae4dbf7c35f11275689ef564;hp=e9e0b4be3e1f5beb2107b89733a1ac72ba04bc2d;hpb=7396956f115b87067970f690588017fc05393e7a;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/ParentNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/ParentNode.java index e9e0b4be3..510bbabb0 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/ParentNode.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/ParentNode.java @@ -14,9 +14,11 @@ package org.simantics.scenegraph; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; +import gnu.trove.map.TLongObjectMap; import gnu.trove.map.hash.TLongObjectHashMap; /** @@ -46,8 +48,41 @@ public abstract class ParentNode extends Node { } }; - protected transient Map children = createChildMap(); - private transient TLongObjectHashMap childrenIdMap = new TLongObjectHashMap<>(); + private static class ImmutableIdMap extends TLongObjectHashMap { + private static final String MSG = "immutable singleton instance"; + + @Override + public String put(long key, String value) { + throw new UnsupportedOperationException(MSG); + } + @Override + public void putAll(Map map) { + throw new UnsupportedOperationException(MSG); + } + @Override + public void putAll(TLongObjectMap map) { + throw new UnsupportedOperationException(MSG); + } + @Override + public String putIfAbsent(long key, String value) { + throw new UnsupportedOperationException(MSG); + } + } + + /** + * This is the value given to {@link #children} when this node is disposed and + * cleaned up. + */ + private static final Map DISPOSED_CHILDREN = Collections.emptyMap(); + + /** + * This is the value given to {@link #childrenIdMap} when this node is disposed + * and cleaned up. + */ + private static final TLongObjectMap DISPOSED_CHILDREN_ID_MAP = new ImmutableIdMap(); + + protected transient Map children = createChildMap(); + private transient TLongObjectMap childrenIdMap = new TLongObjectHashMap<>(); /** * A cached value for the root node of this parent node. This makes it @@ -206,7 +241,7 @@ public abstract class ParentNode extends Node { */ @SuppressWarnings("unchecked") public Collection getNodes() { - return (Collection) children.values(); + return children.isEmpty() ? Collections.emptyList() : (Collection) children.values(); } public int getNodeCount() { @@ -232,15 +267,15 @@ public abstract class ParentNode extends Node { @Override public void cleanup() { retractMapping(); - if (children != null) { + if (children != DISPOSED_CHILDREN) { children.forEach((id, child) -> { child.cleanup(); child.setParent(null); }); children.clear(); childrenIdMap.clear(); - children = null; - childrenIdMap = null; + children = DISPOSED_CHILDREN; + childrenIdMap = DISPOSED_CHILDREN_ID_MAP; childrenChanged(); rootNodeCache = DISPOSED; }