X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fapi%2FYangInstanceIdentifier.java;h=a8a02c84189d730ed8a0b55661c286d9a91bcd12;hb=c24d6e2f39acbb11e22b5676bb7481ed52bec461;hp=caee3c30d6450c1b44349c7a1bb99f728c35b31b;hpb=18106ed2f407f10077bc2a698c2b835fc9a857a4;p=yangtools.git
diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java
index caee3c30d6..a8a02c8418 100644
--- a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java
+++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java
@@ -10,7 +10,9 @@ package org.opendaylight.yangtools.yang.data.api;
import com.google.common.annotations.Beta;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableMap;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import java.io.Serializable;
@@ -31,6 +33,7 @@ import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.concepts.Path;
import org.opendaylight.yangtools.util.HashCodeBuilder;
import org.opendaylight.yangtools.util.ImmutableOffsetMap;
+import org.opendaylight.yangtools.util.SharedSingletonMap;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
@@ -43,16 +46,18 @@ import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
* which conceptually is XPath expression minimized to uniquely identify element
* in data tree which conforms to constraints maintained by YANG Model,
* effectively this makes Instance Identifier a path to element in data tree.
+ *
*
* Constraints put in YANG specification on instance-identifier allowed it to be
* effectively represented in Java and it's evaluation does not require
* full-blown XPath processor.
- *
+ *
* Path Arguments
+ *
* Path to the node represented in instance identifier consists of
* {@link PathArgument} which carries necessary information to uniquely identify
* node on particular level in the subtree.
- *
+ *
*
* - {@link NodeIdentifier} - Identifier of node, which has cardinality
*
0..1
in particular subtree in data tree.
@@ -111,10 +116,19 @@ public abstract class YangInstanceIdentifier implements Path relativeTo(final YangInstanceIdentifier ancestor) {
+ if (this == ancestor) {
+ return Optional.of(EMPTY);
+ }
+ if (ancestor.isEmpty()) {
+ return Optional.of(this);
+ }
+
final Iterator> lit = getPathArguments().iterator();
final Iterator> oit = ancestor.getPathArguments().iterator();
int common = 0;
@@ -243,9 +264,12 @@ public abstract class YangInstanceIdentifier implements Path lit = getPathArguments().iterator();
final Iterator> oit = other.getPathArguments().iterator();
@@ -393,7 +417,7 @@ public abstract class YangInstanceIdentifier implements Path {
- /**
- * Adds a {@link PathArgument} to to path arguments of resulting instance identifier.
- *
- * @param arg A {@link PathArgument} to be added
- * @return this builder
- */
- InstanceIdentifierBuilder node(PathArgument arg);
-
- /**
- * Adds {@link NodeIdentifier} with supplied QName to path arguments of resulting instance identifier.
- *
- * @param nodeType QName of {@link NodeIdentifier} which will be added
- * @return this builder
- */
- InstanceIdentifierBuilder node(QName nodeType);
-
- /**
- * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key values to path arguments of resulting instance identifier.
- *
- * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
- * @param keyValues Map of key components and their respective values for {@link NodeIdentifierWithPredicates}
- * @return this builder
- */
- InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map keyValues);
-
- /**
- * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key, value.
- *
- * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
- * @param key QName of key which will be added
- * @param value value of key which will be added
- * @return this builder
- */
- InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value);
-
- /**
- *
- * Builds an {@link YangInstanceIdentifier} with path arguments from this builder
- *
- * @return {@link YangInstanceIdentifier}
- */
- @Override
- YangInstanceIdentifier build();
-
- /*
- * @deprecated use #build()
- */
- @Deprecated
- YangInstanceIdentifier toInstance();
- }
-
/**
* Simple path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.ContainerNode} or
* {@link org.opendaylight.yangtools.yang.data.api.schema.LeafNode} leaf in particular subtree.
*/
public static final class NodeIdentifier extends AbstractPathArgument {
private static final long serialVersionUID = -2255888212390871347L;
+ private static final LoadingCache CACHE = CacheBuilder.newBuilder().weakValues()
+ .build(new CacheLoader() {
+ @Override
+ public NodeIdentifier load(@Nonnull final QName key) {
+ return new NodeIdentifier(key);
+ }
+ });
public NodeIdentifier(final QName node) {
super(node);
}
+
+ /**
+ * Return a NodeIdentifier for a particular QName. Unlike the constructor, this factory method uses a global
+ * instance cache, resulting in object reuse for equal inputs.
+ *
+ * @param node Node's QName
+ * @return A {@link NodeIdentifier}
+ */
+ public static NodeIdentifier create(final QName node) {
+ return CACHE.getUnchecked(node);
+ }
}
/**
@@ -531,12 +518,13 @@ public abstract class YangInstanceIdentifier implements Path keyValues) {
super(node);
- // Retains ImmutableMap for maps with size() <= 1. For larger sizes uses a shared key set.
- this.keyValues = ImmutableOffsetMap.copyOf(keyValues);
+ // Retains ImmutableMap for empty maps. For larger sizes uses a shared key set.
+ this.keyValues = ImmutableOffsetMap.unorderedCopyOf(keyValues);
}
public NodeIdentifierWithPredicates(final QName node, final QName key, final Object value) {
- this(node, ImmutableMap.of(key, value));
+ super(node);
+ this.keyValues = SharedSingletonMap.unorderedOf(key, value);
}
public Map getKeyValues() {
@@ -597,17 +585,17 @@ public abstract class YangInstanceIdentifier implements Path extends AbstractPathArgument {
private static final long serialVersionUID = -3637456085341738431L;
- private final Object value;
+ private final T value;
- public NodeWithValue(final QName node, final Object value) {
+ public NodeWithValue(final QName node, final T value) {
super(node);
this.value = value;
}
- public Object getValue() {
+ public T getValue() {
return value;
}
@@ -615,7 +603,7 @@ public abstract class YangInstanceIdentifier implements Path other = (NodeWithValue>) obj;
return Objects.deepEquals(value, other.value);
}
@@ -686,9 +674,7 @@ public abstract class YangInstanceIdentifier implements Path {
+ /**
+ * Adds a {@link PathArgument} to to path arguments of resulting instance identifier.
+ *
+ * @param arg A {@link PathArgument} to be added
+ * @return this builder
+ */
+ InstanceIdentifierBuilder node(PathArgument arg);
+
+ /**
+ * Adds {@link NodeIdentifier} with supplied QName to path arguments of resulting instance identifier.
+ *
+ * @param nodeType QName of {@link NodeIdentifier} which will be added
+ * @return this builder
+ */
+ InstanceIdentifierBuilder node(QName nodeType);
+
+ /**
+ * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key values to path arguments of resulting instance identifier.
+ *
+ * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
+ * @param keyValues Map of key components and their respective values for {@link NodeIdentifierWithPredicates}
+ * @return this builder
+ */
+ InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map keyValues);
+
+ /**
+ * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key, value.
+ *
+ * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
+ * @param key QName of key which will be added
+ * @param value value of key which will be added
+ * @return this builder
+ */
+ InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value);
+
+ /**
+ *
+ * Builds an {@link YangInstanceIdentifier} with path arguments from this builder
+ *
+ * @return {@link YangInstanceIdentifier}
+ */
+ @Override
+ YangInstanceIdentifier build();
+ }
}