You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2013/09/12 06:29:48 UTC
svn commit: r1522355 [2/5] - in /commons/proper/functor/trunk:
core/src/main/java/org/apache/commons/functor/adapter/
core/src/main/java/org/apache/commons/functor/core/algorithm/
core/src/main/java/org/apache/commons/functor/generator/ core/src/main/j...
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/IntegerRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/IntegerRange.java?rev=1522355&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/IntegerRange.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/IntegerRange.java Thu Sep 12 04:29:46 2013
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.range;
+
+import java.util.Iterator;
+
+import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.lang3.Validate;
+
+/**
+ * A range of integers.
+ *
+ * @since 1.0
+ * @version $Revision: 1385335 $ $Date: 2012-09-16 15:08:31 -0300 (Sun, 16 Sep 2012) $
+ */
+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;
+
+ /**
+ * Current value.
+ */
+ private int currentValue;
+
+ /**
+ * Calculate default step.
+ */
+ public static final BinaryFunction<Integer, Integer, Integer> DEFAULT_STEP =
+ new BinaryFunction<Integer, Integer, Integer>() {
+
+ public Integer evaluate(Integer left, Integer right) {
+ return left > right ? -1 : 1;
+ }
+ };
+
+ // constructors
+ // ---------------------------------------------------------------
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param to end
+ */
+ public IntegerRange(Number from, Number to) {
+ this(from.intValue(), to.intValue());
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ */
+ public IntegerRange(Number from, Number to, Number step) {
+ this(from.intValue(), to.intValue(), step.intValue());
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param to end
+ */
+ public IntegerRange(int from, int to) {
+ this(from, to, DEFAULT_STEP.evaluate(from, to).intValue());
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ */
+ public IntegerRange(int from, int to, int step) {
+ this(from, DEFAULT_LEFT_BOUND_TYPE, to, DEFAULT_RIGHT_BOUND_TYPE, step);
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param to end
+ */
+ public IntegerRange(Endpoint<Integer> from, Endpoint<Integer> to) {
+ this(from.getValue(), from.getBoundType(), to.getValue(), to.getBoundType(),
+ DEFAULT_STEP.evaluate(from.getValue(), to.getValue()));
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ */
+ public IntegerRange(Endpoint<Integer> from, Endpoint<Integer> to, int step) {
+ this(from.getValue(), from.getBoundType(), to.getValue(), to.getBoundType(), step);
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ */
+ public IntegerRange(int from, BoundType leftBoundType, int to,
+ BoundType rightBoundType) {
+ this(from, leftBoundType, to, rightBoundType, DEFAULT_STEP.evaluate(from, to));
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @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);
+ }
+ if (this.leftEndpoint.getBoundType() == BoundType.CLOSED) {
+ this.currentValue = this.leftEndpoint.getValue();
+ } else {
+ this.currentValue = this.leftEndpoint.getValue() + this.step;
+ }
+ }
+
+ // range methods
+ // ---------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public Endpoint<Integer> getLeftEndpoint() {
+ return this.leftEndpoint;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Endpoint<Integer> getRightEndpoint() {
+ return this.rightEndpoint;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Integer getStep() {
+ return this.step;
+ }
+
+ // iterable, iterator methods
+ // ---------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public boolean hasNext() {
+ final int to = this.rightEndpoint.getValue();
+ if (step < 0) {
+ if (this.rightEndpoint.getBoundType() == BoundType.CLOSED) {
+ return this.currentValue >= to;
+ } else {
+ return this.currentValue > to;
+ }
+ } else {
+ if (this.rightEndpoint.getBoundType() == BoundType.CLOSED) {
+ return this.currentValue <= to;
+ } else {
+ return this.currentValue < to;
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Integer next() {
+ final int step = this.getStep();
+ final int r = this.currentValue;
+ this.currentValue += step;
+ return r;
+ }
+
+ /**
+ * {@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;
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/LongRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/LongRange.java?rev=1522355&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/LongRange.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/LongRange.java Thu Sep 12 04:29:46 2013
@@ -0,0 +1,297 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.range;
+
+import java.util.Iterator;
+
+import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.lang3.Validate;
+
+/**
+ * A range of longs.
+ *
+ * @since 1.0
+ * @version $Revision: 1385335 $ $Date: 2012-09-16 15:08:31 -0300 (Sun, 16 Sep 2012) $
+ */
+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;
+
+ /**
+ * Current value.
+ */
+ private long currentValue;
+
+ /**
+ * Calculate default step.
+ */
+ public static final BinaryFunction<Long, Long, Long> DEFAULT_STEP = new BinaryFunction<Long, Long, Long>() {
+
+ public Long evaluate(Long left, Long right) {
+ return left > right ? -1L : 1L;
+ }
+ };
+
+ // constructors
+ // ---------------------------------------------------------------
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param to end
+ */
+ public LongRange(Number from, Number to) {
+ this(from.longValue(), to.longValue());
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ */
+ public LongRange(Number from, Number to, Number step) {
+ this(from.longValue(), to.longValue(), step.longValue());
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param to end
+ */
+ public LongRange(long from, long to) {
+ this(from, to, DEFAULT_STEP.evaluate(from, to).longValue());
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ */
+ public LongRange(long from, long to, long step) {
+ this(from, DEFAULT_LEFT_BOUND_TYPE, to, DEFAULT_RIGHT_BOUND_TYPE, step);
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param to end
+ */
+ public LongRange(Endpoint<Long> from, Endpoint<Long> to) {
+ this(from.getValue(), from.getBoundType(), to.getValue(), to.getBoundType(),
+ DEFAULT_STEP.evaluate(from.getValue(), to.getValue()));
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ */
+ public LongRange(Endpoint<Long> from, Endpoint<Long> to, int step) {
+ this(from.getValue(), from.getBoundType(), to.getValue(), to.getBoundType(), step);
+ }
+
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ */
+ public LongRange(long from, BoundType leftBoundType, long to,
+ BoundType rightBoundType) {
+ this(from, leftBoundType, to, rightBoundType, DEFAULT_STEP.evaluate(from, to));
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ */
+ public LongRange(Endpoint<Long> from, Endpoint<Long> to, long step) {
+ this(from.getValue(), from.getBoundType(), to.getValue(), to.getBoundType(), step);
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @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);
+ }
+ if (this.leftEndpoint.getBoundType() == BoundType.CLOSED) {
+ this.currentValue = this.leftEndpoint.getValue();
+ } else {
+ this.currentValue = this.leftEndpoint.getValue() + this.step;
+ }
+ }
+
+ // range methods
+ // ---------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public Endpoint<Long> getLeftEndpoint() {
+ return this.leftEndpoint;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Endpoint<Long> getRightEndpoint() {
+ return this.rightEndpoint;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Long getStep() {
+ return this.step;
+ }
+
+ // iterable, iterator methods
+ // ---------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public boolean hasNext() {
+ final long to = this.rightEndpoint.getValue();
+ if (step < 0) {
+ if (this.rightEndpoint.getBoundType() == BoundType.CLOSED) {
+ return this.currentValue >= to;
+ } else {
+ return this.currentValue > to;
+ }
+ } else {
+ if (this.rightEndpoint.getBoundType() == BoundType.CLOSED) {
+ return this.currentValue <= to;
+ } else {
+ return this.currentValue < to;
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Long next() {
+ final long step = this.getStep();
+ final long r = this.currentValue;
+ this.currentValue += step;
+ return r;
+ }
+
+ /**
+ * {@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;
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/NumericRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/NumericRange.java?rev=1522355&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/NumericRange.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/NumericRange.java Thu Sep 12 04:29:46 2013
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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>.
+ *
+ * @param <T> the type of numbers and step that are both a number and comparable
+ * @see org.apache.commons.functor.range.IntegerRange
+ * @see org.apache.commons.functor.range.LongRange
+ * @see org.apache.commons.functor.range.FloatRange
+ * @see org.apache.commons.functor.range.DoubleRange
+ * @see org.apache.commons.functor.range.CharacterRange
+ * @since 0.1
+ * @version $Revision$ $Date$
+ */
+public abstract class NumericRange<T extends Number & Comparable<?>> implements Range<T, T> {
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isEmpty() {
+ double leftValue = this.getLeftEndpoint().getValue().doubleValue();
+ double rightValue = this.getRightEndpoint().getValue().doubleValue();
+ boolean closedLeft = this.getLeftEndpoint().getBoundType() == BoundType.CLOSED;
+ boolean closedRight = this.getRightEndpoint().getBoundType() == BoundType.CLOSED;
+ if (!closedLeft && !closedRight
+ && this.getLeftEndpoint().equals(this.getRightEndpoint())) {
+ return Boolean.TRUE;
+ }
+ double step = this.getStep().doubleValue();
+ if (step > 0.0) {
+ double firstValue = closedLeft ? leftValue : leftValue + step;
+ return closedRight ? firstValue > rightValue
+ : firstValue >= rightValue;
+ } else {
+ double firstValue = closedLeft ? leftValue : leftValue + step;
+ return closedRight ? firstValue < rightValue
+ : firstValue <= rightValue;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean contains(T obj) {
+ if (obj == null) {
+ return Boolean.FALSE;
+ }
+ double leftValue = this.getLeftEndpoint().getValue().doubleValue();
+ double rightValue = this.getRightEndpoint().getValue().doubleValue();
+ boolean includeLeft = this.getLeftEndpoint().getBoundType() == BoundType.CLOSED;
+ boolean includeRight = this.getRightEndpoint().getBoundType() == BoundType.CLOSED;
+ double step = this.getStep().doubleValue();
+ double value = obj.doubleValue();
+
+ double firstValue = 0;
+ double lastValue = 0;
+
+ if (step < 0.0) {
+ firstValue = includeLeft ? leftValue : leftValue + step;
+ lastValue = includeRight ? rightValue : Math.nextUp(rightValue);
+ if (value > firstValue || value < lastValue) {
+ return Boolean.FALSE;
+ }
+ } else {
+ firstValue = includeLeft ? leftValue : leftValue + step;
+ lastValue = includeRight ? rightValue : rightValue
+ - (rightValue - Math
+ .nextUp(rightValue));
+ if (value < firstValue || value > lastValue) {
+ return Boolean.FALSE;
+ }
+ }
+ return ((value - firstValue) / step + 1) % 1.0 == 0.0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean containsAll(Collection<T> col) {
+ if (col == null || col.size() == 0) {
+ return Boolean.FALSE;
+ }
+ boolean r = Boolean.TRUE;
+ for (T t : col) {
+ if (!this.contains(t)) {
+ r = Boolean.FALSE;
+ break;
+ }
+ }
+ return r;
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/Range.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/Range.java?rev=1522355&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/Range.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/Range.java Thu Sep 12 04:29:46 2013
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.range;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+
+/**
+ * Represent an interval of elements that varies from the <b>left limit</b>
+ * to the <b>right limit</b>. Each limit in this range is an {@link Endpoint
+ * Endpoint}. The left and the right limits can be <b>inclusive</b>
+ * (<b>bounded</b>, <b>closed</b>) or <b>exclusive</b> (<b>unbounded</b>,
+ * <b>open</b>).
+ * <p>
+ * The difference between each element within this range is called <b>step</b>.
+ * The step can be positive or negative, displaying whether the range elements
+ * are ascending or descending.
+ *
+ * @param <T> the type of elements held by this range.
+ * @param <S> the type of the step of this range.
+ * @see org.apache.commons.functor.range.Endpoint
+ * @since 1.0
+ * @version $Revision$ $Date$
+ */
+public interface Range<T extends Comparable<?>, S extends Comparable<?>> extends Iterable<T>, Iterator<T> {
+
+ /**
+ * Default left bound type.
+ */
+ BoundType DEFAULT_LEFT_BOUND_TYPE = BoundType.CLOSED;
+
+ /**
+ * Default right bound type.
+ */
+ BoundType DEFAULT_RIGHT_BOUND_TYPE = BoundType.OPEN;
+
+ /**
+ * Get the left limit of this range.
+ *
+ * @return Endpoint
+ */
+ Endpoint<T> getLeftEndpoint();
+
+ /**
+ * Get the right limit of this range.
+ *
+ * @return Endpoint
+ */
+ Endpoint<T> getRightEndpoint();
+
+ /**
+ * Get the step between elements of this range.
+ *
+ * @return Number
+ */
+ S getStep();
+
+ /**
+ * Returns <code>true</code> if this range is empty.
+ *
+ * @return <code>true</code> if this range is empty
+ */
+ boolean isEmpty();
+
+ /**
+ * Returns <code>true</code> if this range contains the specified element.
+ *
+ * @param obj element whose presence is being tested in this range
+ * @return <code>true</code> if this range contains the specified element
+ */
+ boolean contains(T obj);
+
+ /**
+ * Returns <code>true</code> is this range contains all of the elements in
+ * the specified collection.
+ *
+ * @param col collection to be checked for the containment in this range
+ * @return <code>true</code> if this range contains all of the elements in
+ * the specified collection
+ */
+ boolean containsAll(Collection<T> col);
+}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/Ranges.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/Ranges.java?rev=1522355&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/Ranges.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/Ranges.java Thu Sep 12 04:29:46 2013
@@ -0,0 +1,468 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.range;
+
+/**
+ * Range factory.
+ *
+ * @since 1.0
+ * @version $Revision$ $Date$
+ */
+public final class Ranges {
+
+ /**
+ * Hidden constructor as this only is a helper class with static methods.
+ */
+ private Ranges() {
+ }
+
+ // Integer ranges
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param to end
+ * @return IntegerRange
+ */
+ public static IntegerRange integerRange(Number from, Number to) {
+ return new IntegerRange(from, to);
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ * @return IntegerRange
+ */
+ public static IntegerRange integerRange(Number from, Number to, Number step) {
+ return new IntegerRange(from, to, step);
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param to end
+ * @return IntegerRange
+ */
+ public static IntegerRange integerRange(int from, int to) {
+ return new IntegerRange(from, to);
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ * @return IntegerRange
+ */
+ public static IntegerRange integerRange(int from, int to, int step) {
+ return new IntegerRange(from, to, step);
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ * @return IntegerRange
+ */
+ public static IntegerRange integerRange(int from, BoundType leftBoundType,
+ int to, BoundType rightBoundType) {
+ return new IntegerRange(from, leftBoundType, to, rightBoundType);
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ * @param step increment
+ * @return IntegerRange
+ */
+ public static IntegerRange integerRange(int from, BoundType leftBoundType,
+ int to, BoundType rightBoundType,
+ int step) {
+ return new IntegerRange(from, leftBoundType, to, rightBoundType, step);
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param leftEndpoint start
+ * @param rightEndpoint end
+ * @param step increment
+ * @return IntegerRange
+ */
+ public static IntegerRange integerRange(Endpoint<Integer> leftEndpoint,
+ Endpoint<Integer> rightEndpoint,
+ int step) {
+ return new IntegerRange(leftEndpoint, rightEndpoint, step);
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param to end
+ * @return LongRange
+ */
+ public static LongRange longRange(Number from, Number to) {
+ return new LongRange(from, to);
+ }
+
+ /**
+ * Create a new IntegerRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ * @return LongRange
+ */
+ public static LongRange longRange(Number from, Number to, Number step) {
+ return new LongRange(from, to, step);
+ }
+
+ // Long ranges
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param to end
+ * @return LongRange
+ */
+ public static LongRange longRange(long from, long to) {
+ return new LongRange(from, to);
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ * @return LongRange
+ */
+ public static LongRange longRange(long from, long to, long step) {
+ return new LongRange(from, to, step);
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ * @return LongRange
+ */
+ public static LongRange longRange(long from, BoundType leftBoundType,
+ long to, BoundType rightBoundType) {
+ return new LongRange(from, leftBoundType, to, rightBoundType);
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ * @param step increment
+ * @return LongRange
+ */
+ public static LongRange longRange(long from, BoundType leftBoundType,
+ long to, BoundType rightBoundType,
+ long step) {
+ return new LongRange(from, leftBoundType, to, rightBoundType, step);
+ }
+
+ /**
+ * Create a new LongRange.
+ *
+ * @param leftEndpoint start
+ * @param rightEndpoint end
+ * @param step increment
+ * @return LongRange
+ */
+ public static LongRange longRange(Endpoint<Long> leftEndpoint,
+ Endpoint<Long> rightEndpoint,
+ long step) {
+ return new LongRange(leftEndpoint, rightEndpoint, step);
+ }
+
+ // Float ranges
+ /**
+ * Create a new FloatRange.
+ *
+ * @param from start
+ * @param to end
+ * @return FloatRange
+ */
+ public static FloatRange floatRange(Number from, Number to) {
+ return new FloatRange(from, to);
+ }
+
+ /**
+ * Create a new FloatRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ * @return FloatRange
+ */
+ public static FloatRange floatRange(Number from, Number to, Number step) {
+ return new FloatRange(from, to, step);
+ }
+
+ /**
+ * Create a new FloatRange.
+ *
+ * @param from start
+ * @param to end
+ * @return FloatRange
+ */
+ public static FloatRange floatRange(float from, float to) {
+ return new FloatRange(from, to);
+ }
+
+ /**
+ * Create a new FloatRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ * @return FloatRange
+ */
+ public static FloatRange floatRange(float from, float to, float step) {
+ return new FloatRange(from, to, step);
+ }
+
+ /**
+ * Create a new FloatRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ * @return FloatRange
+ */
+ public static FloatRange floatRange(float from, BoundType leftBoundType,
+ float to, BoundType rightBoundType) {
+ return new FloatRange(from, leftBoundType, to, rightBoundType);
+ }
+
+ /**
+ * Create a new FloatRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ * @param step increment
+ * @return FloatRange
+ */
+ public static FloatRange floatRange(float from, BoundType leftBoundType,
+ float to, BoundType rightBoundType,
+ float step) {
+ return new FloatRange(from, leftBoundType, to, rightBoundType, step);
+ }
+
+ /**
+ * Create a new FloatRange.
+ *
+ * @param leftEndpoint start
+ * @param rightEndpoint end
+ * @param step increment
+ * @return FloatRange
+ */
+ public static FloatRange floatRange(Endpoint<Float> leftEndpoint,
+ Endpoint<Float> rightEndpoint,
+ float step) {
+ return new FloatRange(leftEndpoint, rightEndpoint, step);
+ }
+
+ // Double ranges
+ /**
+ * Create a new DoubleRange.
+ *
+ * @param from start
+ * @param to end
+ * @return DoubleRange
+ */
+ public static DoubleRange doubleRange(Number from, Number to) {
+ return new DoubleRange(from, to);
+ }
+
+ /**
+ * Create a new DoubleRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ * @return DoubleRange
+ */
+ public static DoubleRange doubleRange(Number from, Number to, Number step) {
+ return new DoubleRange(from, to, step);
+ }
+
+ /**
+ * Create a new DoubleRange.
+ *
+ * @param from start
+ * @param to end
+ * @return DoubleRange
+ */
+ public static DoubleRange doubleRange(double from, double to) {
+ return new DoubleRange(from, to);
+ }
+
+ /**
+ * Create a new DoubleRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ * @return DoubleRange
+ */
+ public static DoubleRange doubleRange(double from, double to, double step) {
+ return new DoubleRange(from, to, step);
+ }
+
+ /**
+ * Create a new DoubleRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ * @param step increment
+ * @return DoubleRange
+ */
+ public static DoubleRange doubleRange(double from, BoundType leftBoundType,
+ double to, BoundType rightBoundType,
+ double step) {
+ return new DoubleRange(from, leftBoundType, to, rightBoundType, step);
+ }
+
+ /**
+ * Create a new DoubleRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ * @return DoubleRange
+ */
+ public static DoubleRange doubleRange(double from, BoundType leftBoundType,
+ double to, BoundType rightBoundType) {
+ return new DoubleRange(from, leftBoundType, to, rightBoundType);
+ }
+
+ /**
+ * Create a new DoubleRange.
+ *
+ * @param leftEndpoint start
+ * @param rightEndpoint end
+ * @param step increment
+ * @return DoubleRange
+ */
+ public static DoubleRange doubleRange(Endpoint<Double> leftEndpoint,
+ Endpoint<Double> rightEndpoint,
+ double step) {
+ return new DoubleRange(leftEndpoint, rightEndpoint, step);
+ }
+
+ // Character ranges
+ /**
+ * Create a new CharacterRange.
+ *
+ * @param from start
+ * @param to end
+ * @return CharacterRange
+ */
+ public static CharacterRange characterRange(char from, char to) {
+ return new CharacterRange(from, to);
+ }
+
+ /**
+ * Create a new CharacterRange.
+ *
+ * @param from start
+ * @param to end
+ * @param step increment
+ * @return CharacterRange
+ */
+ public static CharacterRange characterRange(char from, char to, int step) {
+ return new CharacterRange(from, to, step);
+ }
+
+ /**
+ * Create a new CharacterRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ * @return CharacterRange
+ */
+ public static CharacterRange characterRange(char from,
+ BoundType leftBoundType,
+ char to,
+ BoundType rightBoundType) {
+ return new CharacterRange(from, leftBoundType, to, rightBoundType);
+ }
+
+ /**
+ * Create a new CharacterRange.
+ *
+ * @param from start
+ * @param leftBoundType type of left bound
+ * @param to end
+ * @param rightBoundType type of right bound
+ * @param step increment
+ * @return CharacterRange
+ */
+ public static CharacterRange characterRange(char from,
+ BoundType leftBoundType,
+ char to,
+ BoundType rightBoundType,
+ int step) {
+ return new CharacterRange(from, leftBoundType, to, rightBoundType, step);
+ }
+
+ /**
+ * Create a new CharacterRange.
+ *
+ * @param leftEndpoint start
+ * @param rightEndpoint end
+ * @param step increment
+ * @return CharacterRange
+ */
+ public static CharacterRange characterRange(Endpoint<Character> leftEndpoint,
+ Endpoint<Character> rightEndpoint,
+ int step) {
+ return new CharacterRange(leftEndpoint, rightEndpoint, step);
+ }
+}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/package-info.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/package-info.java?rev=1522355&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/package-info.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/range/package-info.java Thu Sep 12 04:29:46 2013
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * <p>
+ * Contains code related to Ranges.
+ * </p>
+ */
+package org.apache.commons.functor.range;
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/TestAlgorithms.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/TestAlgorithms.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/TestAlgorithms.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/TestAlgorithms.java Thu Sep 12 04:29:46 2013
@@ -31,10 +31,10 @@ import java.util.Set;
import org.apache.commons.functor.core.Identity;
import org.apache.commons.functor.core.composite.Not;
import org.apache.commons.functor.generator.FilteredGenerator;
-import org.apache.commons.functor.generator.Generator;
-import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
-import org.apache.commons.functor.generator.TransformedGenerator;
-import org.apache.commons.functor.generator.util.IntegerRange;
+import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
+import org.apache.commons.functor.generator.loop.LoopGenerator;
+import org.apache.commons.functor.generator.loop.TransformedGenerator;
+import org.apache.commons.functor.range.IntegerRange;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -116,7 +116,7 @@ public class TestAlgorithms {
@Test
public void testApplyToGenerator() {
- Generator<Integer> gen = new IntegerRange(1,5);
+ LoopGenerator<Integer> gen = IteratorToGeneratorAdapter.adapt(new IntegerRange(1,5));
Summer summer = new Summer();
new TransformedGenerator<Integer, Integer>(gen, new Doubler()).run(summer);
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFindWithinGenerator.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFindWithinGenerator.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFindWithinGenerator.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFindWithinGenerator.java Thu Sep 12 04:29:46 2013
@@ -28,7 +28,7 @@ import org.apache.commons.functor.BaseFu
import org.apache.commons.functor.Predicate;
import org.apache.commons.functor.adapter.LeftBoundPredicate;
import org.apache.commons.functor.core.IsEqual;
-import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
+import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
import org.junit.Test;
/**
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldLeft.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldLeft.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldLeft.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldLeft.java Thu Sep 12 04:29:46 2013
@@ -25,7 +25,7 @@ import java.util.List;
import org.apache.commons.functor.BaseFunctorTest;
import org.apache.commons.functor.BinaryFunction;
-import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
+import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
import org.junit.Test;
/**
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldRight.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldRight.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldRight.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldRight.java Thu Sep 12 04:29:46 2013
@@ -25,7 +25,7 @@ import java.util.List;
import org.apache.commons.functor.BaseFunctorTest;
import org.apache.commons.functor.BinaryFunction;
-import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
+import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
import org.junit.Test;
/**
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestGeneratorContains.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestGeneratorContains.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestGeneratorContains.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestGeneratorContains.java Thu Sep 12 04:29:46 2013
@@ -27,7 +27,7 @@ import org.apache.commons.functor.Predic
import org.apache.commons.functor.adapter.LeftBoundPredicate;
import org.apache.commons.functor.core.IsEqual;
import org.apache.commons.functor.core.algorithm.GeneratorContains;
-import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
+import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
import org.junit.Test;
/**
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestIndexOfInGenerator.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestIndexOfInGenerator.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestIndexOfInGenerator.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/algorithm/TestIndexOfInGenerator.java Thu Sep 12 04:29:46 2013
@@ -25,7 +25,7 @@ import org.apache.commons.functor.BaseFu
import org.apache.commons.functor.Predicate;
import org.apache.commons.functor.adapter.LeftBoundPredicate;
import org.apache.commons.functor.core.IsEqual;
-import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
+import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
import org.junit.Test;
/**
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/QuicksortExample.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/QuicksortExample.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/QuicksortExample.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/QuicksortExample.java Thu Sep 12 04:29:46 2013
@@ -33,7 +33,7 @@ import org.apache.commons.functor.core.c
import org.apache.commons.functor.core.comparator.IsLessThan;
import org.apache.commons.functor.core.composite.ConditionalFunction;
import org.apache.commons.functor.generator.FilteredGenerator;
-import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
+import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
import org.junit.Test;
/*
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/kata/two/TestBinaryChop.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/kata/two/TestBinaryChop.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/kata/two/TestBinaryChop.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/kata/two/TestBinaryChop.java Thu Sep 12 04:29:46 2013
@@ -18,7 +18,9 @@ package org.apache.commons.functor.examp
import static org.junit.Assert.assertEquals;
+import java.util.ArrayList;
import java.util.Collections;
+import java.util.Iterator;
import java.util.List;
import org.apache.commons.functor.NullaryFunction;
@@ -26,7 +28,7 @@ import org.apache.commons.functor.Nullar
import org.apache.commons.functor.NullaryProcedure;
import org.apache.commons.functor.core.algorithm.RecursiveEvaluation;
import org.apache.commons.functor.core.algorithm.UntilDo;
-import org.apache.commons.functor.generator.util.IntegerRange;
+import org.apache.commons.functor.range.IntegerRange;
import org.junit.Test;
/**
@@ -102,7 +104,10 @@ public class TestBinaryChop {
assertEquals(-1, chopper.find(6, new int[] { 1, 3, 5, 7 }));
assertEquals(-1, chopper.find(8, new int[] { 1, 3, 5, 7 }));
- List<Integer> largeList = (List<Integer>) (new IntegerRange(0, 100001).toCollection());
+ List<Integer> largeList = new ArrayList<Integer>();
+ Iterator<Integer> ints = new IntegerRange(0, 100001);
+ while(ints.hasNext())
+ largeList.add(ints.next());
assertEquals(-1, chopper.find(new Integer(-5), largeList));
assertEquals(100000, chopper.find(new Integer(100000), largeList));
assertEquals(0, chopper.find(new Integer(0), largeList));
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/lines/Lines.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/lines/Lines.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/lines/Lines.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/lines/Lines.java Thu Sep 12 04:29:46 2013
@@ -23,12 +23,12 @@ import java.io.FileReader;
import java.io.Reader;
import org.apache.commons.functor.Procedure;
-import org.apache.commons.functor.generator.BaseGenerator;
+import org.apache.commons.functor.generator.loop.LoopGenerator;
/**
* @version $Revision$ $Date$
*/
-public class Lines extends BaseGenerator<String> {
+public class Lines extends LoopGenerator<String> {
public static Lines from(Reader reader) {
return new Lines(reader);
}
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/lines/TestLines.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/lines/TestLines.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/lines/TestLines.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/lines/TestLines.java Thu Sep 12 04:29:46 2013
@@ -31,7 +31,7 @@ import org.apache.commons.functor.core.c
import org.apache.commons.functor.core.composite.And;
import org.apache.commons.functor.core.composite.Not;
import org.apache.commons.functor.generator.FilteredGenerator;
-import org.apache.commons.functor.generator.TransformedGenerator;
+import org.apache.commons.functor.generator.loop.TransformedGenerator;
/**
* @version $Revision$ $Date$
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/map/FixedSizeMap.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/map/FixedSizeMap.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/map/FixedSizeMap.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/example/map/FixedSizeMap.java Thu Sep 12 04:29:46 2013
@@ -24,7 +24,7 @@ import org.apache.commons.functor.Binary
import org.apache.commons.functor.adapter.BinaryProcedureBinaryFunction;
import org.apache.commons.functor.core.algorithm.GeneratorContains;
import org.apache.commons.functor.core.composite.Not;
-import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
+import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
/**
* @version $Revision$ $Date$
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java Thu Sep 12 04:29:46 2013
@@ -47,9 +47,6 @@ public class TestBaseGenerator {
public void run(Procedure<? super Integer> proc) {
for (int i=0;i<5;i++) {
proc.run(new Integer(i));
- if (isStopped()) {
- break;
- }
}
}
};
@@ -95,60 +92,6 @@ public class TestBaseGenerator {
assertEquals("01234", result.toString());
}
- @Test
- public void testStop() {
- final StringBuffer result = new StringBuffer();
- simpleGenerator.run(new Procedure<Integer>() {
- int i=0;
- public void run(Integer obj) {
- result.append(obj);
- if (i++ > 1) {
- simpleGenerator.stop();
- }
- }
- });
-
- assertEquals("012", result.toString());
- }
-
- @Test
- public void testWrappingGenerator() {
- final StringBuffer result = new StringBuffer();
- final Generator<Integer> gen = new BaseGenerator<Integer>(simpleGenerator) {
- public void run(final Procedure<? super Integer> proc) {
- Generator<Integer> wrapped = (Generator<Integer>)getWrappedGenerator();
- assertSame(simpleGenerator, wrapped);
- wrapped.run(new Procedure<Integer>() {
- public void run(Integer obj) {
- proc.run(new Integer(obj.intValue() + 1));
- }
- });
- }
- };
-
- gen.run(new Procedure<Integer>() {
- public void run(Integer obj) {
- result.append(obj);
- }
- });
-
- assertEquals("12345", result.toString());
-
- // try to stop the wrapped generator
- final StringBuffer result2 = new StringBuffer();
- gen.run(new Procedure<Integer>() {
- int i=0;
- public void run(Integer obj) {
- result2.append(obj);
- if (i++ > 1) {
- gen.stop();
- }
- }
- });
-
- assertEquals("123", result2.toString());
- }
-
// Tests
// ------------------------------------------------------------------------
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java?rev=1522355&r1=1522354&r2=1522355&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java Thu Sep 12 04:29:46 2013
@@ -25,7 +25,8 @@ import java.util.List;
import org.apache.commons.functor.Predicate;
import org.apache.commons.functor.Procedure;
-import org.apache.commons.functor.generator.util.IntegerRange;
+import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
+import org.apache.commons.functor.range.IntegerRange;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -39,7 +40,7 @@ public class TestFilteredGenerator
@Before
public void setUp() throws Exception {
- wrappedGenerator = new IntegerRange(1, 10);
+ wrappedGenerator = IteratorToGeneratorAdapter.adapt(new IntegerRange(1, 10));
filteredGenerator = new FilteredGenerator<Integer>(wrappedGenerator, isEven);
}
@@ -70,13 +71,15 @@ public class TestFilteredGenerator
@Test
public void testEquals() {
- Generator<Integer> anotherGenerate = new FilteredGenerator<Integer>(new IntegerRange(1, 10), isEven);
+ Generator<Integer> anotherGenerate = new FilteredGenerator<Integer>(
+ IteratorToGeneratorAdapter.adapt(new IntegerRange(1, 10)), isEven);
assertEquals(filteredGenerator, filteredGenerator);
assertEquals(filteredGenerator, anotherGenerate);
assertTrue(!filteredGenerator.equals((FilteredGenerator<Integer>)null));
Generator<Integer> aGenerateWithADifferentPredicate = new FilteredGenerator<Integer>(
- new IntegerRange(1, 10), new Predicate<Integer>() {
+ IteratorToGeneratorAdapter.adapt(new IntegerRange(1, 10)),
+ new Predicate<Integer>() {
public boolean test(Integer obj) {
return obj % 2 == 0;
}
@@ -84,7 +87,8 @@ public class TestFilteredGenerator
assertTrue(!filteredGenerator.equals(aGenerateWithADifferentPredicate));
- Generator<Integer> aGenerateWithADifferentWrapped = new FilteredGenerator<Integer>(new IntegerRange(1,11), isEven);
+ Generator<Integer> aGenerateWithADifferentWrapped = new FilteredGenerator<Integer>(
+ IteratorToGeneratorAdapter.adapt(new IntegerRange(1,11)), isEven);
assertTrue(!filteredGenerator.equals(aGenerateWithADifferentWrapped));
}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestGenerateUntil.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestGenerateUntil.java?rev=1522355&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestGenerateUntil.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestGenerateUntil.java Thu Sep 12 04:29:46 2013
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.generator.loop;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.functor.Predicate;
+import org.apache.commons.functor.generator.Generator;
+import org.apache.commons.functor.generator.loop.GenerateUntil;
+import org.apache.commons.functor.range.IntegerRange;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests the Generate Until class.
+ * @version $Revision: 1508677 $ $Date: 2013-07-30 19:48:02 -0300 (Tue, 30 Jul 2013) $
+ */
+public class TestGenerateUntil
+{
+
+ @Before
+ public void setUp() throws Exception {
+ wrappedGenerator = IteratorToGeneratorAdapter.adapt(new IntegerRange(1, 10));
+ generateUntil = new GenerateUntil<Integer>(wrappedGenerator, isMoreThanFive);
+ }
+
+ @After
+ public void tearDown() {
+ wrappedGenerator = null;
+ isMoreThanFive = null;
+ generateUntil = null;
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test(expected=NullPointerException.class)
+ public void testConstructorProhibitsNullWrappedPredicate() {
+ new GenerateUntil<Integer>(generateUntil, null);
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testConstructorProhibitsNullGenerator() {
+ new GenerateUntil<Integer>(null, isMoreThanFive);
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testConstructorProhibitsNullGeneratorOrNullWrappedPredicate() {
+ new GenerateUntil<Integer>(null, null);
+ }
+
+ @Test
+ public void testEquals() {
+ Generator<Integer> anotherGenerate = new GenerateUntil<Integer>(
+ IteratorToGeneratorAdapter.adapt(new IntegerRange(1, 10)), isMoreThanFive);
+ assertEquals(generateUntil, generateUntil);
+ assertEquals(generateUntil, anotherGenerate);
+ assertTrue(!generateUntil.equals((GenerateUntil<Integer>)null));
+
+ Generator<Integer> aGenerateWithADifferentPredicate = new GenerateUntil<Integer>(
+ IteratorToGeneratorAdapter.adapt(new IntegerRange(1, 10)),
+ new Predicate<Integer>() {
+ public boolean test(Integer obj) {
+ return obj > FIVE;
+ }
+ });
+ assertTrue(!generateUntil.equals(aGenerateWithADifferentPredicate));
+
+ Generator<Integer> aGenerateWithADifferentWrapped = new GenerateUntil<Integer>(
+ IteratorToGeneratorAdapter.adapt(new IntegerRange(1,2)), isMoreThanFive);
+ assertTrue(!generateUntil.equals(aGenerateWithADifferentWrapped));
+ }
+
+ @Test
+ public void testHashcode() {
+ assertEquals(generateUntil.hashCode(), generateUntil.hashCode());
+ assertEquals(generateUntil.hashCode(), new GenerateUntil<Integer>(wrappedGenerator, isMoreThanFive).hashCode());
+ }
+
+ // Attributes
+ // ------------------------------------------------------------------------
+ private static final Integer FIVE = new Integer(5);
+
+ private Generator<Integer> wrappedGenerator = null;
+ private Predicate<Integer> isMoreThanFive = new Predicate<Integer>() {
+ public boolean test( Integer obj ) {
+ return obj > FIVE;
+ }
+ };
+ private Generator<Integer> generateUntil = null;
+}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestGenerateWhile.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestGenerateWhile.java?rev=1522355&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestGenerateWhile.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestGenerateWhile.java Thu Sep 12 04:29:46 2013
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.generator.loop;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.functor.Predicate;
+import org.apache.commons.functor.generator.Generator;
+import org.apache.commons.functor.generator.loop.GenerateWhile;
+import org.apache.commons.functor.range.IntegerRange;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests the Generate While class.
+ * @version $Revision: 1508677 $ $Date: 2013-07-30 19:48:02 -0300 (Tue, 30 Jul 2013) $
+ */
+public class TestGenerateWhile
+{
+
+ @Before
+ public void setUp() throws Exception {
+ wrappedGenerator = IteratorToGeneratorAdapter.adapt(new IntegerRange(1, 10));
+ generateWhile = new GenerateWhile<Integer>(wrappedGenerator, isLessThanFive);
+ }
+
+ @After
+ public void tearDown() {
+ wrappedGenerator = null;
+ isLessThanFive = null;
+ generateWhile = null;
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test(expected=NullPointerException.class)
+ public void testConstructorProhibitsNullPredicate() {
+ new GenerateWhile<Integer>(generateWhile, null);
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testConstructorProhibitsNullWrappedGenerator() {
+ new GenerateWhile<Integer>(null, isLessThanFive);
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testConstructorProhibitsNullPredicateOrNullWrappedGenerator() {
+ new GenerateWhile<Integer>(null, null);
+ }
+
+ @Test
+ public void testEquals() {
+ Generator<Integer> anotherGenerate = new GenerateWhile<Integer>(
+ IteratorToGeneratorAdapter.adapt(new IntegerRange(1, 10)), isLessThanFive);
+ assertEquals(generateWhile, generateWhile);
+ assertEquals(generateWhile, anotherGenerate);
+ assertTrue(!generateWhile.equals((GenerateWhile<Integer>)null));
+
+ Generator<Integer> aGenerateWithADifferentPredicate = new GenerateWhile<Integer>(
+ IteratorToGeneratorAdapter.adapt(new IntegerRange(1, 10)), new Predicate<Integer>() {
+ public boolean test(Integer obj) {
+ return obj < FIVE;
+ }
+ });
+
+ assertTrue(!generateWhile.equals(aGenerateWithADifferentPredicate));
+
+ Generator<Integer> aGenerateWithADifferentWrapped = new GenerateWhile<Integer>(
+ IteratorToGeneratorAdapter.adapt(new IntegerRange(1,11)), isLessThanFive);
+ assertTrue(!generateWhile.equals(aGenerateWithADifferentWrapped));
+ }
+
+ @Test
+ public void testHashcode() {
+ assertEquals(generateWhile.hashCode(), generateWhile.hashCode());
+ assertEquals(generateWhile.hashCode(), new GenerateWhile<Integer>(wrappedGenerator, isLessThanFive).hashCode());
+ }
+
+ // Attributes
+ // ------------------------------------------------------------------------
+ private static final Integer FIVE = new Integer(5);
+
+ private Generator<Integer> wrappedGenerator = null;
+ private Predicate<Integer> isLessThanFive = new Predicate<Integer>()
+ {
+ public boolean test( Integer obj ) {
+ return obj < FIVE;
+ }
+ };
+ private Generator<Integer> generateWhile = null;
+
+}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestIteratorToGeneratorAdapter.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestIteratorToGeneratorAdapter.java?rev=1522355&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestIteratorToGeneratorAdapter.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestIteratorToGeneratorAdapter.java Thu Sep 12 04:29:46 2013
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.generator.loop;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.generator.Generator;
+import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests the Iterator to Generator Adapter class.
+ * @version $Revision: 1363514 $ $Date: 2012-07-19 17:13:49 -0300 (Thu, 19 Jul 2012) $
+ */
+@SuppressWarnings("unchecked")
+public class TestIteratorToGeneratorAdapter extends BaseFunctorTest {
+
+ @Override
+ public Object makeFunctor() {
+ List<String> list = new ArrayList<String>();
+ list.add("1");
+ return new IteratorToGeneratorAdapter<String>(list.iterator());
+ }
+
+ // Lifecycle
+ // ------------------------------------------------------------------------
+
+ private List<String> list = null;
+
+ @Before
+ public void setUp() throws Exception {
+ list = new ArrayList<String>();
+ list.add("1");
+ list.add("two");
+ list.add("c");
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ list = null;
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testAdaptNull() {
+ assertNull(IteratorToGeneratorAdapter.adapt(null));
+ }
+
+ @Test
+ public void testAdaptNonNull() {
+ assertNotNull(IteratorToGeneratorAdapter.adapt(list.iterator()));
+ }
+
+ @Test
+ public void testGenerate() {
+ Iterator<String> iter = list.iterator();
+ Generator<String> gen = new IteratorToGeneratorAdapter<String>(iter);
+ List<String> list2 = new ArrayList<String>();
+ list2.addAll((Collection<String>)gen.toCollection());
+ assertEquals(list,list2);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testConstructNull() {
+ new IteratorToGeneratorAdapter<Object>(null);
+ }
+
+ @Test
+ public void testEquals() {
+ Iterator<String> iter = list.iterator();
+ Generator<String> gen = new IteratorToGeneratorAdapter<String>(iter);
+ assertObjectsAreEqual(gen,gen);
+ assertObjectsAreEqual(gen,new IteratorToGeneratorAdapter<String>(iter));
+ }
+}
\ No newline at end of file
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestLoopGenerator.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestLoopGenerator.java?rev=1522355&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestLoopGenerator.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestLoopGenerator.java Thu Sep 12 04:29:46 2013
@@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.generator.loop;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.functor.Procedure;
+import org.apache.commons.functor.generator.util.CollectionTransformer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests the Loop Generator class.
+ */
+@SuppressWarnings("unchecked")
+public class TestLoopGenerator {
+
+ private LoopGenerator<Integer> simpleGenerator = null;
+
+ // Lifecycle
+ // ------------------------------------------------------------------------
+
+ @Before
+ public void setUp() throws Exception {
+ simpleGenerator = new LoopGenerator<Integer>() {
+ public void run(Procedure<? super Integer> proc) {
+ for (int i=0;i<5;i++) {
+ proc.run(new Integer(i));
+ if (isStopped()) {
+ break;
+ }
+ }
+ }
+ };
+
+ list = new ArrayList<Integer>();
+ evens = new ArrayList<Integer>();
+ doubled = new ArrayList<Integer>();
+ listWithDuplicates = new ArrayList<Integer>();
+ sum = 0;
+ for (int i=0;i<10;i++) {
+ list.add(new Integer(i));
+ doubled.add(new Integer(i*2));
+ listWithDuplicates.add(new Integer(i));
+ listWithDuplicates.add(new Integer(i));
+ sum += i;
+ if (i%2 == 0) {
+ evens.add(new Integer(i));
+ }
+ }
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ simpleGenerator = null;
+ list = null;
+ evens = null;
+ listWithDuplicates = null;
+ sum = 0;
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testSimpleGenerator() {
+ final StringBuffer result = new StringBuffer();
+ simpleGenerator.run(new Procedure<Integer>() {
+ public void run(Integer obj) {
+ result.append(obj);
+ }
+ });
+
+ assertEquals("01234", result.toString());
+ }
+
+ @Test
+ public void testStop() {
+ final StringBuffer result = new StringBuffer();
+ simpleGenerator.run(new Procedure<Integer>() {
+ int i=0;
+ public void run(Integer obj) {
+ result.append(obj);
+ if (i++ > 1) {
+ simpleGenerator.stop();
+ }
+ }
+ });
+
+ assertEquals("012", result.toString());
+ }
+
+ @Test
+ public void testWrappingGenerator() {
+ final StringBuffer result = new StringBuffer();
+ final LoopGenerator<Integer> gen = new LoopGenerator<Integer>(simpleGenerator) {
+ public void run(final Procedure<? super Integer> proc) {
+ LoopGenerator<Integer> wrapped = (LoopGenerator<Integer>)getWrappedGenerator();
+ assertSame(simpleGenerator, wrapped);
+ wrapped.run(new Procedure<Integer>() {
+ public void run(Integer obj) {
+ proc.run(new Integer(obj.intValue() + 1));
+ }
+ });
+ }
+ };
+
+ gen.run(new Procedure<Integer>() {
+ public void run(Integer obj) {
+ result.append(obj);
+ }
+ });
+
+ assertEquals("12345", result.toString());
+
+ // try to stop the wrapped generator
+ final StringBuffer result2 = new StringBuffer();
+ gen.run(new Procedure<Integer>() {
+ int i=0;
+ public void run(Integer obj) {
+ result2.append(obj);
+ if (i++ > 1) {
+ gen.stop();
+ }
+ }
+ });
+
+ assertEquals("123", result2.toString());
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testTo() {
+ Collection<Integer> col = simpleGenerator.to(CollectionTransformer.<Integer> toCollection());
+ assertEquals("[0, 1, 2, 3, 4]", col.toString());
+
+ Collection<Integer> fillThis = new LinkedList<Integer>();
+ col = simpleGenerator.to(new CollectionTransformer<Integer, Collection<Integer>>(fillThis));
+ assertSame(fillThis, col);
+ assertEquals("[0, 1, 2, 3, 4]", col.toString());
+
+ col = (Collection<Integer>)simpleGenerator.toCollection();
+ assertEquals("[0, 1, 2, 3, 4]", col.toString());
+ assertEquals("[0, 1, 2, 3, 4]", col.toString());
+
+ fillThis = new LinkedList<Integer>();
+ col = (Collection<Integer>)simpleGenerator.to(fillThis);
+ assertSame(fillThis, col);
+ assertEquals("[0, 1, 2, 3, 4]", col.toString());
+ }
+
+ // Attributes
+ // ------------------------------------------------------------------------
+ private List<Integer> list = null;
+ private List<Integer> doubled = null;
+ private List<Integer> evens = null;
+ private List<Integer> listWithDuplicates = null;
+ @SuppressWarnings("unused")
+ private int sum = 0;
+// private UnaryPredicate equalsThree = LeftBoundPredicate.bind(IsEqual.instance(),new Integer(3));
+// private UnaryPredicate equalsTwentyThree = LeftBoundPredicate.bind(IsEqual.instance(),new Integer(23));
+// private UnaryPredicate isEven = new UnaryPredicate() {
+// public boolean test(Object obj) {
+// return ((Number) obj).intValue() % 2 == 0;
+// }
+// };
+// private UnaryPredicate isOdd = new UnaryPredicate() {
+// public boolean test(Object obj) {
+// return ((Number) obj).intValue() % 2 != 0;
+// }
+// };
+
+ // Classes
+ // ------------------------------------------------------------------------
+
+ static class Summer implements Procedure<Number> {
+ public void run(Number that) {
+ sum += (that).intValue();
+ }
+ public int sum = 0;
+ }
+}
\ No newline at end of file
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestTransformedGenerator.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestTransformedGenerator.java?rev=1522355&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestTransformedGenerator.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/generator/loop/TestTransformedGenerator.java Thu Sep 12 04:29:46 2013
@@ -0,0 +1,125 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.generator.loop;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.commons.functor.Function;
+import org.apache.commons.functor.Procedure;
+import org.apache.commons.functor.generator.Generator;
+import org.apache.commons.functor.generator.loop.TransformedGenerator;
+import org.apache.commons.functor.range.IntegerRange;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests the Transformed Generator class.
+ * @version $Revision: 1508677 $ $Date: 2013-07-30 19:48:02 -0300 (Tue, 30 Jul 2013) $
+ */
+public class TestTransformedGenerator
+{
+
+ @Before
+ public void setUp() throws Exception {
+ wrappedGenerator = IteratorToGeneratorAdapter.adapt(new IntegerRange(1, 10));
+ sumsTwoGenerator = new TransformedGenerator<Integer, Integer>(wrappedGenerator, sumsTwo);
+ }
+
+ @After
+ public void tearDown() {
+ wrappedGenerator = null;
+ sumsTwo = null;
+ sumsTwoGenerator = null;
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test(expected=NullPointerException.class)
+ public void testConstructorProhibitsNullWrappedGenerator() {
+ new TransformedGenerator<Integer, Integer>(null, sumsTwo);
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testConstructorProhibitsNullFunction() {
+ new TransformedGenerator<Integer, Integer>(wrappedGenerator, null);
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testConstructorProhibitsNullWrappedGeneratorOrNullFunction() {
+ new TransformedGenerator<Integer, Integer>(null, null);
+ }
+
+ @Test
+ public void testEquals() {
+ TransformedGenerator<Integer, Integer> anotherTransformedGenerator =
+ new TransformedGenerator<Integer, Integer>(wrappedGenerator, sumsTwo);
+ assertEquals(sumsTwoGenerator, sumsTwoGenerator);
+ assertEquals(sumsTwoGenerator, anotherTransformedGenerator);
+ assertTrue(!sumsTwoGenerator.equals((TransformedGenerator<Integer, Integer>)null));
+
+ TransformedGenerator<Integer, Integer> aGenerateWithADifferentFunction =
+ new TransformedGenerator<Integer, Integer>(wrappedGenerator, new Function<Integer, Integer>() {
+ public Integer evaluate( Integer obj ) {
+ return obj;
+ }
+ });
+ assertTrue( !sumsTwoGenerator.equals(aGenerateWithADifferentFunction));
+
+ TransformedGenerator<Integer, Integer> aTransformedGeneratorWithADifferentWrapped =
+ new TransformedGenerator<Integer, Integer>(
+ IteratorToGeneratorAdapter.adapt(new IntegerRange(1,2)), sumsTwo);
+ assertTrue(!sumsTwoGenerator.equals(aTransformedGeneratorWithADifferentWrapped));
+ }
+
+ @Test
+ public void testHashcode() {
+ assertEquals(sumsTwoGenerator.hashCode(), sumsTwoGenerator.hashCode());
+ assertEquals(sumsTwoGenerator.hashCode(), new TransformedGenerator<Integer, Integer>(wrappedGenerator, sumsTwo).hashCode());
+ }
+
+ @Test
+ public void testGenerate() {
+ final List<Integer> doubledValues = new ArrayList<Integer>();
+ sumsTwoGenerator.run(new Procedure<Integer>() {
+ public void run( Integer obj ) {
+ doubledValues.add(obj);
+ }
+ });
+
+ final List<Integer> expected = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10 , 11);
+
+ assertEquals(9, doubledValues.size());
+ assertEquals(expected, doubledValues);
+ }
+
+ // Attributes
+ // ------------------------------------------------------------------------
+ private static final Integer TWO = new Integer(2);
+
+ private Generator<Integer> wrappedGenerator = null;
+ private Function<Integer, Integer> sumsTwo = new Function<Integer, Integer>() {
+ public Integer evaluate( Integer obj ) {
+ return obj += TWO;
+ }
+ };
+ private TransformedGenerator<Integer, Integer> sumsTwoGenerator = null;
+
+}