groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [3/3] groovy git commit: Refactor: minor tidy up of style changes to Range classes
Date Fri, 01 Jul 2016 07:18:09 GMT
Refactor: minor tidy up of style changes to Range classes


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/e9417a4c
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/e9417a4c
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/e9417a4c

Branch: refs/heads/master
Commit: e9417a4ccd16d3038713e1b4db7a0db91d313c93
Parents: 39fad72
Author: paulk <paulk@asert.com.au>
Authored: Fri Jul 1 17:17:51 2016 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Fri Jul 1 17:17:51 2016 +1000

----------------------------------------------------------------------
 src/main/groovy/lang/IntRange.java       | 28 +++++++++++++--------------
 src/main/groovy/lang/ObjectRange.java    | 21 ++++++++++----------
 src/test/groovy/lang/EmptyRangeTest.java | 17 ++++++++--------
 3 files changed, 33 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/e9417a4c/src/main/groovy/lang/IntRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/IntRange.java b/src/main/groovy/lang/IntRange.java
index 6be21b8..574e24e 100644
--- a/src/main/groovy/lang/IntRange.java
+++ b/src/main/groovy/lang/IntRange.java
@@ -30,13 +30,13 @@ import java.util.List;
 /**
  * Represents a list of Integer objects starting at a specified {@code from} value up (or
down)
  * to and potentially including a given {@code to} value.
- * <p/>
+ * <p>
  * Instances of this class may be either inclusive aware or non-inclusive aware. See the
  * relevant constructors for creating each type. Inclusive aware IntRange instances are
  * suitable for use with Groovy's range indexing - in particular if the from or to values
  * might be negative. This normally happens underneath the covers but is worth keeping
  * in mind if creating these ranges yourself explicitly.
- * <p/>
+ * <p>
  * Note: the design of this class might seem a little strange at first. It contains a Boolean
  * field, {@code inclusive}, which can be {@code true}, {@code false} or {@code null}. This
  * design is for backwards compatibility reasons. Groovy uses this class under the covers
@@ -45,7 +45,7 @@ import java.util.List;
  * covers by the {@code new IntRange(x, y)} and {@code new IntRange(x, y-1)}. This turns
  * out to be a lossy abstraction when x and/or y are negative values. Now the latter case
  * is represented by {@code new IntRange(false, x, y)}.
- * <p/>
+ * <p>
  * Note: This class is a copy of {@link ObjectRange} optimized for <code>int</code>.
If you make any
  * changes to this class, you might consider making parallel changes to {@link ObjectRange}.
  */
@@ -123,9 +123,9 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer>
{
     /**
      * If <code>true</code> or null, <code>to</code> is included
in the range.
      * If <code>false</code>, the range stops before the <code>to</code>
value.
-     * <p/>
+     * <p>
      * Null for non-inclusive-aware ranges (which are inclusive).
-     * <p/>
+     * <p>
      * If true or false, the reverse flag is discarded.
      */
     private final Boolean inclusive;
@@ -225,10 +225,10 @@ public class IntRange extends AbstractList<Integer> implements
Range<Integer> {
      * Determines if this object is equal to another object. Delegates to
      * {@link AbstractList#equals(Object)} if <code>that</code> is anything
      * other than an {@link IntRange}.
-     * <p/>
+     * <p>
      * It is not necessary to override <code>hashCode</code>, as
      * {@link AbstractList#hashCode()} provides a suitable hash code.<p>
-     * <p/>
+     * <p>
      * Note that equals is generally handled by {@link org.codehaus.groovy.runtime.DefaultGroovyMethods#equals(List,
List)}
      * instead of this method.
      *
@@ -358,7 +358,8 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer>
{
     public boolean contains(Object value) {
         if (value instanceof Integer) {
             return (Integer) value >= getFrom() && (Integer) value <= getTo();
-        } else if (value instanceof BigInteger) {
+        }
+        if (value instanceof BigInteger) {
             final BigInteger bigint = (BigInteger) value;
             return bigint.compareTo(BigInteger.valueOf(getFrom())) >= 0 &&
                     bigint.compareTo(BigInteger.valueOf(getTo())) <= 0;
@@ -380,9 +381,8 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer>
{
         if (step == 0) {
             if (!getFrom().equals(getTo())) {
                 throw new GroovyRuntimeException("Infinite loop detected due to step size
of 0");
-            } else {
-                return; // from == to and step == 0, nothing to do, so return
             }
+            return; // from == to and step == 0, nothing to do, so return
         }
 
         if (isReverse()) {
@@ -391,8 +391,8 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer>
{
         if (step > 0) {
             int value = getFrom();
             while (value <= getTo()) {
-                closure.call(Integer.valueOf(value));
-                if ((0L + value + step) >= Integer.MAX_VALUE) {
+                closure.call(value);
+                if (((long) value + step) >= Integer.MAX_VALUE) {
                     break;
                 }
                 value = value + step;
@@ -400,8 +400,8 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer>
{
         } else {
             int value = getTo();
             while (value >= getFrom()) {
-                closure.call(Integer.valueOf(value));
-                if ((0L + value + step) <= Integer.MIN_VALUE) {
+                closure.call(value);
+                if (((long) value + step) <= Integer.MIN_VALUE) {
                     break;
                 }
                 value = value + step;

http://git-wip-us.apache.org/repos/asf/groovy/blob/e9417a4c/src/main/groovy/lang/ObjectRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/ObjectRange.java b/src/main/groovy/lang/ObjectRange.java
index 1c00c1a..1814af7 100644
--- a/src/main/groovy/lang/ObjectRange.java
+++ b/src/main/groovy/lang/ObjectRange.java
@@ -91,7 +91,8 @@ public class ObjectRange extends AbstractList implements Range {
     private ObjectRange(Comparable smaller, Comparable larger, Boolean reverse) {
         if (smaller == null) {
             throw new IllegalArgumentException("Must specify a non-null value for the 'from'
index in a Range");
-        } else if (larger == null) {
+        }
+        if (larger == null) {
             throw new IllegalArgumentException("Must specify a non-null value for the 'to'
index in a Range");
         }
         if (reverse == null) {
@@ -259,8 +260,8 @@ public class ObjectRange extends AbstractList implements Range {
     @Override
     public boolean containsWithinBounds(Object value) {
         if (value instanceof Comparable) {
-            final int result = compareTo(from, value);
-            return result == 0 || result < 0 && compareTo(to, value) >= 0;
+            final int result = compareTo(from, (Comparable) value);
+            return result == 0 || result < 0 && compareTo(to, (Comparable) value)
>= 0;
         }
         return contains(value);
     }
@@ -412,7 +413,7 @@ public class ObjectRange extends AbstractList implements Range {
     public Iterator iterator() {
         // non thread-safe iterator
         final Iterator innerIterator = new StepIterator(this, 1);
-        final Iterator safeIterator = new Iterator() {
+        return new Iterator() {
             @Override
             public synchronized boolean hasNext() {
                 return innerIterator.hasNext();
@@ -424,11 +425,10 @@ public class ObjectRange extends AbstractList implements Range {
             }
 
             @Override
-            public void remove() {
+            public synchronized void remove() {
                 innerIterator.remove();
             }
         };
-        return safeIterator;
     }
 
     /**
@@ -547,16 +547,15 @@ public class ObjectRange extends AbstractList implements Range {
     private static Comparable normaliseStringType(final Comparable operand) {
         if (operand instanceof Character) {
             return (int) (Character) operand;
-        } else if (operand instanceof String) {
+        }
+        if (operand instanceof String) {
             final String string = (String) operand;
 
             if (string.length() == 1) {
                 return (int) string.charAt(0);
-            } else {
-                return string;
             }
-        } else {
-            return operand;
+            return string;
         }
+        return operand;
     }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/e9417a4c/src/test/groovy/lang/EmptyRangeTest.java
----------------------------------------------------------------------
diff --git a/src/test/groovy/lang/EmptyRangeTest.java b/src/test/groovy/lang/EmptyRangeTest.java
index dfa5d89..e5e09d9 100644
--- a/src/test/groovy/lang/EmptyRangeTest.java
+++ b/src/test/groovy/lang/EmptyRangeTest.java
@@ -16,14 +16,15 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
-/**
- *
- */
 package groovy.lang;
 
 import groovy.util.GroovyTestCase;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.NoSuchElementException;
 
 /**
  * Provides unit tests for the {@link EmptyRange} class.
@@ -35,7 +36,7 @@ public class EmptyRangeTest extends GroovyTestCase {
     /**
      * The 'from' value for the {@link Range}.
      */
-    private static final Integer AT = new Integer(17);
+    private static final Integer AT = 17;
 
     /**
      * The {@link Range} to test.
@@ -159,7 +160,7 @@ public class EmptyRangeTest extends GroovyTestCase {
      */
     public void testAddIntObject() {
         try {
-            range.add(0, new Integer(12));
+            range.add(0, 12);
             fail("added value to empty range");
         } catch (UnsupportedOperationException e) {
             assertTrue("expected exception thrown", true);
@@ -189,7 +190,7 @@ public class EmptyRangeTest extends GroovyTestCase {
      */
     public void testAddObject() {
         try {
-            range.add(new Integer(12));
+            range.add(12);
             fail("added value to empty range");
         } catch (UnsupportedOperationException e) {
             assertTrue("expected exception thrown", true);
@@ -386,7 +387,7 @@ public class EmptyRangeTest extends GroovyTestCase {
         assertTrue("too many elements", result.isEmpty());
 
         // make sure a new list is returned each time
-        result.add(new Integer(1));
+        result.add(1);
         result = range.step(1);
         assertTrue("too many elements", result.isEmpty());
     }


Mime
View raw message