Author: henrib Date: Fri Jul 17 10:18:20 2015 New Revision: 1691507 URL: http://svn.apache.org/r1691507 Log: JEXL: JEXL-164 fix, added getMin/getMax methods, added tests, updated changes.xml
Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/IntegerRange.java commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/LongRange.java commons/proper/jexl/trunk/src/site/xdoc/changes.xml commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/internal/RangeTest.java Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/IntegerRange.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/IntegerRange.java?rev=1691507&r1=1691506&r2=1691507&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/IntegerRange.java (original) +++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/IntegerRange.java Fri Jul 17 10:18:20 2015 @@ -26,9 +26,9 @@ import java.util.NoSuchElementException; */ public class IntegerRange implements Collection<Integer> { /** The lower boundary. */ - private final int low; + private final int min; /** The upper boundary. */ - private final int high; + private final int max; /** * Creates a new range. @@ -37,19 +37,36 @@ public class IntegerRange implements Col */ public IntegerRange(int from, int to) { if (from > to) { - high = from; - low = to; + max = from; + min = to; } else { - low = from; - high = to; + min = from; + max = to; } } + /** + * Gets the interval minimum value. + * @return the low boundary + */ + public int getMin() { + return min; + } + + /** + * Gets the interval maximum value. + * @return the high boundary + */ + public int getMax() { + return max; + } + + @Override public int hashCode() { int hash = 7; - hash = 13 * hash + this.low; - hash = 13 * hash + this.high; + hash = 13 * hash + this.min; + hash = 13 * hash + this.max; return hash; } @@ -62,10 +79,10 @@ public class IntegerRange implements Col return false; } final IntegerRange other = (IntegerRange) obj; - if (this.low != other.low) { + if (this.min != other.min) { return false; } - if (this.high != other.high) { + if (this.max != other.max) { return false; } return true; @@ -73,12 +90,12 @@ public class IntegerRange implements Col @Override public Iterator<Integer> iterator() { - return new IntegerIterator(low, high); + return new IntegerIterator(min, max); } @Override public int size() { - return high - low + 1; + return max - min + 1; } @Override @@ -90,7 +107,7 @@ public class IntegerRange implements Col public boolean contains(Object o) { if (o instanceof Number) { long v = ((Number) o).intValue(); - return low <= v && v <= high; + return min <= v && v <= max; } else { return false; } @@ -101,7 +118,7 @@ public class IntegerRange implements Col final int size = size(); Object[] array = new Object[size]; for(int a = 0; a < size; ++a) { - array[a] = low + a; + array[a] = min + a; } return array; } @@ -119,7 +136,7 @@ public class IntegerRange implements Col : (T[]) Array.newInstance(ct, length); } for (int a = 0; a < length; ++a) { - Array.set(copy, a, low + a); + Array.set(copy, a, min + a); } if (length < array.length) { array[length] = null; @@ -175,9 +192,9 @@ public class IntegerRange implements Col */ class IntegerIterator implements Iterator<Integer> { /** The lower boundary. */ - private final int low; + private final int min; /** The upper boundary. */ - private final int high; + private final int max; /** The current value. */ private int cursor; /** @@ -186,25 +203,22 @@ class IntegerIterator implements Iterato * @param h high boundary */ public IntegerIterator(int l, int h) { - low = l; - high = h; - cursor = low; + min = l; + max = h; + cursor = min; } @Override public boolean hasNext() { - return cursor <= high; + return cursor <= max; } @Override public Integer next() { - if (cursor <= high) { - int next = cursor; - cursor += 1; - return Integer.valueOf(next); - } else { - throw new NoSuchElementException(); + if (cursor <= max) { + return cursor++; } + throw new NoSuchElementException(); } @Override Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/LongRange.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/LongRange.java?rev=1691507&r1=1691506&r2=1691507&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/LongRange.java (original) +++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/LongRange.java Fri Jul 17 10:18:20 2015 @@ -27,9 +27,9 @@ import java.util.NoSuchElementException; */ public class LongRange implements Collection<Long> { /** The lower boundary. */ - private final long low; + private final long min; /** The upper boundary. */ - private final long high; + private final long max; /** * Creates a new range. @@ -38,19 +38,35 @@ public class LongRange implements Collec */ public LongRange(long from, long to) { if (from > to) { - high = from; - low = to; + max = from; + min = to; } else { - low = from; - high = to; + min = from; + max = to; } } + /** + * Gets the interval minimum value. + * @return the low boundary + */ + public long getMin() { + return min; + } + + /** + * Gets the interval maximum value. + * @return the high boundary + */ + public long getMax() { + return max; + } + @Override public int hashCode() { int hash = 3; - hash = 41 * hash + (int) (this.low ^ (this.low >>> 32)); - hash = 41 * hash + (int) (this.high ^ (this.high >>> 32)); + hash = 41 * hash + (int) (this.min ^ (this.min >>> 32)); + hash = 41 * hash + (int) (this.max ^ (this.max >>> 32)); return hash; } @@ -63,10 +79,10 @@ public class LongRange implements Collec return false; } final LongRange other = (LongRange) obj; - if (this.low != other.low) { + if (this.min != other.min) { return false; } - if (this.high != other.high) { + if (this.max != other.max) { return false; } return true; @@ -74,12 +90,12 @@ public class LongRange implements Collec @Override public Iterator<Long> iterator() { - return new LongIterator(low, high); + return new LongIterator(min, max); } @Override public int size() { - return (int)(high - low + 1); + return (int)(max - min + 1); } @Override @@ -91,7 +107,7 @@ public class LongRange implements Collec public boolean contains(Object o) { if (o instanceof Number) { long v = ((Number) o).longValue(); - return low <= v && v <= high; + return min <= v && v <= max; } else { return false; } @@ -102,7 +118,7 @@ public class LongRange implements Collec final int size = size(); Object[] array = new Object[size]; for(int a = 0; a < size; ++a) { - array[a] = low + a; + array[a] = min + a; } return array; } @@ -120,7 +136,7 @@ public class LongRange implements Collec : (T[]) Array.newInstance(ct, length); } for (int a = 0; a < length; ++a) { - Array.set(copy, a, low + a); + Array.set(copy, a, min + a); } if (length < array.length) { array[length] = null; @@ -176,9 +192,9 @@ public class LongRange implements Collec */ class LongIterator implements Iterator<Long> { /** The lower boundary. */ - private final long low; + private final long min; /** The upper boundary. */ - private final long high; + private final long max; /** The current value. */ private long cursor; /** @@ -187,25 +203,22 @@ class LongIterator implements Iterator<L * @param h high boundary */ public LongIterator(long l, long h) { - low = l; - high = h; - cursor = low; + min = l; + max = h; + cursor = min; } @Override public boolean hasNext() { - return cursor <= high; + return cursor <= max; } @Override public Long next() { - if (cursor <= high) { - long next = cursor; - cursor += 1; - return Long.valueOf(next); - } else { - throw new NoSuchElementException(); + if (cursor <= max) { + return cursor++; } + throw new NoSuchElementException(); } @Override Modified: commons/proper/jexl/trunk/src/site/xdoc/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/site/xdoc/changes.xml?rev=1691507&r1=1691506&r2=1691507&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/site/xdoc/changes.xml (original) +++ commons/proper/jexl/trunk/src/site/xdoc/changes.xml Fri Jul 17 10:18:20 2015 @@ -26,6 +26,9 @@ </properties> <body> <release version="3.0" date="unreleased"> + <action dev="henrib" type="add" issue="JEXL-164" due-to="Dmitri Blinov"> + public getters for high/low properties for IntegerRange and LongRange + </action> <action dev="henrib" type="fix" issue="JEXL-163" due-to="Dmitri Blinov"> empty(new ("java.lang.Long", 4294967296)) returns true </action> Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/internal/RangeTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/internal/RangeTest.java?rev=1691507&r1=1691506&r2=1691507&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/internal/RangeTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/internal/RangeTest.java Fri Jul 17 10:18:20 2015 @@ -51,11 +51,14 @@ public class RangeTest extends JexlTestC @Test public void testRanges() { LongRange lr0 = new LongRange(20,10); + Assert.assertEquals(10L, lr0.getMin()); + Assert.assertEquals(20L, lr0.getMax()); Assert.assertFalse(lr0.isEmpty()); Assert.assertTrue(lr0.contains(10L)); Assert.assertTrue(lr0.contains(20L)); Assert.assertFalse(lr0.contains(30L)); Assert.assertFalse(lr0.contains(5L)); + Assert.assertFalse(lr0.contains(null)); LongRange lr1 = new LongRange(10,20); Assert.assertEquals(lr0, lr1); Assert.assertTrue(lr0.containsAll(lr1)); @@ -64,11 +67,14 @@ public class RangeTest extends JexlTestC Assert.assertTrue(lr0.containsAll(lr2)); Assert.assertFalse(lr2.containsAll(lr1)); IntegerRange ir0 = new IntegerRange(20,10); + Assert.assertEquals(10, ir0.getMin()); + Assert.assertEquals(20, ir0.getMax()); Assert.assertFalse(ir0.isEmpty()); Assert.assertTrue(ir0.contains(10)); Assert.assertTrue(ir0.contains(20)); Assert.assertFalse(ir0.contains(30)); Assert.assertFalse(ir0.contains(5)); + Assert.assertFalse(ir0.contains(null)); IntegerRange ir1 = new IntegerRange(10,20); Assert.assertEquals(ir0, ir1); Assert.assertTrue(ir0.containsAll(ir1));