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 com.google.common.base.Preconditions;
11 import com.google.common.collect.AbstractIterator;
12 import com.google.common.collect.ImmutableList;
13 import java.util.AbstractCollection;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.function.Consumer;
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
22 * Simple integer-to-StatementContextBase map optimized for size and restricted in scope of operations. It does not
23 * implement {@link java.util.Map} for simplicity's sake.
25 * @author Robert Varga
27 abstract class StatementMap {
28 private static final class Empty extends StatementMap {
30 StatementContextBase<?, ?, ?> get(final int index) {
35 StatementMap put(final int index, final StatementContextBase<?, ?, ?> object) {
36 return index == 0 ? new Singleton(object) : new Regular(index, object);
40 Collection<StatementContextBase<?, ?, ?>> values() {
41 return ImmutableList.of();
45 private static final class Regular extends StatementMap {
46 private StatementContextBase<?, ?, ?>[] elements;
48 Regular(final int index, final StatementContextBase<?, ?, ?> object) {
49 elements = new StatementContextBase<?, ?, ?>[index + 1];
50 elements[index] = Preconditions.checkNotNull(object);
53 Regular(final StatementContextBase<?, ?, ?> object0, final int index,
54 final StatementContextBase<?, ?, ?> object) {
55 elements = new StatementContextBase<?, ?, ?>[index + 1];
56 elements[0] = Preconditions.checkNotNull(object0);
57 elements[index] = Preconditions.checkNotNull(object);
61 StatementContextBase<?, ?, ?> get(final int index) {
62 if (index >= elements.length) {
66 return elements[index];
70 StatementMap put(final int index, final StatementContextBase<?, ?, ?> object) {
71 if (index < elements.length) {
72 Preconditions.checkArgument(elements[index] == null);
74 elements = Arrays.copyOf(elements, index + 1);
77 elements[index] = Preconditions.checkNotNull(object);
82 Collection<StatementContextBase<?, ?, ?>> values() {
83 return new RegularAsCollection<>(elements);
87 private static final class RegularAsCollection<T> extends AbstractCollection<T> {
88 private final T[] elements;
90 RegularAsCollection(final T[] elements) {
91 this.elements = Preconditions.checkNotNull(elements);
95 public void forEach(final Consumer<? super T> action) {
96 for (T e : elements) {
104 public boolean isEmpty() {
105 // This has a single-use and when it is instantiated, we know to have at least two items
110 public Iterator<T> iterator() {
111 return new AbstractIterator<T>() {
112 private int nextOffset = 0;
115 protected T computeNext() {
116 while (nextOffset < elements.length) {
117 final T ret = elements[nextOffset++];
130 // Optimized for non-sparse case
132 for (T e : elements) {
138 return elements.length - nulls;
143 private static final class Singleton extends StatementMap {
144 private final StatementContextBase<?, ?, ?> object;
146 Singleton(final StatementContextBase<?, ?, ?> object) {
147 this.object = Preconditions.checkNotNull(object);
151 StatementContextBase<?, ?, ?> get(final int index) {
152 return index == 0 ? object : null;
156 StatementMap put(final int index, final StatementContextBase<?, ?, ?> object) {
157 Preconditions.checkArgument(index != 0);
158 return new Regular(this.object, index, object);
162 Collection<StatementContextBase<?, ?, ?>> values() {
163 return ImmutableList.of(object);
167 private static final StatementMap EMPTY = new Empty();
169 static StatementMap empty() {
174 * Return the statement context at specified index.
176 * @param index Element index, must be non-negative
177 * @return Requested element or null if there is no element at that index
179 abstract @Nullable StatementContextBase<?, ?, ?> get(int index);
182 * Add a statement at specified index.
184 * @param index Element index, must be non-negative
185 * @param object Object to store
186 * @return New statement map
187 * @throws IllegalArgumentException if the index is already occupied
189 abstract @Nonnull StatementMap put(int index, @Nonnull StatementContextBase<?, ?, ?> object);
192 * Return a read-only view of the elements in this map. Unlike other maps, this view does not detect concurrent
193 * modification. Iteration is performed in order of increasing offset. In face of concurrent modification, number
194 * of elements returned through iteration may not match the size reported via {@link Collection#size()}.
196 * @return Read-only view of available statements.
198 abstract @Nonnull Collection<StatementContextBase<?, ?, ?>> values();