Merge branch 'master' of ../controller
[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.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 = requireNonNull(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(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) 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 }