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