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 static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.Iterators;
14 import com.google.common.collect.UnmodifiableIterator;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 final class StackedReversePathArguments extends PathArgumentList {
21 private final StackedYangInstanceIdentifier identifier;
23 private volatile int size;
25 StackedReversePathArguments(final StackedYangInstanceIdentifier identifier) {
26 this.identifier = requireNonNull(identifier);
29 private static int calculateSize(final @NonNull YangInstanceIdentifier parent) {
30 YangInstanceIdentifier current = parent;
31 for (int i = 1;; ++i) {
32 final Collection<PathArgument> args = current.tryReversePathArguments();
34 return i + args.size();
37 verify(current instanceof StackedYangInstanceIdentifier);
38 current = ((StackedYangInstanceIdentifier) current).getParent();
46 ret = calculateSize(identifier.getParent());
54 @SuppressWarnings("checkstyle:parameterName")
55 public boolean contains(final Object o) {
56 final PathArgument srch = (PathArgument) requireNonNull(o);
57 return Iterators.contains(iterator(), srch);
61 public PathArgument get(final int index) {
62 return Iterators.get(iterator(), index);
66 @SuppressWarnings("checkstyle:parameterName")
67 public int indexOf(final Object o) {
68 final PathArgument srch = (PathArgument) requireNonNull(o);
69 return super.indexOf(srch);
73 @SuppressWarnings("checkstyle:parameterName")
74 public int lastIndexOf(final Object o) {
75 final PathArgument srch = (PathArgument) requireNonNull(o);
78 final Iterator<PathArgument> it = iterator();
79 for (int i = 0; it.hasNext(); ++i) {
80 if (srch.equals(it.next())) {
89 public UnmodifiableIterator<PathArgument> iterator() {
90 return new IteratorImpl(identifier);
93 private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
94 private StackedYangInstanceIdentifier identifier;
95 private Iterator<PathArgument> tail;
97 IteratorImpl(final StackedYangInstanceIdentifier identifier) {
98 this.identifier = requireNonNull(identifier);
102 public boolean hasNext() {
103 return tail == null || tail.hasNext();
107 public PathArgument next() {
112 final PathArgument ret = identifier.getLastPathArgument();
113 final YangInstanceIdentifier next = identifier.getParent();
114 final Iterable<PathArgument> args = next.tryReversePathArguments();
116 tail = args.iterator();
119 verify(next instanceof StackedYangInstanceIdentifier);
120 identifier = (StackedYangInstanceIdentifier) next;