extract some duplicate Range code into AbstractRange
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/functor/trunk@1540231 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/core/src/main/java/org/apache/commons/functor/range/AbstractRange.java b/core/src/main/java/org/apache/commons/functor/range/AbstractRange.java
new file mode 100644
index 0000000..52c1695
--- /dev/null
+++ b/core/src/main/java/org/apache/commons/functor/range/AbstractRange.java
@@ -0,0 +1,132 @@
+package org.apache.commons.functor.range;
+
+import java.util.Collection;
+
+import org.apache.commons.lang3.Validate;
+import org.apache.commons.lang3.builder.EqualsBuilder;
+
+/**
+ * Abstract {@link Range}
+ *
+ * @param <T>
+ * @param <S>
+ */
+public abstract class AbstractRange<T extends Comparable<?>, S extends Comparable<?>> implements Range<T, S> {
+
+ /**
+ * Left limit.
+ */
+ protected final Endpoint<T> leftEndpoint;
+
+ /**
+ * Right limit.
+ */
+ protected final Endpoint<T> rightEndpoint;
+
+ /**
+ * Increment step.
+ */
+ protected final S step;
+
+ /**
+ * Create a new {@link AbstractRange}.
+ *
+ * @param leftEndpoint
+ * @param rightEndpoint
+ * @param step
+ */
+ protected AbstractRange(Endpoint<T> leftEndpoint, Endpoint<T> rightEndpoint, S step) {
+ super();
+ this.leftEndpoint = Validate.notNull(leftEndpoint, "Left Endpoint argument must not be null");
+ this.rightEndpoint = Validate.notNull(rightEndpoint, "Right Endpoint argument must not be null");
+ this.step = Validate.notNull(step, "step must not be null");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Endpoint<T> getLeftEndpoint() {
+ return leftEndpoint;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Endpoint<T> getRightEndpoint() {
+ return rightEndpoint;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public S getStep() {
+ return step;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean containsAll(Collection<T> col) {
+ if (col == null || col.isEmpty()) {
+ return false;
+ }
+ for (T t : col) {
+ if (!contains(t)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ // iterable, iterator methods
+ // ---------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ // object methods
+ // ---------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ final String pattern = "%s<%s, %s, %s>";
+ return String.format(pattern, getClass().getSimpleName(), leftEndpoint.toLeftString(),
+ rightEndpoint.toRightString(), step);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (!(obj instanceof AbstractRange<?, ?>)) {
+ return false;
+ }
+ final Range<?, ?> that = (Range<?, ?>) obj;
+ return new EqualsBuilder().append(getLeftEndpoint(), that.getLeftEndpoint())
+ .append(getRightEndpoint(), that.getRightEndpoint()).append(getStep(), that.getStep()).build();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ int hash = getClass().getName().hashCode();
+ hash <<= 2;
+ hash ^= this.leftEndpoint.hashCode();
+ hash <<= 2;
+ hash ^= this.rightEndpoint.hashCode();
+ hash <<= 2;
+ hash ^= this.step.hashCode();
+ return hash;
+ }
+}
diff --git a/core/src/main/java/org/apache/commons/functor/range/CharacterRange.java b/core/src/main/java/org/apache/commons/functor/range/CharacterRange.java
index c7544ce..ae918c4 100644
--- a/core/src/main/java/org/apache/commons/functor/range/CharacterRange.java
+++ b/core/src/main/java/org/apache/commons/functor/range/CharacterRange.java
@@ -16,7 +16,6 @@
*/
package org.apache.commons.functor.range;
-import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.functor.BinaryFunction;
@@ -28,7 +27,7 @@
* @since 1.0
* @version $Revision$ $Date$
*/
-public final class CharacterRange implements Range<Character, Integer> {
+public final class CharacterRange extends AbstractRange<Character, Integer> {
// attributes
// ---------------------------------------------------------------
@@ -43,21 +42,6 @@
public static final BoundType DEFAULT_RIGHT_BOUND_TYPE = BoundType.CLOSED;
/**
- * Left limit.
- */
- private final Endpoint<Character> leftEndpoint;
-
- /**
- * Right limit.
- */
- private final Endpoint<Character> rightEndpoint;
-
- /**
- * Increment step.
- */
- private final int step;
-
- /**
* Current value.
*/
private char currentValue;
@@ -116,9 +100,7 @@
* @throws NullPointerException if either {@link Endpoint} is {@code null}
*/
public CharacterRange(Endpoint<Character> from, Endpoint<Character> 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;
+ super(from, to, Integer.valueOf(step));
final char f = from.getValue();
final char t = to.getValue();
@@ -161,26 +143,6 @@
// range methods
// ---------------------------------------------------------------
- /**
- * {@inheritDoc}
- */
- public Endpoint<Character> getLeftEndpoint() {
- return this.leftEndpoint;
- }
-
- /**
- * {@inheritDoc}
- */
- public Endpoint<Character> getRightEndpoint() {
- return this.rightEndpoint;
- }
-
- /**
- * {@inheritDoc}
- */
- public Integer getStep() {
- return Integer.valueOf(step);
- }
// iterable, iterator methods
// ---------------------------------------------------------------
@@ -217,60 +179,10 @@
/**
* {@inheritDoc}
*/
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- */
public Iterator<Character> iterator() {
return this;
}
- // object methods
- // ---------------------------------------------------------------
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- return "CharacterRange<" + this.leftEndpoint.toLeftString() + ", "
- + this.rightEndpoint.toRightString() + ", " + step + ">";
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean equals(Object obj) {
- if (obj == this) {
- return true;
- }
- if (!(obj instanceof CharacterRange)) {
- return false;
- }
- CharacterRange that = (CharacterRange) obj;
- return this.leftEndpoint.equals(that.leftEndpoint)
- && this.rightEndpoint.equals(that.rightEndpoint)
- && this.step == that.step;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode() {
- int hash = "CharacterRange".hashCode();
- hash <<= 2;
- hash ^= this.leftEndpoint.getValue();
- hash <<= 2;
- hash ^= this.rightEndpoint.getValue();
- hash <<= 2;
- hash ^= this.step;
- return hash;
- }
-
/**
* {@inheritDoc}
*/
@@ -328,21 +240,4 @@
return ((double) (value - firstValue) / step + 1) % 1.0 == 0.0;
}
- /**
- * {@inheritDoc}
- */
- public boolean containsAll(Collection<Character> col) {
- if (col == null || col.size() == 0) {
- return Boolean.FALSE;
- }
- boolean r = Boolean.TRUE;
- for (Character t : col) {
- if (!this.contains(t)) {
- r = Boolean.FALSE;
- break;
- }
- }
- return r;
- }
-
}
diff --git a/core/src/main/java/org/apache/commons/functor/range/DoubleRange.java b/core/src/main/java/org/apache/commons/functor/range/DoubleRange.java
index 63d3a1f..1a1e2f3 100644
--- a/core/src/main/java/org/apache/commons/functor/range/DoubleRange.java
+++ b/core/src/main/java/org/apache/commons/functor/range/DoubleRange.java
@@ -31,20 +31,6 @@
// attributes
// ---------------------------------------------------------------
- /**
- * Left limit.
- */
- private final Endpoint<Double> leftEndpoint;
-
- /**
- * Right limit.
- */
- private final Endpoint<Double> rightEndpoint;
-
- /**
- * Increment step.
- */
- private final double step;
/**
* Current value.
@@ -140,9 +126,7 @@
* @throws NullPointerException if either {@link Endpoint} is {@code null}
*/
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;
+ super(from, to, Double.valueOf(step));
final double f = from.getValue();
final double t = to.getValue();
@@ -171,29 +155,7 @@
this(new Endpoint<Double>(from, leftBoundType), new Endpoint<Double>(to, rightBoundType), step);
}
- // range methods
- // ---------------------------------------------------------------
- /**
- * {@inheritDoc}
- */
- public Endpoint<Double> getLeftEndpoint() {
- return this.leftEndpoint;
- }
-
- /**
- * {@inheritDoc}
- */
- public Endpoint<Double> getRightEndpoint() {
- return this.rightEndpoint;
- }
-
- /**
- * {@inheritDoc}
- */
- public Double getStep() {
- return Double.valueOf(step);
- }
- // iterable, iterator methods
+ // iterable, iterator methods
// ---------------------------------------------------------------
/**
* {@inheritDoc}
@@ -228,58 +190,8 @@
/**
* {@inheritDoc}
*/
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- */
public Iterator<Double> iterator() {
return this;
}
- // object methods
- // ---------------------------------------------------------------
- /**
- * {@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;
- }
-
}
diff --git a/core/src/main/java/org/apache/commons/functor/range/FloatRange.java b/core/src/main/java/org/apache/commons/functor/range/FloatRange.java
index e3e3111..3c6de49 100644
--- a/core/src/main/java/org/apache/commons/functor/range/FloatRange.java
+++ b/core/src/main/java/org/apache/commons/functor/range/FloatRange.java
@@ -31,20 +31,6 @@
// attributes
// ---------------------------------------------------------------
- /**
- * Left limit.
- */
- private final Endpoint<Float> leftEndpoint;
-
- /**
- * Right limit.
- */
- private final Endpoint<Float> rightEndpoint;
-
- /**
- * Increment step.
- */
- private final float step;
/**
* Current value.
@@ -125,9 +111,7 @@
* @throws NullPointerException if either {@link Endpoint} is {@code null}
*/
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;
+ super(from, to, Float.valueOf(step));
final float f = from.getValue();
final float t = to.getValue();
@@ -170,29 +154,6 @@
this(new Endpoint<Float>(from, leftBoundType), new Endpoint<Float>(to, rightBoundType), step);
}
- // range methods
- // ---------------------------------------------------------------
- /**
- * {@inheritDoc}
- */
- public Endpoint<Float> getLeftEndpoint() {
- return this.leftEndpoint;
- }
-
- /**
- * {@inheritDoc}
- */
- public Endpoint<Float> getRightEndpoint() {
- return this.rightEndpoint;
- }
-
- /**
- * {@inheritDoc}
- */
- public Float getStep() {
- return Float.valueOf(step);
- }
-
// iterable, iterator methods
// ---------------------------------------------------------------
/**
@@ -228,58 +189,8 @@
/**
* {@inheritDoc}
*/
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- */
public Iterator<Float> iterator() {
return this;
}
- // object methods
- // ---------------------------------------------------------------
- /**
- * {@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;
- }
-
}
diff --git a/core/src/main/java/org/apache/commons/functor/range/IntegerRange.java b/core/src/main/java/org/apache/commons/functor/range/IntegerRange.java
index 981d4a2..2f98736 100644
--- a/core/src/main/java/org/apache/commons/functor/range/IntegerRange.java
+++ b/core/src/main/java/org/apache/commons/functor/range/IntegerRange.java
@@ -31,20 +31,6 @@
// attributes
// ---------------------------------------------------------------
- /**
- * Left limit.
- */
- private final Endpoint<Integer> leftEndpoint;
-
- /**
- * Right limit.
- */
- private final Endpoint<Integer> rightEndpoint;
-
- /**
- * Increment step.
- */
- private final int step;
/**
* Current value.
@@ -126,9 +112,7 @@
* @throws NullPointerException if either {@link Endpoint} is {@code null}
*/
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;
+ super(from, to, Integer.valueOf(step));
final int f = from.getValue();
final int t = to.getValue();
@@ -170,29 +154,6 @@
this(new Endpoint<Integer>(from, leftBoundType), new Endpoint<Integer>(to, rightBoundType), step);
}
- // range methods
- // ---------------------------------------------------------------
- /**
- * {@inheritDoc}
- */
- public Endpoint<Integer> getLeftEndpoint() {
- return this.leftEndpoint;
- }
-
- /**
- * {@inheritDoc}
- */
- public Endpoint<Integer> getRightEndpoint() {
- return this.rightEndpoint;
- }
-
- /**
- * {@inheritDoc}
- */
- public Integer getStep() {
- return Integer.valueOf(step);
- }
-
// iterable, iterator methods
// ---------------------------------------------------------------
/**
@@ -228,59 +189,8 @@
/**
* {@inheritDoc}
*/
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- */
public Iterator<Integer> iterator() {
return this;
}
- // object methods
- // ---------------------------------------------------------------
- /**
- * {@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;
- }
-
}
diff --git a/core/src/main/java/org/apache/commons/functor/range/LongRange.java b/core/src/main/java/org/apache/commons/functor/range/LongRange.java
index 3777c1f..ed035d5 100644
--- a/core/src/main/java/org/apache/commons/functor/range/LongRange.java
+++ b/core/src/main/java/org/apache/commons/functor/range/LongRange.java
@@ -32,21 +32,6 @@
//---------------------------------------------------------------
/**
- * Left limit.
- */
- private final Endpoint<Long> leftEndpoint;
-
- /**
- * Right limit.
- */
- private final Endpoint<Long> rightEndpoint;
-
- /**
- * Increment step.
- */
- private final long step;
-
- /**
* Current value.
*/
private long currentValue;
@@ -150,9 +135,7 @@
* @throws NullPointerException if either {@link Endpoint} is {@code null}
*/
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;
+ super(from, to, Long.valueOf(step));
final long f = from.getValue();
final long t = to.getValue();
@@ -181,29 +164,6 @@
this(new Endpoint<Long>(from, leftBoundType), new Endpoint<Long>(to, rightBoundType), step);
}
- // range methods
- // ---------------------------------------------------------------
- /**
- * {@inheritDoc}
- */
- public Endpoint<Long> getLeftEndpoint() {
- return this.leftEndpoint;
- }
-
- /**
- * {@inheritDoc}
- */
- public Endpoint<Long> getRightEndpoint() {
- return this.rightEndpoint;
- }
-
- /**
- * {@inheritDoc}
- */
- public Long getStep() {
- return Long.valueOf(step);
- }
-
// iterable, iterator methods
// ---------------------------------------------------------------
/**
@@ -239,58 +199,8 @@
/**
* {@inheritDoc}
*/
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- */
public Iterator<Long> iterator() {
return this;
}
- // object methods
- // ---------------------------------------------------------------
- /**
- * {@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;
- }
-
}
diff --git a/core/src/main/java/org/apache/commons/functor/range/NumericRange.java b/core/src/main/java/org/apache/commons/functor/range/NumericRange.java
index 5ec2daf..862250c 100644
--- a/core/src/main/java/org/apache/commons/functor/range/NumericRange.java
+++ b/core/src/main/java/org/apache/commons/functor/range/NumericRange.java
@@ -16,8 +16,6 @@
*/
package org.apache.commons.functor.range;
-import java.util.Collection;
-
/**
* A base class for numeric ranges. The elements within this range must be a
* <b>Number</b> and <b>Comparable</b>.
@@ -31,7 +29,17 @@
* @since 0.1
* @version $Revision$ $Date$
*/
-public abstract class NumericRange<T extends Number & Comparable<?>> implements Range<T, T> {
+public abstract class NumericRange<T extends Number & Comparable<?>> extends AbstractRange<T, T> {
+
+ /**
+ * Construct a new {@link NumericRange}.
+ * @param leftEndpoint
+ * @param rightEndpoint
+ * @param step
+ */
+ protected NumericRange(Endpoint<T> leftEndpoint, Endpoint<T> rightEndpoint, T step) {
+ super(leftEndpoint, rightEndpoint, step);
+ }
/**
* {@inheritDoc}
@@ -92,19 +100,4 @@
return ((value - firstValue) / step + 1) % 1.0 == 0.0;
}
- /**
- * {@inheritDoc}
- */
- public boolean containsAll(Collection<T> col) {
- if (col == null || col.isEmpty()) {
- return false;
- }
- for (T t : col) {
- if (!contains(t)) {
- return false;
- }
- }
- return true;
- }
-
}