Fix eclipse/checkstyle warnings
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / StackedPathArguments.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.UnmodifiableIterator;
13 import java.util.Iterator;
14 import java.util.List;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17
18 final class StackedPathArguments extends PathArgumentList {
19     private final List<PathArgument> base;
20     private final List<PathArgument> stack;
21
22     StackedPathArguments(@Nonnull final YangInstanceIdentifier base, @Nonnull final List<PathArgument> stack) {
23         Verify.verify(!stack.isEmpty());
24         this.base = base.getPathArguments();
25         this.stack = stack;
26     }
27
28     @Override
29     public int size() {
30         return stack.size() + base.size();
31     }
32
33     @Override
34     @SuppressWarnings("checkstyle:parameterName")
35     public boolean contains(final Object o) {
36         final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
37         return stack.contains(srch) || base.contains(srch);
38     }
39
40     @Override
41     public PathArgument get(final int index) {
42         if (index < base.size()) {
43             return base.get(index);
44         }
45         return stack.get(index - base.size());
46     }
47
48     @Override
49     @SuppressWarnings("checkstyle:parameterName")
50     public int indexOf(final Object o) {
51         final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
52
53         int ret = base.indexOf(srch);
54         if (ret == -1) {
55             ret = stack.indexOf(srch);
56             if (ret != -1) {
57                 return base.size() + ret;
58             }
59         }
60         return ret;
61     }
62
63     @Override
64     @SuppressWarnings("checkstyle:parameterName")
65     public int lastIndexOf(final Object o) {
66         final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
67
68         final int ret = stack.lastIndexOf(srch);
69         if (ret != -1) {
70             return base.size() + ret;
71         }
72
73         return base.lastIndexOf(srch);
74     }
75
76     @Nonnull
77     @Override
78     public UnmodifiableIterator<PathArgument> iterator() {
79         return new IteratorImpl(base, stack);
80     }
81
82     private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
83         private final Iterator<PathArgument> stack;
84         private final Iterator<PathArgument> base;
85
86         IteratorImpl(final Iterable<PathArgument> base, final Iterable<PathArgument> stack) {
87             this.base = base.iterator();
88             this.stack = stack.iterator();
89         }
90
91         @Override
92         public boolean hasNext() {
93             return stack.hasNext();
94         }
95
96         @Override
97         public PathArgument next() {
98             if (base.hasNext()) {
99                 return base.next();
100             }
101             return stack.next();
102         }
103     }
104 }