Fix eclipse/checkstyle warnings
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / StackedReversePathArguments.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.Iterators;
13 import com.google.common.collect.UnmodifiableIterator;
14 import java.util.Collection;
15 import java.util.Iterator;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18
19 final class StackedReversePathArguments extends PathArgumentList {
20     private final StackedYangInstanceIdentifier identifier;
21     private int size;
22     private volatile boolean haveSize;
23
24     StackedReversePathArguments(final StackedYangInstanceIdentifier identifier) {
25         this.identifier = Preconditions.checkNotNull(identifier);
26     }
27
28     private static int calculateSize(final YangInstanceIdentifier parent) {
29         YangInstanceIdentifier current = parent;
30         for (int i = 1;; ++i) {
31             final Collection<PathArgument> args = current.tryReversePathArguments();
32             if (args != null) {
33                 return i + args.size();
34             }
35
36             Verify.verify(current instanceof StackedYangInstanceIdentifier);
37             current = current.getParent();
38         }
39     }
40
41     @Override
42     public int size() {
43         int ret = size;
44         if (!haveSize) {
45             ret = calculateSize(identifier.getParent());
46             size = ret;
47             haveSize = true;
48         }
49
50         return ret;
51     }
52
53     @Override
54     @SuppressWarnings("checkstyle:parameterName")
55     public boolean contains(final Object o) {
56         final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
57         return Iterators.contains(iterator(), srch);
58     }
59
60     @Override
61     public PathArgument get(final int index) {
62         return Iterators.get(iterator(), index);
63     }
64
65     @Override
66     @SuppressWarnings("checkstyle:parameterName")
67     public int indexOf(final Object o) {
68         final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
69         return super.indexOf(srch);
70     }
71
72     @Override
73     @SuppressWarnings("checkstyle:parameterName")
74     public int lastIndexOf(final Object o) {
75         final PathArgument srch = (PathArgument) Preconditions.checkNotNull(o);
76
77         int ret = -1;
78         final Iterator<PathArgument> it = iterator();
79         for (int i = 0; it.hasNext(); ++i) {
80             if (srch.equals(it.next())) {
81                 ret = i;
82             }
83         }
84
85         return ret;
86     }
87
88     @Nonnull
89     @Override
90     public UnmodifiableIterator<PathArgument> iterator() {
91         return new IteratorImpl(identifier);
92     }
93
94     private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
95         private StackedYangInstanceIdentifier identifier;
96         private Iterator<PathArgument> tail;
97
98         IteratorImpl(final StackedYangInstanceIdentifier identifier) {
99             this.identifier = Preconditions.checkNotNull(identifier);
100         }
101
102         @Override
103         public boolean hasNext() {
104             return tail == null || tail.hasNext();
105         }
106
107         @Override
108         public PathArgument next() {
109             if (tail != null) {
110                 return tail.next();
111             }
112
113             final PathArgument ret = identifier.getLastPathArgument();
114             final YangInstanceIdentifier next = identifier.getParent();
115             final Iterable<PathArgument> args = next.tryReversePathArguments();
116             if (args != null) {
117                 tail = args.iterator();
118                 identifier = null;
119             } else {
120                 Verify.verify(next instanceof StackedYangInstanceIdentifier);
121                 identifier = (StackedYangInstanceIdentifier) next;
122             }
123
124             return ret;
125         }
126     }
127 }