Use simple requireNonNull()
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementMap.java
index e50c618facb42c044832e51c18264561b54065ee..d837b93ba6e5ad9a6d494a2bf0d0491794f45345 100644 (file)
@@ -8,11 +8,11 @@
 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Verify.verify;
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.base.MoreObjects;
 import com.google.common.collect.AbstractIterator;
-import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterators;
 import java.util.AbstractCollection;
 import java.util.Arrays;
 import java.util.Collection;
@@ -25,10 +25,26 @@ import org.eclipse.jdt.annotation.Nullable;
  * Simple integer-to-StatementContextBase map optimized for size and restricted in scope of operations. It does not
  * implement {@link java.util.Map} for simplicity's sake.
  *
+ * <p>
+ * Unlike other collections, this view does not detect concurrent modification. Iteration is performed in order of
+ * increasing offset. In face of concurrent modification, number of elements returned through iteration may not match
+ * the size reported via {@link Collection#size()}.
+ *
  * @author Robert Varga
  */
-abstract class StatementMap {
+abstract class StatementMap extends AbstractCollection<AbstractResumedStatement<?, ?, ?>> {
     private static final class Empty extends StatementMap {
+        private static final Iterator<AbstractResumedStatement<?, ?, ?>> EMPTY_ITERATOR;
+
+        static {
+            // This may look weird, but we really want to return two Iterator implementations from StatementMap, so that
+            // users have to deal with bimorphic invocation. Note that we want to invoke hasNext() here, as we want to
+            // initialize state to AbstractIterator.endOfData().
+            final Iterator<AbstractResumedStatement<?, ?, ?>> it = new Regular(0).iterator();
+            verify(!it.hasNext());
+            EMPTY_ITERATOR = it;
+        }
+
         @Override
         AbstractResumedStatement<?, ?, ?> get(final int index) {
             return null;
@@ -40,12 +56,7 @@ abstract class StatementMap {
         }
 
         @Override
-        Collection<AbstractResumedStatement<?, ?, ?>> values() {
-            return ImmutableList.of();
-        }
-
-        @Override
-        int size() {
+        public int size() {
             return 0;
         }
 
@@ -55,8 +66,13 @@ abstract class StatementMap {
         }
 
         @Override
-        int capacity() {
-            return 0;
+        public void forEach(final Consumer<? super AbstractResumedStatement<?, ?, ?>> action) {
+            // No-op
+        }
+
+        @Override
+        public Iterator<AbstractResumedStatement<?, ?, ?>> iterator() {
+            return EMPTY_ITERATOR;
         }
     }
 
@@ -105,12 +121,7 @@ abstract class StatementMap {
         }
 
         @Override
-        Collection<AbstractResumedStatement<?, ?, ?>> values() {
-            return new RegularAsCollection<>(elements, size);
-        }
-
-        @Override
-        int size() {
+        public int size() {
             return size;
         }
 
@@ -123,38 +134,14 @@ abstract class StatementMap {
         }
 
         @Override
-        int capacity() {
-            return elements.length;
-        }
-    }
-
-    private static final class RegularAsCollection<T> extends AbstractCollection<T> {
-        private final T[] elements;
-        private final int size;
-
-        RegularAsCollection(final T[] elements, final int size) {
-            this.elements = requireNonNull(elements);
-            this.size = size;
-        }
-
-        @Override
-        public void forEach(final Consumer<? super T> action) {
-            for (T e : elements) {
-                if (e != null) {
-                    action.accept(e);
-                }
-            }
-        }
-
-        @Override
-        public Iterator<T> iterator() {
+        public Iterator<AbstractResumedStatement<?, ?, ?>> iterator() {
             return new AbstractIterator<>() {
                 private int nextOffset = 0;
 
                 @Override
-                protected T computeNext() {
+                protected AbstractResumedStatement<?, ?, ?> computeNext() {
                     while (nextOffset < elements.length) {
-                        final T ret = elements[nextOffset++];
+                        final AbstractResumedStatement<?, ?, ?> ret = elements[nextOffset++];
                         if (ret != null) {
                             return ret;
                         }
@@ -164,11 +151,6 @@ abstract class StatementMap {
                 }
             };
         }
-
-        @Override
-        public int size() {
-            return size;
-        }
     }
 
     private static final class Singleton extends StatementMap {
@@ -190,12 +172,7 @@ abstract class StatementMap {
         }
 
         @Override
-        Collection<AbstractResumedStatement<?, ?, ?>> values() {
-            return ImmutableList.of(object);
-        }
-
-        @Override
-        int size() {
+        public int size() {
             return 1;
         }
 
@@ -205,8 +182,8 @@ abstract class StatementMap {
         }
 
         @Override
-        int capacity() {
-            return 1;
+        public Iterator<AbstractResumedStatement<?, ?, ?>> iterator() {
+            return Iterators.singletonIterator(object);
         }
     }
 
@@ -234,23 +211,5 @@ abstract class StatementMap {
      */
     abstract @NonNull StatementMap put(int index, @NonNull AbstractResumedStatement<?, ?, ?> obj);
 
-    /**
-     * Return a read-only view of the elements in this map. Unlike other maps, this view does not detect concurrent
-     * modification. Iteration is performed in order of increasing offset. In face of concurrent modification, number
-     * of elements returned through iteration may not match the size reported via {@link Collection#size()}.
-     *
-     * @return Read-only view of available statements.
-     */
-    abstract @NonNull Collection<AbstractResumedStatement<?, ?, ?>> values();
-
-    abstract int size();
-
     abstract @NonNull StatementMap ensureCapacity(int expectedLimit);
-
-    abstract int capacity();
-
-    @Override
-    public final String toString() {
-        return MoreObjects.toStringHelper(StatementMap.class).add("values", values()).toString();
-    }
 }