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.base.Preconditions;
14 import com.google.common.collect.AbstractIterator;
15 import com.google.common.collect.ImmutableList;
16 import java.util.AbstractCollection;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.Iterator;
20 import java.util.function.Consumer;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
25 * Simple integer-to-StatementContextBase map optimized for size and restricted in scope of operations. It does not
26 * implement {@link java.util.Map} for simplicity's sake.
28 * @author Robert Varga
30 abstract class StatementMap {
31 private static final class Empty extends StatementMap {
33 StatementContextBase<?, ?, ?> get(final int index) {
38 StatementMap put(final int index, final StatementContextBase<?, ?, ?> obj) {
39 return index == 0 ? new Singleton(obj) : new Regular(index, obj);
43 Collection<StatementContextBase<?, ?, ?>> values() {
44 return ImmutableList.of();
53 StatementMap ensureCapacity(final int expectedLimit) {
54 return expectedLimit < 2 ? this : new Regular(expectedLimit);
63 private static final class Regular extends StatementMap {
64 private StatementContextBase<?, ?, ?>[] elements;
67 Regular(final int expectedLimit) {
68 elements = new StatementContextBase<?, ?, ?>[expectedLimit];
71 Regular(final int index, final StatementContextBase<?, ?, ?> object) {
72 this(index + 1, index, object);
75 Regular(final StatementContextBase<?, ?, ?> object0, final int index,
76 final StatementContextBase<?, ?, ?> object) {
77 this(index + 1, 0, object0);
78 elements[index] = requireNonNull(object);
82 Regular(final int expectedLimit, final int index, final StatementContextBase<?, ?, ?> object) {
84 elements[index] = requireNonNull(object);
89 StatementContextBase<?, ?, ?> get(final int index) {
90 if (index >= elements.length) {
94 return elements[index];
98 StatementMap put(final int index, final StatementContextBase<?, ?, ?> obj) {
99 if (index < elements.length) {
100 checkArgument(elements[index] == null);
102 // FIXME: detect linear growth
103 elements = Arrays.copyOf(elements, index + 1);
106 elements[index] = requireNonNull(obj);
112 Collection<StatementContextBase<?, ?, ?>> values() {
113 return new RegularAsCollection<>(elements);
122 StatementMap ensureCapacity(final int expectedLimit) {
123 if (elements.length < expectedLimit) {
124 elements = Arrays.copyOf(elements, expectedLimit);
131 return elements.length;
135 private static final class RegularAsCollection<T> extends AbstractCollection<T> {
136 private final T[] elements;
139 RegularAsCollection(final T[] elements) {
140 this.elements = Preconditions.checkNotNull(elements);
144 public void forEach(final Consumer<? super T> action) {
145 for (T e : elements) {
153 public boolean isEmpty() {
158 public Iterator<T> iterator() {
159 return new AbstractIterator<T>() {
160 private int nextOffset = 0;
163 protected T computeNext() {
164 while (nextOffset < elements.length) {
165 final T ret = elements[nextOffset++];
182 private static final class Singleton extends StatementMap {
183 private final StatementContextBase<?, ?, ?> object;
185 Singleton(final StatementContextBase<?, ?, ?> object) {
186 this.object = requireNonNull(object);
190 StatementContextBase<?, ?, ?> get(final int index) {
191 return index == 0 ? object : null;
195 StatementMap put(final int index, final StatementContextBase<?, ?, ?> obj) {
196 checkArgument(index != 0);
197 return new Regular(this.object, index, obj);
201 Collection<StatementContextBase<?, ?, ?>> values() {
202 return ImmutableList.of(object);
211 StatementMap ensureCapacity(final int expectedLimit) {
212 return expectedLimit < 2 ? this : new Regular(expectedLimit, 0, object);
221 private static final StatementMap EMPTY = new Empty();
223 static StatementMap empty() {
228 * Return the statement context at specified index.
230 * @param index Element index, must be non-negative
231 * @return Requested element or null if there is no element at that index
233 abstract @Nullable StatementContextBase<?, ?, ?> get(int index);
236 * Add a statement at specified index.
238 * @param index Element index, must be non-negative
239 * @param obj Object to store
240 * @return New statement map
241 * @throws IllegalArgumentException if the index is already occupied
243 abstract @Nonnull StatementMap put(int index, @Nonnull StatementContextBase<?, ?, ?> obj);
246 * Return a read-only view of the elements in this map. Unlike other maps, this view does not detect concurrent
247 * modification. Iteration is performed in order of increasing offset. In face of concurrent modification, number
248 * of elements returned through iteration may not match the size reported via {@link Collection#size()}.
250 * @return Read-only view of available statements.
252 abstract @Nonnull Collection<StatementContextBase<?, ?, ?>> values();
256 abstract StatementMap ensureCapacity(int expectedLimit);
258 abstract int capacity();