changeset 6355:c30c20389172

Fixes in Nodes -- format string errors, warning cleanup, extract increaseCapacity method out of add()
author briangoetz
date Fri, 26 Oct 2012 10:57:35 -0400
parents 48a71b1bba2c
children 4e7f6a6e8369
files src/share/classes/java/util/functions/Mappers.java src/share/classes/java/util/streams/ops/Nodes.java
diffstat 2 files changed, 35 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/functions/Mappers.java	Fri Oct 26 00:26:11 2012 +0100
+++ b/src/share/classes/java/util/functions/Mappers.java	Fri Oct 26 10:57:35 2012 -0400
@@ -47,8 +47,7 @@
      * A mapper which performs a mapping from an object to it's
      * string representation.
      */
-    private static final Mapper<Object, String> STRING =
-        t -> String.valueOf(t);
+    private static final Mapper<Object, String> STRING = String::valueOf;
 
     /**
      * singleton utils
--- a/src/share/classes/java/util/streams/ops/Nodes.java	Fri Oct 26 00:26:11 2012 +0100
+++ b/src/share/classes/java/util/streams/ops/Nodes.java	Fri Oct 26 10:57:35 2012 -0400
@@ -27,7 +27,6 @@
 import java.util.*;
 import java.util.functions.Block;
 import java.util.functions.UnaryOperator;
-import java.util.logging.Logger;
 import java.util.streams.*;
 
 public class Nodes {
@@ -100,6 +99,7 @@
         final T[] array;
         int curSize;
 
+        @SuppressWarnings("unchecked")
         ArrayNode(int size) {
             this.array = (T[]) new Object[size];
             this.curSize = 0;
@@ -187,6 +187,7 @@
         }
 
         @Override
+        @SuppressWarnings("unchecked")
         public Spliterator<T> spliterator() {
             // @@@ Not very efficient
             //     This requires a general way to obtain a spliterator from a collection
@@ -202,6 +203,7 @@
         }
 
         @Override
+        @SuppressWarnings("unchecked")
         public T[] asArray() {
             return (T[]) c.toArray();
         }
@@ -486,7 +488,7 @@
         @Override
         public Node<T> build() {
             if (curSize < array.length) {
-                throw new IllegalStateException(String.format("Current size %s is less than fixed size", curSize, array.length));
+                throw new IllegalStateException(String.format("Current size %d is less than fixed size %d", curSize, array.length));
             }
 
             return this;
@@ -522,7 +524,7 @@
         @Override
         public void end() {
             if (curSize < array.length) {
-                throw new IllegalStateException(String.format("End size %s is less than fixed size", curSize, array.length));
+                throw new IllegalStateException(String.format("End size %d is less than fixed size %d", curSize, array.length));
             }
         }
 
@@ -533,9 +535,7 @@
 
         @Override
         public boolean equals(Object obj) {
-            if (!(obj instanceof NodeBuilder))
-                return false;
-            return Nodes.equals(this, (NodeBuilder) obj);
+            return (obj instanceof NodeBuilder) && Nodes.equals(this, (NodeBuilder) obj);
         }
 
         @Override
@@ -556,10 +556,12 @@
         protected static final int MIN_SPINE_SIZE = 8; // @@@ something with object allocation overhead.
         protected static final int MAX_SPINE_SIZE = (MAX_CHUNK_SIZE_POWER - MIN_CHUNK_SIZE_POWER + 1) * CHUNKS_PER_SIZE;
 
+        protected int spineIndex = 0;
+        protected int chunkIndex = 0;
+        @SuppressWarnings("unchecked")
         protected T[][] spine = (T[][])new Object[MIN_SPINE_SIZE][];
-        protected int spineIndex = 0;
+        @SuppressWarnings("unchecked")
         protected T[] chunk = spine[spineIndex] = (T[])new Object[MIN_CHUNK_SIZE];
-        protected int chunkIndex = 0;
 
         private SpinedList(int initialSize) {
             ensureCapacity(initialSize);
@@ -595,14 +597,31 @@
             return 1 << (MIN_CHUNK_SIZE_POWER + (spineIndex / CHUNKS_PER_SIZE));
         }
 
+        protected void increaseCapacity() {
+            if (spineIndex + 1 == MAX_SPINE_SIZE) {
+                throw new IllegalStateException("Unable to expand capacity");
+            }
+
+            spineIndex += 1;
+            if(spineIndex == spine.length) {
+                spine = Arrays.copyOf(spine, Math.min(spine.length + MIN_SPINE_SIZE, MAX_SPINE_SIZE));
+            }
+
+            if (null == spine[spineIndex]) {
+                @SuppressWarnings("unchecked")
+                T[] newArray = (T[]) new Object[SpinedList.sizeForSpineIndex(spineIndex)];
+                spine[spineIndex] = newArray;
+            }
+
+            chunk = spine[spineIndex];
+            chunkIndex = 0;
+        }
+
         // <editor-fold desc="Object">
 
         @Override
         public boolean equals(Object obj) {
-            if (!(obj instanceof Iterable)) {
-                return false;
-            }
-            return Nodes.equals(this, (Iterable<?>) obj);
+            return (obj instanceof Iterable) && Nodes.equals(this, (Iterable<?>) obj);
         }
 
         @Override
@@ -620,7 +639,7 @@
         }
 
         private Iterator<T> iterator(int startChunk) {
-            Iterator<T> result = new Iterator<T>() {
+            return new Iterator<T>() {
                 int currentChunk = startChunk;
                 int currentIndex = 0;
 
@@ -647,8 +666,6 @@
                     return result;
                 }
             };
-
-            return result;
         }
 
         // </editor-fold>
@@ -658,23 +675,8 @@
         @Override
         public boolean add(T t) {
             if (chunkIndex == chunk.length) {
-                if (spineIndex + 1 == MAX_SPINE_SIZE) {
-                    throw new IllegalStateException("Unable to expand capacity");
-                }
-
-                spineIndex += 1;
-                if(spineIndex == spine.length) {
-                    spine = Arrays.copyOf(spine, Math.min(spine.length + MIN_SPINE_SIZE, MAX_SPINE_SIZE));
-                }
-
-                if(null == spine[spineIndex]) {
-                    spine[spineIndex] = (T[])new Object[SpinedList.sizeForSpineIndex(spineIndex)];
-                }
-
-                chunk = spine[spineIndex];
-                chunkIndex = 0;
+                increaseCapacity();
             }
-
             chunk[chunkIndex++] = t;
 
             return true;
@@ -897,6 +899,7 @@
         @Override
         public T[] asArray() {
             assert !building : "during building";
+            @SuppressWarnings("unchecked")
             T[] result = (T[]) new Object[size()]; // will fail for size == MAX_VALUE
 
             copyInto(result, 0);