Do not pretty-print body class
[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 org.eclipse.jdt.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
23     private volatile int size;
24
25     StackedReversePathArguments(final StackedYangInstanceIdentifier identifier) {
26         this.identifier = requireNonNull(identifier);
27     }
28
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();
33             if (args != null) {
34                 return i + args.size();
35             }
36
37             verify(current instanceof StackedYangInstanceIdentifier);
38             current = ((StackedYangInstanceIdentifier) current).getParent();
39         }
40     }
41
42     @Override
43     public int size() {
44         int ret = size;
45         if (ret == 0) {
46             ret = calculateSize(identifier.getParent());
47             size = ret;
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) requireNonNull(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) requireNonNull(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) requireNonNull(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     @Override
89     public UnmodifiableIterator<PathArgument> iterator() {
90         return new IteratorImpl(identifier);
91     }
92
93     private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
94         private StackedYangInstanceIdentifier identifier;
95         private Iterator<PathArgument> tail;
96
97         IteratorImpl(final StackedYangInstanceIdentifier identifier) {
98             this.identifier = requireNonNull(identifier);
99         }
100
101         @Override
102         public boolean hasNext() {
103             return tail == null || tail.hasNext();
104         }
105
106         @Override
107         public PathArgument next() {
108             if (tail != null) {
109                 return tail.next();
110             }
111
112             final PathArgument ret = identifier.getLastPathArgument();
113             final YangInstanceIdentifier next = identifier.getParent();
114             final Iterable<PathArgument> args = next.tryReversePathArguments();
115             if (args != null) {
116                 tail = args.iterator();
117                 identifier = null;
118             } else {
119                 verify(next instanceof StackedYangInstanceIdentifier);
120                 identifier = (StackedYangInstanceIdentifier) next;
121             }
122
123             return ret;
124         }
125     }
126 }