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 boolean haveSize;
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());
55 @SuppressWarnings("checkstyle:parameterName")
56 public boolean contains(final Object o) {
57 final PathArgument srch = (PathArgument) requireNonNull(o);
58 return Iterators.contains(iterator(), srch);
62 public PathArgument get(final int index) {
63 return Iterators.get(iterator(), index);
67 @SuppressWarnings("checkstyle:parameterName")
68 public int indexOf(final Object o) {
69 final PathArgument srch = (PathArgument) requireNonNull(o);
70 return super.indexOf(srch);
74 @SuppressWarnings("checkstyle:parameterName")
75 public int lastIndexOf(final Object o) {
76 final PathArgument srch = (PathArgument) requireNonNull(o);
79 final Iterator<PathArgument> it = iterator();
80 for (int i = 0; it.hasNext(); ++i) {
81 if (srch.equals(it.next())) {
90 public UnmodifiableIterator<PathArgument> iterator() {
91 return new IteratorImpl(identifier);
94 private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
95 private StackedYangInstanceIdentifier identifier;
96 private Iterator<PathArgument> tail;
98 IteratorImpl(final StackedYangInstanceIdentifier identifier) {
99 this.identifier = requireNonNull(identifier);
103 public boolean hasNext() {
104 return tail == null || tail.hasNext();
108 public PathArgument next() {
113 final PathArgument ret = identifier.getLastPathArgument();
114 final YangInstanceIdentifier next = identifier.getParent();
115 final Iterable<PathArgument> args = next.tryReversePathArguments();
117 tail = args.iterator();
120 verify(next instanceof StackedYangInstanceIdentifier);
121 identifier = (StackedYangInstanceIdentifier) next;