2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.api;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.Iterators;
13 import com.google.common.collect.UnmodifiableIterator;
14 import java.util.Collection;
15 import java.util.Iterator;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 final class StackedReversePathArguments extends PathArgumentList {
19 private final StackedYangInstanceIdentifier identifier;
21 private volatile boolean haveSize;
23 StackedReversePathArguments(final StackedYangInstanceIdentifier identifier) {
24 this.identifier = Preconditions.checkNotNull(identifier);
27 private static int calculateSize(final YangInstanceIdentifier parent) {
28 YangInstanceIdentifier current = parent;
29 for (int i = 1;; ++i) {
30 final Collection<PathArgument> args = current.tryReversePathArguments();
32 return i + args.size();
35 Verify.verify(current instanceof StackedYangInstanceIdentifier);
36 current = ((StackedYangInstanceIdentifier)current).getParent();
44 ret = calculateSize(identifier.getParent());
54 public boolean contains(final Object o) {
55 final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
56 return Iterators.contains(iterator(), srch);
60 public PathArgument get(final int index) {
61 return Iterators.get(iterator(), index);
65 public int indexOf(final Object o) {
66 final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
67 return super.indexOf(srch);
71 public int lastIndexOf(final Object o) {
72 final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
75 final Iterator<PathArgument> it = iterator();
76 for (int i = 0; it.hasNext(); ++i) {
77 if (srch.equals(it.next())) {
86 public UnmodifiableIterator<PathArgument> iterator() {
87 return new IteratorImpl(identifier);
90 private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
91 private StackedYangInstanceIdentifier identifier;
92 private Iterator<PathArgument> tail;
94 IteratorImpl(final StackedYangInstanceIdentifier identifier) {
95 this.identifier = Preconditions.checkNotNull(identifier);
99 public boolean hasNext() {
100 return tail == null || tail.hasNext();
104 public PathArgument next() {
109 final PathArgument ret = identifier.getLastPathArgument();
110 final YangInstanceIdentifier next = identifier.getParent();
111 final Iterable<PathArgument> args = next.tryReversePathArguments();
113 tail = args.iterator();
116 Verify.verify(next instanceof StackedYangInstanceIdentifier);
117 identifier = (StackedYangInstanceIdentifier) next;