2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.yangtools.yang.data.api;
9 import com.google.common.base.Preconditions;
10 import com.google.common.base.Verify;
11 import com.google.common.collect.Iterators;
12 import com.google.common.collect.UnmodifiableIterator;
13 import java.util.Collection;
14 import java.util.Iterator;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 final class StackedReversePathArguments extends PathArgumentList {
18 private final StackedYangInstanceIdentifier identifier;
20 private volatile boolean haveSize;
22 StackedReversePathArguments(final StackedYangInstanceIdentifier identifier) {
23 this.identifier = Preconditions.checkNotNull(identifier);
26 private static int calculateSize(final YangInstanceIdentifier parent) {
27 YangInstanceIdentifier current = parent;
28 for (int i = 1;; ++i) {
29 final Collection<PathArgument> args = current.tryReversePathArguments();
31 return i + args.size();
34 Verify.verify(current instanceof StackedYangInstanceIdentifier);
35 current = ((StackedYangInstanceIdentifier)current).getParent();
43 ret = calculateSize(identifier.getParent());
53 public boolean contains(final Object o) {
54 final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
55 return Iterators.contains(iterator(), srch);
59 public PathArgument get(final int index) {
60 return Iterators.get(iterator(), index);
64 public int indexOf(final Object o) {
65 final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
66 return super.indexOf(srch);
70 public int lastIndexOf(final Object o) {
71 final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
74 final Iterator<PathArgument> it = iterator();
75 for (int i = 0; it.hasNext(); ++i) {
76 if (srch.equals(it.next())) {
85 public UnmodifiableIterator<PathArgument> iterator() {
86 return new IteratorImpl(identifier);
89 private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
90 private StackedYangInstanceIdentifier identifier;
91 private Iterator<PathArgument> tail;
93 IteratorImpl(final StackedYangInstanceIdentifier identifier) {
94 this.identifier = Preconditions.checkNotNull(identifier);
98 public boolean hasNext() {
99 return tail == null || tail.hasNext();
103 public PathArgument next() {
108 final PathArgument ret = identifier.getLastPathArgument();
109 final YangInstanceIdentifier next = identifier.getParent();
110 final Iterable<PathArgument> args = next.tryReversePathArguments();
112 tail = args.iterator();
115 Verify.verify(next instanceof StackedYangInstanceIdentifier);
116 identifier = (StackedYangInstanceIdentifier) next;