commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1439153 - in /commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range: DoubleRange.java Endpoint.java FloatRange.java IntegerRange.java LongRange.java NumericRange.java
Date Sun, 27 Jan 2013 19:32:00 GMT
Author: mbenson
Date: Sun Jan 27 19:32:00 2013
New Revision: 1439153

URL: http://svn.apache.org/viewvc?rev=1439153&view=rev
Log:
start to merge commonalities among NumericRanges, including a few small bugfixes

Modified:
    commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/DoubleRange.java
    commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/Endpoint.java
    commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/FloatRange.java
    commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/IntegerRange.java
    commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/LongRange.java
    commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/NumericRange.java

Modified: commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/DoubleRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/DoubleRange.java?rev=1439153&r1=1439152&r2=1439153&view=diff
==============================================================================
--- commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/DoubleRange.java
(original)
+++ commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/DoubleRange.java
Sun Jan 27 19:32:00 2013
@@ -19,7 +19,6 @@ package org.apache.commons.functor.gener
 
 import org.apache.commons.functor.BinaryFunction;
 import org.apache.commons.functor.UnaryProcedure;
-import org.apache.commons.lang3.Validate;
 
 /**
  * A generator for a range of doubles.
@@ -29,23 +28,6 @@ import org.apache.commons.lang3.Validate
  */
 public class DoubleRange extends NumericRange<Double> {
 
-    // attributes
-    // ---------------------------------------------------------------
-    /**
-     * Left limit.
-     */
-    private final Endpoint<Double> leftEndpoint;
-
-    /**
-     * Right limit.
-     */
-    private final Endpoint<Double> rightEndpoint;
-
-    /**
-     * Increment step.
-     */
-    private final double step;
-
     /**
      * Calculate default step.
      */
@@ -110,20 +92,9 @@ public class DoubleRange extends Numeric
      * @param rightBoundType type of right bound
      * @param step increment
      */
-    public DoubleRange(double from, BoundType leftBoundType, double to,
-                       BoundType rightBoundType, double step) {
-        this.leftEndpoint = Validate
-            .notNull(new Endpoint<Double>(from, leftBoundType),
-                     "Left Endpoint argument must not be null");
-        this.rightEndpoint = Validate
-            .notNull(new Endpoint<Double>(to, rightBoundType),
-                     "Right Endpoint argument must not be null");
-        this.step = step;
-        if (from != to && Math.signum(step) != Math.signum(to - from)) {
-            throw new IllegalArgumentException("Will never reach " + to
-                                               + " from " + from
-                                               + " using step " + step);
-        }
+    public DoubleRange(double from, BoundType leftBoundType, double to, BoundType rightBoundType,
double step) {
+        this(new Endpoint<Double>(Double.valueOf(from), leftBoundType), new Endpoint<Double>(Double.valueOf(to),
+            rightBoundType), Double.valueOf(step));
     }
 
     /**
@@ -133,19 +104,8 @@ public class DoubleRange extends Numeric
      * @param to end
      * @param step increment
      */
-    public DoubleRange(Endpoint<Double> from, Endpoint<Double> to, double step)
{
-        this.leftEndpoint = Validate
-            .notNull(from, "Left Endpoint argument must not be null");
-        this.rightEndpoint = Validate
-            .notNull(to, "Right Endpoint argument must not be null");
-        this.step = step;
-        if (from != to
-            && Math.signum(step) != Math.signum(to.getValue().doubleValue()
-                                             - from.getValue().doubleValue())) {
-            throw new IllegalArgumentException("Will never reach " + to
-                                               + " from " + from
-                                               + " using step " + step);
-        }
+    public DoubleRange(Endpoint<Double> from, Endpoint<Double> to, Double step)
{
+        super(from, to, step);
     }
 
     // methods
@@ -154,27 +114,6 @@ public class DoubleRange extends Numeric
     /**
      * {@inheritDoc}
      */
-    public Endpoint<Double> getLeftEndpoint() {
-        return this.leftEndpoint;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public Endpoint<Double> getRightEndpoint() {
-        return this.rightEndpoint;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public Double getStep() {
-        return this.step;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
     public void run(UnaryProcedure<? super Double> proc) {
         final double step = this.getStep();
         final boolean includeLeftValue = this.getLeftEndpoint()
@@ -208,45 +147,4 @@ public class DoubleRange extends Numeric
         }
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public String toString() {
-        return "DoubleRange<" + this.leftEndpoint.toLeftString() + ", "
-                + this.rightEndpoint.toRightString() + ", " + this.step + ">";
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public boolean equals(Object obj) {
-        if (obj == this) {
-            return true;
-        }
-        if (!(obj instanceof DoubleRange)) {
-            return false;
-        }
-        DoubleRange that = (DoubleRange) obj;
-        return this.leftEndpoint.equals(that.leftEndpoint)
-                && this.rightEndpoint.equals(that.rightEndpoint)
-                && this.step == that.step;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public int hashCode() {
-        int hash = "DoubleRange".hashCode();
-        hash <<= 2;
-        hash ^= this.leftEndpoint.getValue().hashCode();
-        hash <<= 2;
-        hash ^= this.rightEndpoint.getValue().hashCode();
-        hash <<= 2;
-        hash ^= Double.valueOf(this.step).hashCode();
-        return hash;
-    }
-
 }

Modified: commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/Endpoint.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/Endpoint.java?rev=1439153&r1=1439152&r2=1439153&view=diff
==============================================================================
--- commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/Endpoint.java
(original)
+++ commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/Endpoint.java
Sun Jan 27 19:32:00 2013
@@ -84,8 +84,7 @@ public class Endpoint<T extends Comparab
             return false;
         }
         Endpoint<?> that = (Endpoint<?>) obj;
-        return this.boundType == that.boundType
-                && this.value.equals(that.value);
+        return this.boundType == that.boundType && this.value.equals(that.value);
     }
 
     /**

Modified: commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/FloatRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/FloatRange.java?rev=1439153&r1=1439152&r2=1439153&view=diff
==============================================================================
--- commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/FloatRange.java
(original)
+++ commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/FloatRange.java
Sun Jan 27 19:32:00 2013
@@ -19,7 +19,6 @@ package org.apache.commons.functor.gener
 
 import org.apache.commons.functor.BinaryFunction;
 import org.apache.commons.functor.UnaryProcedure;
-import org.apache.commons.lang3.Validate;
 
 /**
  * A generator for a range of float.
@@ -29,23 +28,6 @@ import org.apache.commons.lang3.Validate
  */
 public class FloatRange extends NumericRange<Float> {
 
-    // attributes
-    // ---------------------------------------------------------------
-    /**
-     * Left limit.
-     */
-    private final Endpoint<Float> leftEndpoint;
-
-    /**
-     * Right limit.
-     */
-    private final Endpoint<Float> rightEndpoint;
-
-    /**
-     * Increment step.
-     */
-    private final float step;
-
     /**
      * Calculate default step.
      */
@@ -109,20 +91,9 @@ public class FloatRange extends NumericR
      * @param rightBoundType type of right bound
      * @param step increment
      */
-    public FloatRange(float from, BoundType leftBoundType, float to,
-                      BoundType rightBoundType, float step) {
-        this.leftEndpoint = Validate
-            .notNull(new Endpoint<Float>(from, leftBoundType),
-                     "Left Endpoint argument must not be null");
-        this.rightEndpoint = Validate
-            .notNull(new Endpoint<Float>(to, rightBoundType),
-                     "Right Endpoint argument must not be null");
-        this.step = step;
-        if (from != to && Math.signum(step) != Math.signum(to - from)) {
-            throw new IllegalArgumentException("Will never reach " + to
-                                               + " from " + from
-                                               + " using step " + step);
-        }
+    public FloatRange(float from, BoundType leftBoundType, float to, BoundType rightBoundType,
float step) {
+        this(new Endpoint<Float>(Float.valueOf(from), leftBoundType), new Endpoint<Float>(Float.valueOf(to),
+            rightBoundType), Float.valueOf(step));
     }
 
     /**
@@ -132,19 +103,8 @@ public class FloatRange extends NumericR
      * @param to end
      * @param step increment
      */
-    public FloatRange(Endpoint<Float> from, Endpoint<Float> to, float step) {
-        this.leftEndpoint = Validate
-            .notNull(from, "Left Endpoint argument must not be null");
-        this.rightEndpoint = Validate
-            .notNull(to, "Right Endpoint argument must not be null");
-        this.step = step;
-        if (from != to
-            && Math.signum(step) != Math.signum(to.getValue().doubleValue()
-                                             - from.getValue().doubleValue())) {
-            throw new IllegalArgumentException("Will never reach " + to
-                                               + " from " + from
-                                               + " using step " + step);
-        }
+    public FloatRange(Endpoint<Float> from, Endpoint<Float> to, Float step) {
+        super(from, to, step);
     }
 
     // methods
@@ -152,27 +112,6 @@ public class FloatRange extends NumericR
     /**
      * {@inheritDoc}
      */
-    public Endpoint<Float> getLeftEndpoint() {
-        return this.leftEndpoint;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public Endpoint<Float> getRightEndpoint() {
-        return this.rightEndpoint;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public Float getStep() {
-        return this.step;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
     public void run(UnaryProcedure<? super Float> proc) {
         final float step = this.getStep();
         final boolean includeLeftValue = this.getLeftEndpoint()
@@ -206,45 +145,4 @@ public class FloatRange extends NumericR
         }
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public String toString() {
-        return "FloatRange<" + this.leftEndpoint.toLeftString() + ", "
-                + this.rightEndpoint.toRightString() + ", " + this.step + ">";
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public boolean equals(Object obj) {
-        if (obj == this) {
-            return true;
-        }
-        if (!(obj instanceof FloatRange)) {
-            return false;
-        }
-        FloatRange that = (FloatRange) obj;
-        return this.leftEndpoint.equals(that.leftEndpoint)
-                && this.rightEndpoint.equals(that.rightEndpoint)
-                && this.step == that.step;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public int hashCode() {
-        int hash = "FloatRange".hashCode();
-        hash <<= 2;
-        hash ^= this.leftEndpoint.getValue().hashCode();
-        hash <<= 2;
-        hash ^= this.rightEndpoint.getValue().hashCode();
-        hash <<= 2;
-        hash ^= Float.valueOf(this.step).hashCode();
-        return hash;
-    }
-
 }

Modified: commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/IntegerRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/IntegerRange.java?rev=1439153&r1=1439152&r2=1439153&view=diff
==============================================================================
--- commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/IntegerRange.java
(original)
+++ commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/IntegerRange.java
Sun Jan 27 19:32:00 2013
@@ -19,7 +19,6 @@ package org.apache.commons.functor.gener
 
 import org.apache.commons.functor.BinaryFunction;
 import org.apache.commons.functor.UnaryProcedure;
-import org.apache.commons.lang3.Validate;
 
 /**
  * A range of integers.
@@ -29,23 +28,6 @@ import org.apache.commons.lang3.Validate
  */
 public class IntegerRange extends NumericRange<Integer> {
 
-    // attributes
-    // ---------------------------------------------------------------
-    /**
-     * Left limit.
-     */
-    private final Endpoint<Integer> leftEndpoint;
-
-    /**
-     * Right limit.
-     */
-    private final Endpoint<Integer> rightEndpoint;
-
-    /**
-     * Increment step.
-     */
-    private final int step;
-
     /**
      * Calculate default step.
      */
@@ -110,20 +92,9 @@ public class IntegerRange extends Numeri
      * @param rightBoundType type of right bound
      * @param step increment
      */
-    public IntegerRange(int from, BoundType leftBoundType, int to,
-                        BoundType rightBoundType, int step) {
-        this.leftEndpoint = Validate
-            .notNull(new Endpoint<Integer>(from, leftBoundType),
-                     "Left Endpoint argument must not be null");
-        this.rightEndpoint = Validate
-            .notNull(new Endpoint<Integer>(to, rightBoundType),
-                     "Right Endpoint argument must not be null");
-        this.step = step;
-        if (from != to && Integer.signum(step) != Integer.signum(to - from)) {
-            throw new IllegalArgumentException("Will never reach " + to
-                                               + " from " + from
-                                               + " using step " + step);
-        }
+    public IntegerRange(int from, BoundType leftBoundType, int to, BoundType rightBoundType,
int step) {
+        this(new Endpoint<Integer>(Integer.valueOf(from), leftBoundType), new Endpoint<Integer>(Integer.valueOf(to),
+            rightBoundType), Integer.valueOf(step));
     }
 
     /**
@@ -133,43 +104,12 @@ public class IntegerRange extends Numeri
      * @param to end
      * @param step increment
      */
-    public IntegerRange(Endpoint<Integer> from, Endpoint<Integer> to, int step)
{
-        this.leftEndpoint = Validate
-            .notNull(from, "Left Endpoint argument must not be null");
-        this.rightEndpoint = Validate
-            .notNull(to, "Right Endpoint argument must not be null");
-        this.step = step;
-        if (from != to
-            && Integer.signum(step) != Integer.signum(to.getValue()
-                                                   - from.getValue())) {
-            throw new IllegalArgumentException("Will never reach " + to
-                                               + " from " + from
-                                               + " using step " + step);
-        }
-    }
+    public IntegerRange(Endpoint<Integer> from, Endpoint<Integer> to, Integer
step) {
+        super(from, to, step);
+   }
 
     // methods
     // ---------------------------------------------------------------
-    /**
-     * {@inheritDoc}
-     */
-    public Endpoint<Integer> getLeftEndpoint() {
-        return this.leftEndpoint;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public Endpoint<Integer> getRightEndpoint() {
-        return this.rightEndpoint;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public Integer getStep() {
-        return this.step;
-    }
 
     /**
      * {@inheritDoc}
@@ -207,45 +147,4 @@ public class IntegerRange extends Numeri
         }
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public String toString() {
-        return "IntegerRange<" + this.leftEndpoint.toLeftString()
-                + ", " + this.rightEndpoint.toRightString()
-                + ", " + this.step + ">";
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public boolean equals(Object obj) {
-        if (obj == this) {
-            return true;
-        }
-        if (!(obj instanceof IntegerRange)) {
-            return false;
-        }
-        IntegerRange that = (IntegerRange) obj;
-        return this.leftEndpoint.equals(that.leftEndpoint)
-                && this.rightEndpoint.equals(that.rightEndpoint)
-                && this.step == that.step;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public int hashCode() {
-        int hash = "IntegerRange".hashCode();
-        hash <<= 2;
-        hash ^= this.leftEndpoint.getValue();
-        hash <<= 2;
-        hash ^= this.rightEndpoint.getValue();
-        hash <<= 2;
-        hash ^= this.step;
-        return hash;
-    }
 }

Modified: commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/LongRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/LongRange.java?rev=1439153&r1=1439152&r2=1439153&view=diff
==============================================================================
--- commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/LongRange.java
(original)
+++ commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/LongRange.java
Sun Jan 27 19:32:00 2013
@@ -16,7 +16,6 @@ package org.apache.commons.functor.gener
 
 import org.apache.commons.functor.BinaryFunction;
 import org.apache.commons.functor.UnaryProcedure;
-import org.apache.commons.lang3.Validate;
 
 /**
  * A range of longs.
@@ -25,24 +24,6 @@ import org.apache.commons.lang3.Validate
  * @version $Revision$ $Date$
  */
 public final class LongRange extends NumericRange<Long> {
-    // attributes
-    //---------------------------------------------------------------
-
-    /**
-     * Left limit.
-     */
-    private final Endpoint<Long> leftEndpoint;
-
-    /**
-     * Right limit.
-     */
-    private final Endpoint<Long> rightEndpoint;
-
-    /**
-     * Increment step.
-     */
-    private final long step;
-
     /**
      * Calculate default step.
      */
@@ -106,20 +87,9 @@ public final class LongRange extends Num
      * @param rightBoundType type of right bound
      * @param step increment
      */
-    public LongRange(long from, BoundType leftBoundType, long to,
-                     BoundType rightBoundType, long step) {
-        this.leftEndpoint = Validate
-            .notNull(new Endpoint<Long>(from, leftBoundType),
-                     "Left Endpoint argument must not be null");
-        this.rightEndpoint = Validate
-            .notNull(new Endpoint<Long>(to, rightBoundType),
-                     "Right Endpoint argument must not be null");
-        this.step = step;
-        if (from != to && Long.signum(step) != Long.signum(to - from)) {
-            throw new IllegalArgumentException("Will never reach " + to
-                                               + " from " + from
-                                               + " using step " + step);
-        }
+    public LongRange(long from, BoundType leftBoundType, long to, BoundType rightBoundType,
long step) {
+        this(new Endpoint<Long>(Long.valueOf(from), leftBoundType),
+            new Endpoint<Long>(Long.valueOf(to), rightBoundType), Long.valueOf(step));
     }
 
     /**
@@ -129,18 +99,8 @@ public final class LongRange extends Num
      * @param to end
      * @param step increment
      */
-    public LongRange(Endpoint<Long> from, Endpoint<Long> to, long step) {
-        this.leftEndpoint = Validate
-            .notNull(from, "Left Endpoint argument must not be null");
-        this.rightEndpoint = Validate
-            .notNull(to, "Right Endpoint argument must not be null");
-        this.step = step;
-        if (from.equals(to) == Boolean.FALSE
-            && Long.signum(step) != Long.signum(to.getValue() - from.getValue()))
{
-            throw new IllegalArgumentException("Will never reach " + to
-                                               + " from " + from
-                                               + " using step " + step);
-        }
+    public LongRange(Endpoint<Long> from, Endpoint<Long> to, Long step) {
+        super(from, to, step);
     }
 
     // methods
@@ -181,66 +141,4 @@ public final class LongRange extends Num
         }
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    public Endpoint<Long> getLeftEndpoint() {
-        return this.leftEndpoint;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public Endpoint<Long> getRightEndpoint() {
-        return this.rightEndpoint;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public Long getStep() {
-        return this.step;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public String toString() {
-        return "LongRange<" + this.leftEndpoint.toLeftString() + ", "
-                + this.rightEndpoint.toRightString() + ", " + step + ">";
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public boolean equals(Object obj) {
-        if (obj == this) {
-            return true;
-        }
-        if (!(obj instanceof LongRange)) {
-            return false;
-        }
-        LongRange that = (LongRange) obj;
-        return this.leftEndpoint.equals(that.leftEndpoint)
-                && this.rightEndpoint.equals(that.rightEndpoint)
-                && this.step == that.step;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public int hashCode() {
-        int hash = "LongRange".hashCode();
-        hash <<= 2;
-        hash ^= this.leftEndpoint.getValue();
-        hash <<= 2;
-        hash ^= this.rightEndpoint.getValue();
-        hash <<= 2;
-        hash ^= this.step;
-        return hash;
-    }
-
 }

Modified: commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/NumericRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/NumericRange.java?rev=1439153&r1=1439152&r2=1439153&view=diff
==============================================================================
--- commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/NumericRange.java
(original)
+++ commons/proper/functor/branches/FUNCTOR-14-mm/core/src/main/java/org/apache/commons/functor/generator/range/NumericRange.java
Sun Jan 27 19:32:00 2013
@@ -20,6 +20,7 @@ package org.apache.commons.functor.gener
 import java.util.Collection;
 
 import org.apache.commons.functor.generator.loop.LoopGenerator;
+import org.apache.commons.lang3.Validate;
 
 /**
  * A base class for numeric ranges. The elements within this range must be a
@@ -35,6 +36,57 @@ import org.apache.commons.functor.genera
  * @version $Revision: $ $Date: $
  */
 public abstract class NumericRange<T extends Number & Comparable<T>> extends
LoopGenerator<T> implements Range<T, T> {
+    // attributes
+    // ---------------------------------------------------------------
+    /**
+     * Left limit.
+     */
+    protected final Endpoint<T> leftEndpoint;
+
+    /**
+     * Right limit.
+     */
+    protected final Endpoint<T> rightEndpoint;
+    
+    protected final T step;
+
+    /**
+     * Create a new NumericRange instance.
+     * @param from left endpoint
+     * @param to right endpoint
+     * @param step
+     */
+    protected NumericRange(Endpoint<T> from, Endpoint<T> to, T step) {
+        this.leftEndpoint = Validate.notNull(from, "Left Endpoint argument must not be null");
+        this.rightEndpoint = Validate.notNull(to, "Right Endpoint argument must not be null");
+        this.step = Validate.notNull(step, "step argument must not be null");
+        final int cmp = to.getValue().compareTo(from.getValue());
+        if (cmp != 0 && Double.valueOf(Math.signum(step.doubleValue())).intValue()
!= cmp) {
+            throw new IllegalArgumentException(String.format("Will never reach %s from %s
using step %s", to, from,
+                step));
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Endpoint<T> getLeftEndpoint() {
+        return leftEndpoint;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Endpoint<T> getRightEndpoint() {
+        return rightEndpoint;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public T getStep() {
+        return step;
+    }
 
     /**
      * {@inheritDoc}
@@ -110,4 +162,43 @@ public abstract class NumericRange<T ext
         return true;
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        return String.format("%s<%s, %s, %s>", getClass().getSimpleName(), this.leftEndpoint.toLeftString(),
+            rightEndpoint.toRightString(), this.step);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (!(obj instanceof NumericRange<?>)) {
+            return false;
+        }
+        NumericRange<?> that = (NumericRange<?>) obj;
+        return this.leftEndpoint.equals(that.leftEndpoint) && this.rightEndpoint.equals(that.rightEndpoint)
+            && this.step.equals(that.step);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode() {
+        int hash = getClass().getSimpleName().hashCode();
+        hash <<= 2;
+        hash ^= this.leftEndpoint.getValue().hashCode();
+        hash <<= 2;
+        hash ^= this.rightEndpoint.getValue().hashCode();
+        hash <<= 2;
+        hash ^= this.step.hashCode();
+        return hash;
+    }
 }



Mime
View raw message