You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2016/07/01 07:48:30 UTC
[1/3] groovy git commit: Refactor: unified the style of Range related
classes (port to 2_4_X)
Repository: groovy
Updated Branches:
refs/heads/GROOVY_2_4_X c5c2420ca -> 540595e39
Refactor: unified the style of Range related classes (port to 2_4_X)
* made local variables final
* removed non-contructor related `this` prefixes
* added braces to for/if/while
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/9e378044
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/9e378044
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/9e378044
Branch: refs/heads/GROOVY_2_4_X
Commit: 9e37804488247d4748e99b18b5c2cc1d541c145f
Parents: c5c2420
Author: Pap L\u0151rinc <pa...@yahoo.com>
Authored: Thu Jun 30 01:37:57 2016 +0300
Committer: paulk <pa...@asert.com.au>
Committed: Fri Jul 1 17:38:36 2016 +1000
----------------------------------------------------------------------
src/main/groovy/lang/EmptyRange.java | 11 +++--
src/main/groovy/lang/IntRange.java | 31 ++++++++------
src/main/groovy/lang/ObjectRange.java | 59 +++++++++++++++------------
src/main/groovy/lang/Range.java | 23 +++++++++--
src/test/groovy/lang/EmptyRangeTest.java | 55 +++++++++----------------
5 files changed, 97 insertions(+), 82 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/9e378044/src/main/groovy/lang/EmptyRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/EmptyRange.java b/src/main/groovy/lang/EmptyRange.java
index 1680809..df177e6 100644
--- a/src/main/groovy/lang/EmptyRange.java
+++ b/src/main/groovy/lang/EmptyRange.java
@@ -20,7 +20,10 @@ package groovy.lang;
import org.codehaus.groovy.runtime.InvokerHelper;
-import java.util.*;
+import java.util.AbstractList;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
/**
* Constructing Ranges like 0..<0
@@ -86,9 +89,9 @@ public class EmptyRange extends AbstractList implements Range {
* {@inheritDoc}
*/
public String toString() {
- return (null == at)
- ? "null..<null"
- : at.toString() + "..<" + at.toString();
+ return (null == at)
+ ? "null..<null"
+ : at + "..<" + at;
}
/**
http://git-wip-us.apache.org/repos/asf/groovy/blob/9e378044/src/main/groovy/lang/IntRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/IntRange.java b/src/main/groovy/lang/IntRange.java
index fbb8bc9..2e7b05c 100644
--- a/src/main/groovy/lang/IntRange.java
+++ b/src/main/groovy/lang/IntRange.java
@@ -175,7 +175,7 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
private void checkSize() {
// size() in the Collection interface returns an integer, so ranges can have no more than Integer.MAX_VALUE elements
- Long size = (long) this.to - this.from + 1;
+ final Long size = (long) to - from + 1;
if (size > Integer.MAX_VALUE) {
throw new IllegalArgumentException("A range must have no more than " + Integer.MAX_VALUE + " elements but attempted " + size + " elements");
}
@@ -189,7 +189,9 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
* @return the calculated range information (with 1 added to the to value, ready for providing to subList
*/
public RangeInfo subListBorders(int size) {
- if (inclusive == null) throw new IllegalStateException("Should not call subListBorders on a non-inclusive aware IntRange");
+ if (inclusive == null) {
+ throw new IllegalStateException("Should not call subListBorders on a non-inclusive aware IntRange");
+ }
int tempFrom = from;
if (tempFrom < 0) {
tempFrom += size;
@@ -229,18 +231,24 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
* @return <code>true</code> if the ranges are equal
*/
public boolean equals(IntRange that) {
- return that != null && ((this.inclusive == null && this.reverse == that.reverse && this.from == that.from && this.to == that.to)
- || (this.inclusive != null && this.inclusive == that.inclusive && this.from == that.from && this.to == that.to));
+ return that != null && ((inclusive == null && reverse == that.reverse && from == that.from && to == that.to)
+ || (inclusive != null && inclusive == that.inclusive && from == that.from && to == that.to));
}
public Integer getFrom() {
- if (inclusive == null || from <= to) return from;
+ if (inclusive == null || from <= to) {
+ return from;
+ }
return inclusive ? to : to + 1;
}
public Integer getTo() {
- if (inclusive == null) return to;
- if (from <= to) return inclusive ? to : to - 1;
+ if (inclusive == null) {
+ return to;
+ }
+ if (from <= to) {
+ return inclusive ? to : to - 1;
+ }
return from;
}
@@ -325,9 +333,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();
- }
- if (value instanceof BigInteger) {
- BigInteger bigint = (BigInteger) value;
+ } else if (value instanceof BigInteger) {
+ final BigInteger bigint = (BigInteger) value;
return bigint.compareTo(BigInteger.valueOf(getFrom())) >= 0 &&
bigint.compareTo(BigInteger.valueOf(getTo())) <= 0;
}
@@ -337,7 +344,7 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
public boolean containsAll(Collection other) {
if (other instanceof IntRange) {
final IntRange range = (IntRange) other;
- return this.getFrom() <= range.getFrom() && range.getTo() <= this.getTo();
+ return getFrom() <= range.getFrom() && range.getTo() <= getTo();
}
return super.containsAll(other);
}
@@ -376,7 +383,7 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
}
public List<Integer> step(int step) {
- IteratorClosureAdapter<Integer> adapter = new IteratorClosureAdapter<Integer>(this);
+ final IteratorClosureAdapter<Integer> adapter = new IteratorClosureAdapter<Integer>(this);
step(step, adapter);
return adapter.asList();
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/9e378044/src/main/groovy/lang/ObjectRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/ObjectRange.java b/src/main/groovy/lang/ObjectRange.java
index c008408..688f877 100644
--- a/src/main/groovy/lang/ObjectRange.java
+++ b/src/main/groovy/lang/ObjectRange.java
@@ -18,10 +18,10 @@
*/
package groovy.lang;
+import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.IteratorClosureAdapter;
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;
-import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
import java.math.BigDecimal;
@@ -34,7 +34,7 @@ import java.util.List;
* Represents an inclusive list of objects from a value to a value using
* comparators.
* <p>
- * This class is similar to {@link IntRange}. If you make any changes to this
+ * Note: This class is similar to {@link IntRange}. If you make any changes to this
* class, you might consider making parallel changes to {@link IntRange}.
*
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
@@ -123,15 +123,17 @@ public class ObjectRange extends AbstractList implements Range {
if (from instanceof String || to instanceof String) {
// this test depends deeply on the String.next implementation
// 009.next is 00:, not 010
- String start = from.toString();
- String end = to.toString();
+ final String start = from.toString();
+ final String end = to.toString();
if (start.length() > end.length()) {
throw new IllegalArgumentException("Incompatible Strings for Range: starting String is longer than ending string");
}
- int length = Math.min(start.length(), end.length());
+ final int length = Math.min(start.length(), end.length());
int i;
for (i = 0; i < length; i++) {
- if (start.charAt(i) != end.charAt(i)) break;
+ if (start.charAt(i) != end.charAt(i)) {
+ break;
+ }
}
if (i < length - 1) {
throw new IllegalArgumentException("Incompatible Strings for Range: String#next() will not reach the expected value");
@@ -155,9 +157,9 @@ public class ObjectRange extends AbstractList implements Range {
*/
public boolean equals(ObjectRange that) {
return that != null
- && this.reverse == that.reverse
- && DefaultTypeTransformation.compareEqual(this.from, that.from)
- && DefaultTypeTransformation.compareEqual(this.to, that.to);
+ && reverse == that.reverse
+ && DefaultTypeTransformation.compareEqual(from, that.from)
+ && DefaultTypeTransformation.compareEqual(to, that.to);
}
/**
@@ -248,7 +250,7 @@ public class ObjectRange extends AbstractList implements Range {
*/
public boolean containsWithinBounds(Object value) {
if (value instanceof Comparable) {
- int result = compareTo(from, (Comparable) value);
+ final int result = compareTo(from, (Comparable) value);
return result == 0 || result < 0 && compareTo(to, (Comparable) value) >= 0;
}
return contains(value);
@@ -266,20 +268,20 @@ public class ObjectRange extends AbstractList implements Range {
if ((from instanceof Integer || from instanceof Long)
&& (to instanceof Integer || to instanceof Long)) {
// let's fast calculate the size
- long fromNum = ((Number) from).longValue();
- long toNum = ((Number) to).longValue();
+ final long fromNum = ((Number) from).longValue();
+ final long toNum = ((Number) to).longValue();
size = (int) (toNum - fromNum + 1);
} else if (from instanceof Character && to instanceof Character) {
// let's fast calculate the size
- char fromNum = (Character) from;
- char toNum = (Character) to;
+ final char fromNum = (Character) from;
+ final char toNum = (Character) to;
size = toNum - fromNum + 1;
} else if (from instanceof BigDecimal || to instanceof BigDecimal ||
from instanceof BigInteger || to instanceof BigInteger) {
// let's fast calculate the size
- BigDecimal fromNum = new BigDecimal(from.toString());
- BigDecimal toNum = new BigDecimal(to.toString());
- BigInteger sizeNum = toNum.subtract(fromNum).add(new BigDecimal(1.0)).toBigInteger();
+ final BigDecimal fromNum = new BigDecimal(from.toString());
+ final BigDecimal toNum = new BigDecimal(to.toString());
+ final BigInteger sizeNum = toNum.subtract(fromNum).add(new BigDecimal(1.0)).toBigInteger();
size = sizeNum.intValue();
} else {
// let's lazily calculate the size
@@ -327,14 +329,16 @@ public class ObjectRange extends AbstractList implements Range {
* {@inheritDoc}
*/
public String inspect() {
- String toText = InvokerHelper.inspect(to);
- String fromText = InvokerHelper.inspect(from);
+ final String toText = InvokerHelper.inspect(to);
+ final String fromText = InvokerHelper.inspect(from);
return reverse ? "" + toText + ".." + fromText : "" + fromText + ".." + toText;
}
public boolean contains(Object value) {
- Iterator it = iterator();
- if (value == null) return false;
+ final Iterator it = iterator();
+ if (value == null) {
+ return false;
+ }
while (it.hasNext()) {
try {
if (DefaultTypeTransformation.compareEqual(value, it.next())) return true;
@@ -361,7 +365,7 @@ public class ObjectRange extends AbstractList implements Range {
step = -step;
}
if (step > 0) {
- Comparable first = from;
+ final Comparable first = from;
Comparable value = from;
while (compareTo(value, to) <= 0) {
closure.call(value);
@@ -372,7 +376,7 @@ public class ObjectRange extends AbstractList implements Range {
}
} else {
step = -step;
- Comparable first = to;
+ final Comparable first = to;
Comparable value = to;
while (compareTo(value, from) >= 0) {
closure.call(value);
@@ -388,7 +392,7 @@ public class ObjectRange extends AbstractList implements Range {
* {@inheritDoc}
*/
public List step(int step) {
- IteratorClosureAdapter adapter = new IteratorClosureAdapter(this);
+ final IteratorClosureAdapter adapter = new IteratorClosureAdapter(this);
step(step, adapter);
return adapter.asList();
}
@@ -415,14 +419,15 @@ public class ObjectRange extends AbstractList implements Range {
private static Comparable normaliseStringType(final Comparable operand) {
if (operand instanceof Character) {
- return (int) ((Character) operand).charValue();
+ return (int) (Character) operand;
} else if (operand instanceof String) {
final String string = (String) operand;
- if (string.length() == 1)
+ if (string.length() == 1) {
return (int) string.charAt(0);
- else
+ } else {
return string;
+ }
} else {
return operand;
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/9e378044/src/main/groovy/lang/Range.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/Range.java b/src/main/groovy/lang/Range.java
index e672b93..e12f2c2 100644
--- a/src/main/groovy/lang/Range.java
+++ b/src/main/groovy/lang/Range.java
@@ -23,10 +23,21 @@ import java.util.List;
/**
* A Range represents the list of all items obtained by starting from a
* <code>from</code> value and calling <code>next()</code> successively
+<<<<<<< HEAD
* until you reach the <code>to</code> value. For a reverse range,
* the list is obtained by starting at the <code>to</code> value and
* successively calling <code>previous()</code> until the <code>from</code>
* value is reached.
+=======
+ * until another call to <code>next()</code> would be higher than the <code>to</code> value.
+ * For a reverse range, the list is obtained by starting at the <code>to</code> value and
+ * successively calling <code>previous()</code> until another call to
+ * <code>previous()</code> would be lower than the <code>from</code>value.
+ * <p>
+ * This means in odd cases the second boundary may not be contained in the range,
+ * and a..b may produce a different set of elements than (b..a).reversed().
+ * E.g. 1..2.5 == [1, 2]; but 2.5..1 == [2.5, 1.5]
+>>>>>>> a8413d3... Refactor: unified the style of Range related classes
*
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
*/
@@ -56,6 +67,12 @@ public interface Range<T extends Comparable> extends List<T> {
/**
* Indicates whether an object is greater than or equal to the <code>from</code>
* value for the range and less than or equal to the <code>to</code> value.
+ * <p>
+ * This may be true even for values not contained in the range.
+ * <p>
+ * Example: from = 1.5 , to = 3, next() increments by 1
+ * containsWithinBounds(2) == true
+ * contains(2) == false
*
* @param o the object to check against the boundaries of the range
* @return <code>true</code> if the object is between the from and to values
@@ -76,14 +93,12 @@ public interface Range<T extends Comparable> extends List<T> {
*
* @param step the amount by which to step. If negative, steps through the
* range backwards.
- * @return the list formed by stepping through the range by the indicated
- * interval.
+ * @return the list formed by stepping through the range by the indicated interval.
*/
List<T> step(int step);
/**
- * @return the verbose {@link String} representation of this {@link Range} as would be typed into a console
- * to create the {@link Range} instance
+ * @return the verbose {@link String} representation of this {@link Range} as would be typed into a console to create the {@link Range} instance
*/
String inspect();
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/9e378044/src/test/groovy/lang/EmptyRangeTest.java
----------------------------------------------------------------------
diff --git a/src/test/groovy/lang/EmptyRangeTest.java b/src/test/groovy/lang/EmptyRangeTest.java
index 4290c14..dfa5d89 100644
--- a/src/test/groovy/lang/EmptyRangeTest.java
+++ b/src/test/groovy/lang/EmptyRangeTest.java
@@ -137,8 +137,7 @@ public class EmptyRangeTest extends GroovyTestCase {
try {
range.get(0);
fail("got value from empty range");
- }
- catch (IndexOutOfBoundsException e) {
+ } catch (IndexOutOfBoundsException e) {
assertTrue("expected exception thrown", true);
}
}
@@ -150,21 +149,19 @@ public class EmptyRangeTest extends GroovyTestCase {
try {
range.remove(0);
fail("removed value from empty range");
- }
- catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException e) {
assertTrue("expected exception thrown", true);
}
}
/**
- * Test method for {@link groovy.lang.EmptyRange#add(int,java.lang.Object)}.
+ * Test method for {@link groovy.lang.EmptyRange#add(int, java.lang.Object)}.
*/
public void testAddIntObject() {
try {
range.add(0, new Integer(12));
fail("added value to empty range");
- }
- catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException e) {
assertTrue("expected exception thrown", true);
}
}
@@ -194,8 +191,7 @@ public class EmptyRangeTest extends GroovyTestCase {
try {
range.add(new Integer(12));
fail("added value to empty range");
- }
- catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException e) {
assertTrue("expected exception thrown", true);
}
}
@@ -214,21 +210,19 @@ public class EmptyRangeTest extends GroovyTestCase {
try {
range.remove(AT);
fail("removed value from empty range");
- }
- catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException e) {
assertTrue("expected exception thrown", true);
}
}
/**
- * Test method for {@link groovy.lang.EmptyRange#addAll(int,java.util.Collection)}.
+ * Test method for {@link groovy.lang.EmptyRange#addAll(int, java.util.Collection)}.
*/
public void testAddAllIntCollection() {
try {
range.addAll(0, new ArrayList());
fail("added values to empty range");
- }
- catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException e) {
assertTrue("expected exception thrown", true);
}
}
@@ -240,8 +234,7 @@ public class EmptyRangeTest extends GroovyTestCase {
try {
range.addAll(new ArrayList());
fail("added values to empty range");
- }
- catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException e) {
assertTrue("expected exception thrown", true);
}
}
@@ -264,8 +257,7 @@ public class EmptyRangeTest extends GroovyTestCase {
try {
range.removeAll(new ArrayList());
fail("removed values from an empty range");
- }
- catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException e) {
assertTrue("expected exception thrown", true);
}
}
@@ -277,8 +269,7 @@ public class EmptyRangeTest extends GroovyTestCase {
try {
range.retainAll(new ArrayList());
fail("retained values in an empty range");
- }
- catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException e) {
assertTrue("expected exception thrown", true);
}
}
@@ -293,8 +284,7 @@ public class EmptyRangeTest extends GroovyTestCase {
try {
iterator.next();
fail("got next value in an empty range");
- }
- catch (NoSuchElementException e) {
+ } catch (NoSuchElementException e) {
assertTrue("expected exception thrown", true);
}
}
@@ -307,14 +297,13 @@ public class EmptyRangeTest extends GroovyTestCase {
final Iterator iterator = range.iterator();
iterator.remove();
fail("removed via iterator");
- }
- catch (IllegalStateException e) {
+ } catch (IllegalStateException e) {
assertTrue("expected exception thrown", true);
}
}
/**
- * Test method for {@link groovy.lang.EmptyRange#subList(int,int)}.
+ * Test method for {@link groovy.lang.EmptyRange#subList(int, int)}.
*/
public void testSubList() {
final List list = range.subList(0, 0);
@@ -322,8 +311,7 @@ public class EmptyRangeTest extends GroovyTestCase {
try {
range.subList(0, 1);
fail("got sub list in an empty range");
- }
- catch (IndexOutOfBoundsException e) {
+ } catch (IndexOutOfBoundsException e) {
assertTrue("expected exception thrown", true);
}
}
@@ -339,8 +327,7 @@ public class EmptyRangeTest extends GroovyTestCase {
try {
iterator.next();
fail("got next value in an empty range");
- }
- catch (NoSuchElementException e) {
+ } catch (NoSuchElementException e) {
assertTrue("expected exception thrown", true);
}
}
@@ -356,21 +343,19 @@ public class EmptyRangeTest extends GroovyTestCase {
try {
range.listIterator(1);
fail("got list iterator at index 1");
- }
- catch (IndexOutOfBoundsException e) {
+ } catch (IndexOutOfBoundsException e) {
assertTrue("expected exception thrown", true);
}
}
/**
- * Test method for {@link groovy.lang.EmptyRange#set(int,java.lang.Object)}.
+ * Test method for {@link groovy.lang.EmptyRange#set(int, java.lang.Object)}.
*/
public void testSet() {
try {
range.set(0, AT);
fail("got set value 0");
- }
- catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException e) {
assertTrue("expected exception thrown", true);
}
}
@@ -384,7 +369,7 @@ public class EmptyRangeTest extends GroovyTestCase {
}
/**
- * Test method for {@link groovy.lang.EmptyRange#step(int,groovy.lang.Closure)}.
+ * Test method for {@link groovy.lang.EmptyRange#step(int, groovy.lang.Closure)}.
*/
public void testStepIntClosure() {
final List callLog = new ArrayList();
[2/3] groovy git commit: Refactor: added missing @Override
annotations for the Range classes (port to 2_4_X)
Posted by pa...@apache.org.
Refactor: added missing @Override annotations for the Range classes (port to 2_4_X)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/f87a5e9d
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f87a5e9d
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f87a5e9d
Branch: refs/heads/GROOVY_2_4_X
Commit: f87a5e9d4788cc41ef64932fe71341b38492311b
Parents: 9e37804
Author: Pap L\u0151rinc <pa...@yahoo.com>
Authored: Thu Jun 30 01:52:12 2016 +0300
Committer: paulk <pa...@asert.com.au>
Committed: Fri Jul 1 17:42:20 2016 +1000
----------------------------------------------------------------------
src/main/groovy/lang/EmptyRange.java | 17 +++++++++++++++++
src/main/groovy/lang/IntRange.java | 16 ++++++++++++++++
src/main/groovy/lang/ObjectRange.java | 15 +++++++++++++++
3 files changed, 48 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/f87a5e9d/src/main/groovy/lang/EmptyRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/EmptyRange.java b/src/main/groovy/lang/EmptyRange.java
index df177e6..8f74a7e 100644
--- a/src/main/groovy/lang/EmptyRange.java
+++ b/src/main/groovy/lang/EmptyRange.java
@@ -49,6 +49,7 @@ public class EmptyRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public Comparable getFrom() {
return at;
}
@@ -56,6 +57,7 @@ public class EmptyRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public Comparable getTo() {
return at;
}
@@ -65,6 +67,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @return <code>false</code>
*/
+ @Override
public boolean isReverse() {
return false;
}
@@ -74,6 +77,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @return <code>false</code>
*/
+ @Override
public boolean containsWithinBounds(Object o) {
return false;
}
@@ -81,6 +85,7 @@ public class EmptyRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public String inspect() {
return InvokerHelper.inspect(at) + "..<" + InvokerHelper.inspect(at);
}
@@ -99,6 +104,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @return 0
*/
+ @Override
public int size() {
return 0;
}
@@ -108,6 +114,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @throws IndexOutOfBoundsException always
*/
+ @Override
public Object get(int index) {
throw new IndexOutOfBoundsException("can't get values from Empty Ranges");
}
@@ -117,6 +124,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @throws UnsupportedOperationException always
*/
+ @Override
public boolean add(Object o) {
throw new UnsupportedOperationException("cannot add to Empty Ranges");
}
@@ -126,6 +134,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @throws UnsupportedOperationException
*/
+ @Override
public boolean addAll(int index, Collection c) {
throw new UnsupportedOperationException("cannot add to Empty Ranges");
}
@@ -135,6 +144,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @throws UnsupportedOperationException
*/
+ @Override
public boolean addAll(Collection c) {
throw new UnsupportedOperationException("cannot add to Empty Ranges");
}
@@ -144,6 +154,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @throws UnsupportedOperationException
*/
+ @Override
public boolean remove(Object o) {
throw new UnsupportedOperationException("cannot remove from Empty Ranges");
}
@@ -153,6 +164,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @throws UnsupportedOperationException
*/
+ @Override
public Object remove(int index) {
throw new UnsupportedOperationException("cannot remove from Empty Ranges");
}
@@ -162,6 +174,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @throws UnsupportedOperationException
*/
+ @Override
public boolean removeAll(Collection c) {
throw new UnsupportedOperationException("cannot remove from Empty Ranges");
}
@@ -171,6 +184,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @throws UnsupportedOperationException
*/
+ @Override
public boolean retainAll(Collection c) {
throw new UnsupportedOperationException("cannot retainAll in Empty Ranges");
}
@@ -180,6 +194,7 @@ public class EmptyRange extends AbstractList implements Range {
*
* @throws UnsupportedOperationException
*/
+ @Override
public Object set(int index, Object element) {
throw new UnsupportedOperationException("cannot set in Empty Ranges");
}
@@ -187,12 +202,14 @@ public class EmptyRange extends AbstractList implements Range {
/**
* Always does nothing for an empty range.
*/
+ @Override
public void step(int step, Closure closure) {
}
/**
* Always returns an empty list for an empty range.
*/
+ @Override
public List step(int step) {
return new ArrayList();
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/f87a5e9d/src/main/groovy/lang/IntRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/IntRange.java b/src/main/groovy/lang/IntRange.java
index 2e7b05c..bd8a768 100644
--- a/src/main/groovy/lang/IntRange.java
+++ b/src/main/groovy/lang/IntRange.java
@@ -62,10 +62,12 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
*/
private int value = isReverse() ? getTo() : getFrom();
+ @Override
public boolean hasNext() {
return index < size;
}
+ @Override
public Integer next() {
if (!hasNext()) {
// TODO instead of returning null, do this: throw new NoSuchElementException();
@@ -87,6 +89,7 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
* @throws java.lang.UnsupportedOperationException
* always
*/
+ @Override
public void remove() {
IntRange.this.remove(index);
}
@@ -235,6 +238,7 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
|| (inclusive != null && inclusive == that.inclusive && from == that.from && to == that.to));
}
+ @Override
public Integer getFrom() {
if (inclusive == null || from <= to) {
return from;
@@ -242,6 +246,7 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
return inclusive ? to : to + 1;
}
+ @Override
public Integer getTo() {
if (inclusive == null) {
return to;
@@ -277,14 +282,17 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
return getTo();
}
+ @Override
public boolean isReverse() {
return inclusive == null ? reverse : (from > to);
}
+ @Override
public boolean containsWithinBounds(Object o) {
return contains(o);
}
+ @Override
public Integer get(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException("Index: " + index + " should not be negative");
@@ -295,14 +303,17 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
return isReverse() ? getTo() - index : index + getFrom();
}
+ @Override
public int size() {
return getTo() - getFrom() + 1;
}
+ @Override
public Iterator<Integer> iterator() {
return new IntRangeIterator();
}
+ @Override
public List<Integer> subList(int fromIndex, int toIndex) {
if (fromIndex < 0) {
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
@@ -326,10 +337,12 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
: (reverse ? "" + to + ".." + from : "" + from + ".." + to);
}
+ @Override
public String inspect() {
return toString();
}
+ @Override
public boolean contains(Object value) {
if (value instanceof Integer) {
return (Integer) value >= getFrom() && (Integer) value <= getTo();
@@ -341,6 +354,7 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
return false;
}
+ @Override
public boolean containsAll(Collection other) {
if (other instanceof IntRange) {
final IntRange range = (IntRange) other;
@@ -349,6 +363,7 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
return super.containsAll(other);
}
+ @Override
public void step(int step, Closure closure) {
if (step == 0) {
if (!getFrom().equals(getTo())) {
@@ -382,6 +397,7 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
}
}
+ @Override
public List<Integer> step(int step) {
final IteratorClosureAdapter<Integer> adapter = new IteratorClosureAdapter<Integer>(this);
step(step, adapter);
http://git-wip-us.apache.org/repos/asf/groovy/blob/f87a5e9d/src/main/groovy/lang/ObjectRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/ObjectRange.java b/src/main/groovy/lang/ObjectRange.java
index 688f877..9a1548f 100644
--- a/src/main/groovy/lang/ObjectRange.java
+++ b/src/main/groovy/lang/ObjectRange.java
@@ -165,6 +165,7 @@ public class ObjectRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public Comparable getFrom() {
return from;
}
@@ -172,6 +173,7 @@ public class ObjectRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public Comparable getTo() {
return to;
}
@@ -179,6 +181,7 @@ public class ObjectRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public boolean isReverse() {
return reverse;
}
@@ -186,6 +189,7 @@ public class ObjectRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public Object get(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException("Index: " + index + " should not be negative");
@@ -248,6 +252,7 @@ public class ObjectRange extends AbstractList implements Range {
* @param value the value of interest
* @return true if the value is within the bounds
*/
+ @Override
public boolean containsWithinBounds(Object value) {
if (value instanceof Comparable) {
final int result = compareTo(from, (Comparable) value);
@@ -263,6 +268,7 @@ public class ObjectRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public int size() {
if (size == -1) {
if ((from instanceof Integer || from instanceof Long)
@@ -301,6 +307,7 @@ public class ObjectRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public List subList(int fromIndex, int toIndex) {
if (fromIndex < 0) {
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
@@ -328,12 +335,18 @@ public class ObjectRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public String inspect() {
final String toText = InvokerHelper.inspect(to);
final String fromText = InvokerHelper.inspect(from);
return reverse ? "" + toText + ".." + fromText : "" + fromText + ".." + toText;
}
+ /**
+ * iterates over all values and returns true if one value matches.
+ * Also see containsWithinBounds.
+ */
+ @Override
public boolean contains(Object value) {
final Iterator it = iterator();
if (value == null) {
@@ -352,6 +365,7 @@ public class ObjectRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public void step(int step, Closure closure) {
if (step == 0) {
if (compareTo(from, to) != 0) {
@@ -391,6 +405,7 @@ public class ObjectRange extends AbstractList implements Range {
/**
* {@inheritDoc}
*/
+ @Override
public List step(int step) {
final IteratorClosureAdapter adapter = new IteratorClosureAdapter(this);
step(step, adapter);
[3/3] groovy git commit: Refactor: minor tidy up of style changes to
Range classes (port to 2_4_X)
Posted by pa...@apache.org.
Refactor: minor tidy up of style changes to Range classes (port to 2_4_X)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/540595e3
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/540595e3
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/540595e3
Branch: refs/heads/GROOVY_2_4_X
Commit: 540595e39b3c4da7a6e6a98815da409eed452707
Parents: f87a5e9
Author: paulk <pa...@asert.com.au>
Authored: Fri Jul 1 17:17:51 2016 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Fri Jul 1 17:47:48 2016 +1000
----------------------------------------------------------------------
src/main/groovy/lang/EmptyRange.java | 2 --
src/main/groovy/lang/IntRange.java | 16 +++++++---------
src/main/groovy/lang/ObjectRange.java | 15 ++++++---------
src/main/groovy/lang/Range.java | 13 -------------
src/test/groovy/lang/EmptyRangeTest.java | 17 +++++++++--------
5 files changed, 22 insertions(+), 41 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/540595e3/src/main/groovy/lang/EmptyRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/EmptyRange.java b/src/main/groovy/lang/EmptyRange.java
index 8f74a7e..705ff5a 100644
--- a/src/main/groovy/lang/EmptyRange.java
+++ b/src/main/groovy/lang/EmptyRange.java
@@ -27,8 +27,6 @@ import java.util.List;
/**
* Constructing Ranges like 0..<0
- * @author Dierk Koenig
- * @author Edwin Tellman
*/
public class EmptyRange extends AbstractList implements Range {
http://git-wip-us.apache.org/repos/asf/groovy/blob/540595e3/src/main/groovy/lang/IntRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/IntRange.java b/src/main/groovy/lang/IntRange.java
index bd8a768..60a4c14 100644
--- a/src/main/groovy/lang/IntRange.java
+++ b/src/main/groovy/lang/IntRange.java
@@ -38,8 +38,6 @@ import java.util.List;
* 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.
- *
- * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
*/
public class IntRange extends AbstractList<Integer> implements Range<Integer> {
@@ -346,7 +344,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;
@@ -368,9 +367,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()) {
@@ -379,8 +377,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;
@@ -388,8 +386,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/540595e3/src/main/groovy/lang/ObjectRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/ObjectRange.java b/src/main/groovy/lang/ObjectRange.java
index 9a1548f..71839eb 100644
--- a/src/main/groovy/lang/ObjectRange.java
+++ b/src/main/groovy/lang/ObjectRange.java
@@ -36,8 +36,6 @@ import java.util.List;
* <p>
* Note: This class is similar to {@link IntRange}. If you make any changes to this
* class, you might consider making parallel changes to {@link IntRange}.
- *
- * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
*/
public class ObjectRange extends AbstractList implements Range {
@@ -107,9 +105,9 @@ public class ObjectRange extends AbstractList implements Range {
}
if (from instanceof Integer && to instanceof Long) {
- from = Long.valueOf(((Integer) from).longValue());
+ from = ((Integer) from).longValue();
} else if (to instanceof Integer && from instanceof Long) {
- to = Long.valueOf(((Integer) to).longValue());
+ to = ((Integer) to).longValue();
}
// TODO: should we care about different types here?
@@ -435,16 +433,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/540595e3/src/main/groovy/lang/Range.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/Range.java b/src/main/groovy/lang/Range.java
index e12f2c2..788be17 100644
--- a/src/main/groovy/lang/Range.java
+++ b/src/main/groovy/lang/Range.java
@@ -23,23 +23,10 @@ import java.util.List;
/**
* A Range represents the list of all items obtained by starting from a
* <code>from</code> value and calling <code>next()</code> successively
-<<<<<<< HEAD
* until you reach the <code>to</code> value. For a reverse range,
* the list is obtained by starting at the <code>to</code> value and
* successively calling <code>previous()</code> until the <code>from</code>
* value is reached.
-=======
- * until another call to <code>next()</code> would be higher than the <code>to</code> value.
- * For a reverse range, the list is obtained by starting at the <code>to</code> value and
- * successively calling <code>previous()</code> until another call to
- * <code>previous()</code> would be lower than the <code>from</code>value.
- * <p>
- * This means in odd cases the second boundary may not be contained in the range,
- * and a..b may produce a different set of elements than (b..a).reversed().
- * E.g. 1..2.5 == [1, 2]; but 2.5..1 == [2.5, 1.5]
->>>>>>> a8413d3... Refactor: unified the style of Range related classes
- *
- * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
*/
public interface Range<T extends Comparable> extends List<T> {
/**
http://git-wip-us.apache.org/repos/asf/groovy/blob/540595e3/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());
}