BUG-3263: Split off Fixed/Stacked YangInstanceIdentifier 84/20684/6
authorRobert Varga <rovarga@cisco.com>
Mon, 18 May 2015 19:03:09 +0000 (21:03 +0200)
committerRobert Varga <rovarga@cisco.com>
Tue, 19 May 2015 07:31:19 +0000 (09:31 +0200)
This patch makes YangInstanceIdentifier an abstract class, with two
subclasses: FixedYangInstanceIdentifier and
StackedYangInstanceIdentifier.

The fixed version uses an internal ImmutableList and derives efficiency
from it to the maximum extent allowed.

The stacked version stores a reference to the parent identifier and the
last path argument. It caches the support classes which provide
getPathArguments and getReversePathArguments.

Also introduce a getParent() method, as it can now be implemented
efficiently.

Change-Id: I0c4a771e74d031cae0b53a7f97221213222729f6
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/FixedYangInstanceIdentifier.java [new file with mode: 0644]
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedPathArguments.java [new file with mode: 0644]
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedReversePathArguments.java [new file with mode: 0644]
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedYangInstanceIdentifier.java [new file with mode: 0644]
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifierBuilder.java

diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/FixedYangInstanceIdentifier.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/FixedYangInstanceIdentifier.java
new file mode 100644 (file)
index 0000000..cfe4182
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. 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
+ */
+package org.opendaylight.yangtools.yang.data.api;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import java.io.ObjectStreamException;
+import java.util.List;
+import org.opendaylight.yangtools.util.HashCodeBuilder;
+
+final class FixedYangInstanceIdentifier extends YangInstanceIdentifier {
+    static final FixedYangInstanceIdentifier EMPTY_INSTANCE = new FixedYangInstanceIdentifier(ImmutableList.<PathArgument>of(), new HashCodeBuilder<>().build());
+    private static final long serialVersionUID = 1L;
+    private final ImmutableList<PathArgument> path;
+    private transient volatile YangInstanceIdentifier parent;
+
+    private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
+        super(hash);
+        this.path = Preconditions.checkNotNull(path, "path must not be null.");
+    }
+
+    static FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
+        return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return path.isEmpty();
+    }
+
+    @Override
+    public YangInstanceIdentifier getParent() {
+        if (path.isEmpty()) {
+            return null;
+        }
+
+        YangInstanceIdentifier ret = parent;
+        if (ret == null) {
+            ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
+            parent = ret;
+        }
+
+        return ret;
+    }
+
+    @Override
+    public List<PathArgument> getPath() {
+        return path;
+    }
+
+    @Override
+    public List<PathArgument> getPathArguments() {
+        return path;
+    }
+
+    @Override
+    public List<PathArgument> getReversePathArguments() {
+        return path.reverse();
+    }
+
+    @Override
+    List<PathArgument> tryPathArguments() {
+        return path;
+    }
+
+    @Override
+    public PathArgument getLastPathArgument() {
+        return path.isEmpty()? null : path.get(path.size() - 1);
+    }
+
+    @Override
+    YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
+        if (skipFromRoot == path.size()) {
+            return EMPTY_INSTANCE;
+        }
+
+        final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
+        final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
+        for (PathArgument a : newPath) {
+            hash.addArgument(a);
+        }
+
+        return new FixedYangInstanceIdentifier(newPath, hash.build());
+    }
+
+    private Object readResolve() throws ObjectStreamException {
+        return path.isEmpty() ? EMPTY_INSTANCE : this;
+    }
+
+    @Override
+    boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
+        if (other instanceof FixedYangInstanceIdentifier) {
+            return path.equals(((FixedYangInstanceIdentifier) other).path);
+        } else {
+            return super.pathArgumentsEqual(other);
+        }
+    }
+}
diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedPathArguments.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedPathArguments.java
new file mode 100644 (file)
index 0000000..6efb2b4
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. 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
+ */
+package org.opendaylight.yangtools.yang.data.api;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.UnmodifiableIterator;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+
+final class StackedPathArguments implements Iterable<PathArgument> {
+    private final List<StackedYangInstanceIdentifier> stack;
+    private final YangInstanceIdentifier base;
+
+    public StackedPathArguments(final YangInstanceIdentifier base, final List<StackedYangInstanceIdentifier> stack) {
+        this.base = Preconditions.checkNotNull(base);
+        this.stack = Preconditions.checkNotNull(stack);
+    }
+
+    @Override
+    public Iterator<PathArgument> iterator() {
+        return new IteratorImpl(base, stack);
+    }
+
+    private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
+        private final Iterator<StackedYangInstanceIdentifier> stack;
+        private final Iterator<PathArgument> base;
+
+        IteratorImpl(final YangInstanceIdentifier base, final Collection<StackedYangInstanceIdentifier> stack) {
+            this.base = base.getPathArguments().iterator();
+            this.stack = stack.iterator();
+        }
+
+        @Override
+        public boolean hasNext() {
+            return stack.hasNext();
+        }
+
+        @Override
+        public PathArgument next() {
+            if (base.hasNext()) {
+                return base.next();
+            }
+            return stack.next().getLastPathArgument();
+        }
+    }
+}
diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedReversePathArguments.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedReversePathArguments.java
new file mode 100644 (file)
index 0000000..54f6498
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. 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
+ */
+package org.opendaylight.yangtools.yang.data.api;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.UnmodifiableIterator;
+import java.util.Iterator;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+
+final class StackedReversePathArguments implements Iterable<PathArgument> {
+    private final StackedYangInstanceIdentifier identifier;
+
+    StackedReversePathArguments(final StackedYangInstanceIdentifier identifier) {
+        this.identifier = Preconditions.checkNotNull(identifier);
+    }
+
+    @Override
+    public Iterator<PathArgument> iterator() {
+        return new IteratorImpl(identifier);
+    }
+
+    private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
+        private StackedYangInstanceIdentifier identifier;
+        private Iterator<PathArgument> tail;
+
+        IteratorImpl(final StackedYangInstanceIdentifier identifier) {
+            this.identifier = Preconditions.checkNotNull(identifier);
+        }
+
+        @Override
+        public boolean hasNext() {
+            return tail == null || tail.hasNext();
+        }
+
+        @Override
+        public PathArgument next() {
+            if (tail != null) {
+                return tail.next();
+            }
+
+            final PathArgument ret = identifier.getLastPathArgument();
+            final YangInstanceIdentifier next = identifier.getParent();
+            if (!(next instanceof StackedYangInstanceIdentifier)) {
+                tail = next.getReversePathArguments().iterator();
+                identifier = null;
+            } else {
+                identifier = (StackedYangInstanceIdentifier) next;
+            }
+
+            return ret;
+        }
+    }
+}
diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedYangInstanceIdentifier.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedYangInstanceIdentifier.java
new file mode 100644 (file)
index 0000000..dc803ec
--- /dev/null
@@ -0,0 +1,158 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. 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
+ */
+package org.opendaylight.yangtools.yang.data.api;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Verify;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+
+final class StackedYangInstanceIdentifier extends YangInstanceIdentifier {
+    private static final long serialVersionUID = 1L;
+    private static final Field PARENT_FIELD;
+
+    static {
+        final Field f;
+        try {
+            f = StackedYangInstanceIdentifier.class.getDeclaredField("parent");
+        } catch (NoSuchFieldException | SecurityException e) {
+            throw new ExceptionInInitializerError(e);
+        }
+        f.setAccessible(true);
+
+        PARENT_FIELD = f;
+    }
+
+    @SuppressWarnings("rawtypes")
+    private static final AtomicReferenceFieldUpdater<StackedYangInstanceIdentifier, ImmutableList> LEGACYPATH_UPDATER =
+            AtomicReferenceFieldUpdater.newUpdater(StackedYangInstanceIdentifier.class, ImmutableList.class, "legacyPath");
+
+    private final YangInstanceIdentifier parent;
+    private final PathArgument pathArgument;
+
+    private transient volatile ImmutableList<PathArgument> legacyPath;
+    private transient volatile Iterable<PathArgument> pathArguments;
+    private transient volatile Iterable<PathArgument> reversePathArguments;
+
+    StackedYangInstanceIdentifier(final YangInstanceIdentifier parent, final PathArgument pathArgument, final int hash) {
+        super(hash);
+        this.parent = Preconditions.checkNotNull(parent);
+        this.pathArgument = Preconditions.checkNotNull(pathArgument);
+    }
+
+    @Override
+    public YangInstanceIdentifier getParent() {
+        return parent;
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return false;
+    }
+
+    @Override
+    public List<PathArgument> getPath() {
+        // Temporary variable saves a volatile read
+        ImmutableList<PathArgument> ret = legacyPath;
+        if (ret == null) {
+            // We could have used a synchronized block, but the window is quite
+            // small and worst that can happen is duplicate object construction.
+            ret = ImmutableList.copyOf(getPathArguments());
+            LEGACYPATH_UPDATER.lazySet(this, ret);
+        }
+
+        return ret;
+    }
+
+    @Override
+    public Iterable<PathArgument> getPathArguments() {
+        Iterable<PathArgument> ret = tryPathArguments();
+        if (ret == null) {
+            List<StackedYangInstanceIdentifier> stack = new ArrayList<>();
+            YangInstanceIdentifier current = this;
+            while (current.tryPathArguments() == null) {
+                Verify.verify(current instanceof StackedYangInstanceIdentifier);
+
+                final StackedYangInstanceIdentifier stacked = (StackedYangInstanceIdentifier) current;
+                stack.add(stacked);
+                current = stacked.getParent();
+            }
+
+            ret = new StackedPathArguments(current, Lists.reverse(stack));
+            pathArguments = ret;
+        }
+
+        return ret;
+    }
+
+    @Override
+    public Iterable<PathArgument> getReversePathArguments() {
+        Iterable<PathArgument> ret = reversePathArguments;
+        if (ret == null) {
+            ret = new StackedReversePathArguments(this);
+            reversePathArguments = ret;
+        }
+        return ret;
+    }
+
+    @Override
+    public PathArgument getLastPathArgument() {
+        return pathArgument;
+    }
+
+    @Override
+    Iterable<PathArgument> tryPathArguments() {
+        return pathArguments;
+    }
+
+    @Override
+    YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
+        // TODO: can we optimize this one?
+        return YangInstanceIdentifier.create(Iterables.skip(getPathArguments(), skipFromRoot));
+    }
+
+    @Override
+    boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
+        if (other instanceof StackedYangInstanceIdentifier) {
+            final StackedYangInstanceIdentifier stacked = (StackedYangInstanceIdentifier) other;
+            return pathArgument.equals(stacked.pathArgument) && parent.equals(stacked.parent);
+        } else {
+            return super.pathArgumentsEqual(other);
+        }
+    }
+
+    private void readObject(final ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
+        inputStream.defaultReadObject();
+
+        final FixedYangInstanceIdentifier p = (FixedYangInstanceIdentifier) inputStream.readObject();
+        try {
+            PARENT_FIELD.set(this, p);
+        } catch (IllegalArgumentException | IllegalAccessException e) {
+            throw new IOException("Failed to set parent", e);
+        }
+    }
+
+    private void writeObject(final ObjectOutputStream outputStream) throws IOException {
+        outputStream.defaultWriteObject();
+
+        final FixedYangInstanceIdentifier p;
+        if (parent instanceof FixedYangInstanceIdentifier) {
+            p = (FixedYangInstanceIdentifier) parent;
+        } else {
+            p = FixedYangInstanceIdentifier.create(parent.getPathArguments(), parent.hashCode());
+        }
+        outputStream.writeObject(p);
+    }
+}
index 9ead21a3884b036185e3a6c5f380aa5d0110d2a6..55599305c09aadcf27003a9b541c0c3bf6b156e3 100644 (file)
@@ -8,20 +8,12 @@ package org.opendaylight.yangtools.yang.data.api;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.ObjectStreamException;
 import java.io.Serializable;
 import java.lang.reflect.Array;
-import java.lang.reflect.Field;
 import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -29,6 +21,8 @@ import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Builder;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.concepts.Path;
@@ -69,50 +63,43 @@ import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
  *
  * @see <a href="http://tools.ietf.org/html/rfc6020#section-9.13">RFC6020</a>
  */
-public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier>, Immutable, Serializable {
+public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentifier>, Immutable, Serializable {
     /**
      * An empty {@link YangInstanceIdentifier}. It corresponds to the path of the conceptual
      * root of the YANG namespace.
      */
-    public static final YangInstanceIdentifier EMPTY = trustedCreate(Collections.<PathArgument>emptyList());
-    @SuppressWarnings("rawtypes")
-    private static final AtomicReferenceFieldUpdater<YangInstanceIdentifier, ImmutableList> LEGACYPATH_UPDATER =
-            AtomicReferenceFieldUpdater.newUpdater(YangInstanceIdentifier.class, ImmutableList.class, "legacyPath");
+    public static final YangInstanceIdentifier EMPTY = FixedYangInstanceIdentifier.EMPTY_INSTANCE;
+
     private static final AtomicReferenceFieldUpdater<YangInstanceIdentifier, String> TOSTRINGCACHE_UPDATER =
             AtomicReferenceFieldUpdater.newUpdater(YangInstanceIdentifier.class, String.class, "toStringCache");
-    private static final Field PATHARGUMENTS_FIELD;
+    private static final long serialVersionUID = 4L;
 
-    private static final long serialVersionUID = 3L;
-    private final transient Iterable<PathArgument> pathArguments;
     private final int hash;
-
-    private transient volatile ImmutableList<PathArgument> legacyPath = null;
     private transient volatile String toStringCache = null;
 
-    static {
-        final Field f;
-        try {
-            f = YangInstanceIdentifier.class.getDeclaredField("pathArguments");
-        } catch (NoSuchFieldException | SecurityException e) {
-            throw new ExceptionInInitializerError(e);
-        }
-        f.setAccessible(true);
-
-        PATHARGUMENTS_FIELD = f;
+    // Package-private to prevent outside subclassing
+    YangInstanceIdentifier(final int hash) {
+        this.hash = hash;
     }
 
-    private ImmutableList<PathArgument> getLegacyPath() {
-        // Temporary variable saves a volatile read
-        ImmutableList<PathArgument> ret = legacyPath;
-        if (ret == null) {
-            // We could have used a synchronized block, but the window is quite
-            // small and worst that can happen is duplicate object construction.
-            ret = ImmutableList.copyOf(pathArguments);
-            LEGACYPATH_UPDATER.lazySet(this, ret);
-        }
+    @Nonnull abstract YangInstanceIdentifier createRelativeIdentifier(int skipFromRoot);
+    @Nonnull abstract Iterable<PathArgument> tryPathArguments();
 
-        return ret;
-    }
+    /**
+     * Check if this instance identifier has empty path arguments, e.g. it is
+     * empty and corresponds to {@link #EMPTY}.
+     *
+     * @return True if this instance identifier is empty, false otherwise.
+     */
+    public abstract boolean isEmpty();
+
+    /**
+     * Return the conceptual parent {@link YangInstanceIdentifier}, which has
+     * one item less in {@link #getPathArguments()}.
+     *
+     * @return Parent {@link YangInstanceIdentifier}, or null if this is object is {@link #EMPTY}.
+     */
+    @Nullable public abstract YangInstanceIdentifier getParent();
 
     /**
      * Returns a list of path arguments.
@@ -121,18 +108,14 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
      * @return Immutable list of path arguments.
      */
     @Deprecated
-    public List<PathArgument> getPath() {
-        return getLegacyPath();
-    }
+    public abstract List<PathArgument> getPath();
 
     /**
      * Returns an ordered iteration of path arguments.
      *
      * @return Immutable iteration of path arguments.
      */
-    public Iterable<PathArgument> getPathArguments() {
-        return pathArguments;
-    }
+    public abstract Iterable<PathArgument> getPathArguments();
 
     /**
      * Returns an iterable of path arguments in reverse order. This is useful
@@ -140,9 +123,7 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
      *
      * @return Immutable iterable of path arguments in reverse order.
      */
-    public Iterable<PathArgument> getReversePathArguments() {
-        return getLegacyPath().reverse();
-    }
+    public abstract Iterable<PathArgument> getReversePathArguments();
 
     /**
      * Returns the last PathArgument. This is equivalent of iterating
@@ -150,30 +131,19 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
      *
      * @return The last past argument, or null if there are no PathArguments.
      */
-    public PathArgument getLastPathArgument() {
-        return Iterables.getFirst(getReversePathArguments(), null);
-    }
+    public abstract PathArgument getLastPathArgument();
 
-    YangInstanceIdentifier(final Iterable<PathArgument> path, final int hash) {
-        this.pathArguments = Preconditions.checkNotNull(path, "path must not be null.");
-        this.hash = hash;
-    }
+    public static YangInstanceIdentifier create(final Iterable<? extends PathArgument> path) {
+        if (Iterables.isEmpty(path)) {
+            return EMPTY;
+        }
 
-    private static YangInstanceIdentifier trustedCreate(final Iterable<PathArgument> path) {
         final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
         for (PathArgument a : path) {
             hash.addArgument(a);
         }
 
-        return new YangInstanceIdentifier(path, hash.build());
-    }
-
-    public static YangInstanceIdentifier create(final Iterable<? extends PathArgument> path) {
-        if (Iterables.isEmpty(path)) {
-            return EMPTY;
-        }
-
-        return trustedCreate(ImmutableList.copyOf(path));
+        return FixedYangInstanceIdentifier.create(path, hash.build());
     }
 
     public static YangInstanceIdentifier create(final PathArgument... path) {
@@ -182,7 +152,7 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
     }
 
     @Override
-    public int hashCode() {
+    public final int hashCode() {
         /*
          * The caching is safe, since the object contract requires
          * immutability of the object and all objects referenced from this
@@ -193,22 +163,24 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
         return hash;
     }
 
+    boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
+        return Iterables.elementsEqual(getPathArguments(), other.getPathArguments());
+    }
+
     @Override
     public boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
+        if (!(obj instanceof YangInstanceIdentifier)) {
             return false;
         }
         YangInstanceIdentifier other = (YangInstanceIdentifier) obj;
         if (this.hashCode() != obj.hashCode()) {
             return false;
         }
-        return Iterables.elementsEqual(pathArguments, other.pathArguments);
+
+        return pathArgumentsEqual(other);
     }
 
     /**
@@ -217,7 +189,7 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
      * @param name QName of {@link NodeIdentifier}
      * @return Instance Identifier with additional path argument added to the end.
      */
-    public YangInstanceIdentifier node(final QName name) {
+    public final YangInstanceIdentifier node(final QName name) {
         return node(new NodeIdentifier(name));
     }
 
@@ -228,8 +200,8 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
      * @param arg Path argument which should be added to the end
      * @return Instance Identifier with additional path argument added to the end.
      */
-    public YangInstanceIdentifier node(final PathArgument arg) {
-        return new YangInstanceIdentifier(Iterables.concat(pathArguments, Collections.singleton(arg)), HashCodeBuilder.nextHashCode(hash, arg));
+    public final YangInstanceIdentifier node(final PathArgument arg) {
+        return new StackedYangInstanceIdentifier(this, arg, HashCodeBuilder.nextHashCode(hash, arg));
     }
 
     /**
@@ -242,8 +214,8 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
      *         the specified parent is not in fact an ancestor of this object.
      */
     public Optional<YangInstanceIdentifier> relativeTo(final YangInstanceIdentifier ancestor) {
-        final Iterator<?> lit = pathArguments.iterator();
-        final Iterator<?> oit = ancestor.pathArguments.iterator();
+        final Iterator<?> lit = getPathArguments().iterator();
+        final Iterator<?> oit = ancestor.getPathArguments().iterator();
         int common = 0;
 
         while (oit.hasNext()) {
@@ -261,7 +233,8 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
         if (!lit.hasNext()) {
             return Optional.of(EMPTY);
         }
-        return Optional.of(trustedCreate(Iterables.skip(pathArguments, common)));
+
+        return Optional.of(createRelativeIdentifier(common));
     }
 
     private static int hashCode(final Object value) {
@@ -702,11 +675,11 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
     }
 
     @Override
-    public boolean contains(final YangInstanceIdentifier other) {
+    public final boolean contains(final YangInstanceIdentifier other) {
         Preconditions.checkArgument(other != null, "other should not be null");
 
-        final Iterator<?> lit = pathArguments.iterator();
-        final Iterator<?> oit = other.pathArguments.iterator();
+        final Iterator<?> lit = getPathArguments().iterator();
+        final Iterator<?> oit = other.getPathArguments().iterator();
 
         while (lit.hasNext()) {
             if (!oit.hasNext()) {
@@ -722,7 +695,7 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
     }
 
     @Override
-    public String toString() {
+    public final String toString() {
         /*
          * The toStringCache is safe, since the object contract requires
          * immutability of the object and all objects referenced from this
@@ -749,32 +722,4 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
         }
         return ret;
     }
-
-    private void readObject(final ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
-        inputStream.defaultReadObject();
-        legacyPath = ImmutableList.copyOf((Collection<PathArgument>)inputStream.readObject());
-
-        try {
-            PATHARGUMENTS_FIELD.set(this, legacyPath);
-        } catch (IllegalArgumentException | IllegalAccessException e) {
-            throw new IOException(e);
-        }
-    }
-
-    private Object readResolve() throws ObjectStreamException {
-        return legacyPath.isEmpty() ? EMPTY : this;
-    }
-
-    private void writeObject(final ObjectOutputStream outputStream) throws IOException {
-        /*
-         * This may look strange, but what we are doing here is side-stepping the fact
-         * that pathArguments is not generally serializable. We are forcing instantiation
-         * of the legacy path, which is an ImmutableList (thus Serializable) and write
-         * it out. The read path does the opposite -- it reads the legacyPath and then
-         * uses invocation API to set the field.
-         */
-        ImmutableList<PathArgument> pathArguments = getLegacyPath();
-        outputStream.defaultWriteObject();
-        outputStream.writeObject(pathArguments);
-    }
 }
index d0ea997f23942a56c8ea6f2b91ee6bc00df0147e..55f03af8009edf1265e826a40f97d42f37bd554f 100644 (file)
@@ -6,7 +6,6 @@
  */
 package org.opendaylight.yangtools.yang.data.api;
 
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import java.util.ArrayList;
 import java.util.List;
@@ -64,6 +63,6 @@ final class YangInstanceIdentifierBuilder implements InstanceIdentifierBuilder {
 
     @Override
     public YangInstanceIdentifier build() {
-        return new YangInstanceIdentifier(ImmutableList.copyOf(path), hash.build());
+        return FixedYangInstanceIdentifier.create(path, hash.build());
     }
 }
\ No newline at end of file