2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.AbstractIterator;
14 import com.google.common.collect.ImmutableList;
15 import java.util.AbstractCollection;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.Iterator;
19 import java.util.function.Consumer;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
24 * Simple integer-to-StatementContextBase map optimized for size and restricted in scope of operations. It does not
25 * implement {@link java.util.Map} for simplicity's sake.
27 * @author Robert Varga
29 abstract class StatementMap {
30 private static final class Empty extends StatementMap {
32 StatementContextBase<?, ?, ?> get(final int index) {
37 StatementMap put(final int index, final StatementContextBase<?, ?, ?> obj) {
38 return index == 0 ? new Singleton(obj) : new Regular(index, obj);
42 Collection<StatementContextBase<?, ?, ?>> values() {
43 return ImmutableList.of();
52 StatementMap ensureCapacity(final int expectedLimit) {
53 return expectedLimit < 2 ? this : new Regular(expectedLimit);
62 private static final class Regular extends StatementMap {
63 private StatementContextBase<?, ?, ?>[] elements;
66 Regular(final int expectedLimit) {
67 elements = new StatementContextBase<?, ?, ?>[expectedLimit];
70 Regular(final int index, final StatementContextBase<?, ?, ?> object) {
71 this(index + 1, index, object);
74 Regular(final StatementContextBase<?, ?, ?> object0, final int index,
75 final StatementContextBase<?, ?, ?> object) {
76 this(index + 1, 0, object0);
77 elements[index] = requireNonNull(object);
81 Regular(final int expectedLimit, final int index, final StatementContextBase<?, ?, ?> object) {
83 elements[index] = requireNonNull(object);
88 StatementContextBase<?, ?, ?> get(final int index) {
89 return index >= elements.length ? null : elements[index];
93 StatementMap put(final int index, final StatementContextBase<?, ?, ?> obj) {
94 if (index < elements.length) {
95 checkArgument(elements[index] == null);
97 // FIXME: detect linear growth
98 elements = Arrays.copyOf(elements, index + 1);
101 elements[index] = requireNonNull(obj);
107 Collection<StatementContextBase<?, ?, ?>> values() {
108 return new RegularAsCollection<>(elements, size);
117 StatementMap ensureCapacity(final int expectedLimit) {
118 if (elements.length < expectedLimit) {
119 elements = Arrays.copyOf(elements, expectedLimit);
126 return elements.length;
130 private static final class RegularAsCollection<T> extends AbstractCollection<T> {
131 private final T[] elements;
132 private final int size;
134 RegularAsCollection(final T[] elements, final int size) {
135 this.elements = requireNonNull(elements);
140 public void forEach(final Consumer<? super T> action) {
141 for (T e : elements) {
149 public Iterator<T> iterator() {
150 return new AbstractIterator<T>() {
151 private int nextOffset = 0;
154 protected T computeNext() {
155 while (nextOffset < elements.length) {
156 final T ret = elements[nextOffset++];
173 private static final class Singleton extends StatementMap {
174 private final StatementContextBase<?, ?, ?> object;
176 Singleton(final StatementContextBase<?, ?, ?> object) {
177 this.object = requireNonNull(object);
181 StatementContextBase<?, ?, ?> get(final int index) {
182 return index == 0 ? object : null;
186 StatementMap put(final int index, final StatementContextBase<?, ?, ?> obj) {
187 checkArgument(index != 0);
188 return new Regular(this.object, index, obj);
192 Collection<StatementContextBase<?, ?, ?>> values() {
193 return ImmutableList.of(object);
202 StatementMap ensureCapacity(final int expectedLimit) {
203 return expectedLimit < 2 ? this : new Regular(expectedLimit, 0, object);
212 private static final StatementMap EMPTY = new Empty();
214 static StatementMap empty() {
219 * Return the statement context at specified index.
221 * @param index Element index, must be non-negative
222 * @return Requested element or null if there is no element at that index
224 abstract @Nullable StatementContextBase<?, ?, ?> get(int index);
227 * Add a statement at specified index.
229 * @param index Element index, must be non-negative
230 * @param obj Object to store
231 * @return New statement map
232 * @throws IllegalArgumentException if the index is already occupied
234 abstract @Nonnull StatementMap put(int index, @Nonnull StatementContextBase<?, ?, ?> obj);
237 * Return a read-only view of the elements in this map. Unlike other maps, this view does not detect concurrent
238 * modification. Iteration is performed in order of increasing offset. In face of concurrent modification, number
239 * of elements returned through iteration may not match the size reported via {@link Collection#size()}.
241 * @return Read-only view of available statements.
243 abstract @Nonnull Collection<StatementContextBase<?, ?, ?>> values();
247 abstract StatementMap ensureCapacity(int expectedLimit);
249 abstract int capacity();