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.UnmodifiableIterator;
12 import java.util.Iterator;
13 import java.util.List;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 final class StackedPathArguments extends PathArgumentList {
18 private final List<PathArgument> base;
19 private final List<PathArgument> stack;
21 public StackedPathArguments(@Nonnull final YangInstanceIdentifier base, @Nonnull final List<PathArgument> stack) {
22 Verify.verify(!stack.isEmpty());
23 this.base = base.getPathArguments();
29 return stack.size() + base.size();
33 public boolean contains(final Object o) {
34 final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
35 return stack.contains(srch) || base.contains(srch);
39 public final PathArgument get(final int index) {
40 if (index < base.size()) {
41 return base.get(index);
43 return stack.get(index - base.size());
48 public final int indexOf(final Object o) {
49 final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
51 int ret = base.indexOf(srch);
53 ret = stack.indexOf(srch);
55 return base.size() + ret;
62 public final int lastIndexOf(final Object o) {
63 final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
65 final int ret = stack.lastIndexOf(srch);
67 return base.size() + ret;
70 return base.lastIndexOf(srch);
74 public UnmodifiableIterator<PathArgument> iterator() {
75 return new IteratorImpl(base, stack);
78 private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
79 private final Iterator<PathArgument> stack;
80 private final Iterator<PathArgument> base;
82 IteratorImpl(final Iterable<PathArgument> base, final Iterable<PathArgument> stack) {
83 this.base = base.iterator();
84 this.stack = stack.iterator();
88 public boolean hasNext() {
89 return stack.hasNext();
93 public PathArgument next() {