commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwaldh...@apache.org
Subject cvs commit: jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/generator/util LongRange.java IntegerRange.java
Date Mon, 24 Nov 2003 23:59:21 GMT
rwaldhoff    2003/11/24 15:59:21

  Modified:    functor/src/test/org/apache/commons/functor/generator/util
                        TestIntegerRange.java TestAll.java
               functor/src/java/org/apache/commons/functor/generator/util
                        IntegerRange.java
  Added:       functor/src/test/org/apache/commons/functor/generator/util
                        TestLongRange.java
               functor/src/java/org/apache/commons/functor/generator/util
                        LongRange.java
  Log:
  * add LongRange and tests
  * make range functions exclusive of the "to" endpoint, so that things like IntegerRange(Integer.MIN_VALUE,Integer.MAX_VALUE)
can actually complete
  
  Revision  Changes    Path
  1.2       +12 -12    jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestIntegerRange.java
  
  Index: TestIntegerRange.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestIntegerRange.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestIntegerRange.java	24 Nov 2003 23:39:17 -0000	1.1
  +++ TestIntegerRange.java	24 Nov 2003 23:59:21 -0000	1.2
  @@ -124,34 +124,34 @@
   
       public void testObjectConstructor() {
           IntegerRange range = new IntegerRange(new Integer(0), new Integer(5));
  -        assertEquals("[0, 1, 2, 3, 4, 5]", range.toCollection().toString());
  +        assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
           range = new IntegerRange(new Integer(0), new Integer(5), new Integer(1));
  -        assertEquals("[0, 1, 2, 3, 4, 5]", range.toCollection().toString());
  +        assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
       }
   
   
       public void testReverseStep() {
           IntegerRange range = new IntegerRange(10, 0, -2);
  -        assertEquals("[10, 8, 6, 4, 2, 0]", range.toCollection().toString());
  -        assertEquals("[10, 8, 6, 4, 2, 0]", range.toCollection().toString());
  +        assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
  +        assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
       }
   
       public void testStep() {
           IntegerRange range = new IntegerRange(0, 10, 2);
  -        assertEquals("[0, 2, 4, 6, 8, 10]", range.toCollection().toString());
  -        assertEquals("[0, 2, 4, 6, 8, 10]", range.toCollection().toString());
  +        assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
  +        assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
       }
   
       public void testForwardRange() {
           IntegerRange range = new IntegerRange(0, 5);
  -        assertEquals("[0, 1, 2, 3, 4, 5]", range.toCollection().toString());
  -        assertEquals("[0, 1, 2, 3, 4, 5]", range.toCollection().toString());
  +        assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
  +        assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
       }
   
       public void testReverseRange() {
           IntegerRange range = new IntegerRange(5, 0);
  -        assertEquals("[5, 4, 3, 2, 1, 0]", range.toCollection().toString());
  -        assertEquals("[5, 4, 3, 2, 1, 0]", range.toCollection().toString());
  +        assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
  +        assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
       }
   
       public void testEquals() {
  
  
  
  1.3       +3 -2      jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestAll.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestAll.java	24 Nov 2003 23:39:17 -0000	1.2
  +++ TestAll.java	24 Nov 2003 23:59:21 -0000	1.3
  @@ -74,6 +74,7 @@
           suite.addTest(TestEachElement.suite());
           suite.addTest(TestNumberRange.suite());
           suite.addTest(TestIntegerRange.suite());
  +        suite.addTest(TestLongRange.suite());
           return suite;
       }
   }
  
  
  
  1.1                  jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestLongRange.java
  
  Index: TestLongRange.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestLongRange.java,v
1.1 2003/11/24 23:59:21 rwaldhoff Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.functor.generator.util;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.functor.BaseFunctorTest;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/11/24 23:59:21 $
   * @author Jason Horman (jason@jhorman.org)
   * @author Rodney Waldhoff
   */
  public class TestLongRange extends BaseFunctorTest {
      // Conventional
      // ------------------------------------------------------------------------
  
      public TestLongRange(String name) {
          super(name);
      }
  
      public static Test suite() {
          return new TestSuite(TestLongRange.class);
      }
  
      protected Object makeFunctor() throws Exception {
          return new LongRange(10, 20);
      }
  
      // Tests
      // ------------------------------------------------------------------------
  
      public void testStepChecking() {
          {
              new LongRange(2, 2, 0); // step of 0 is ok when range is empty
          }
          {
              new LongRange(2, 2, 1); // positive step is ok when range is empty
          }
          {
              new LongRange(2, 2, -1); // negative step is ok when range is empty
          }
          {
              new LongRange(0, 1, 10); // big steps are ok
          }
          {
              new LongRange(1, 0, -10); // big steps are ok
          }
          try {
              new LongRange(0, 1, 0);
              fail("Expected IllegalArgumentException");
          } catch(IllegalArgumentException e) {
              // expected 
          }  
          try {
              new LongRange(0, 1, -1);
              fail("Expected IllegalArgumentException");
          } catch(IllegalArgumentException e) {
              // expected 
          }  
          try {
              new LongRange(0, -1, 1);
              fail("Expected IllegalArgumentException");
          } catch(IllegalArgumentException e) {
              // expected 
          }  
      }
  
      public void testObjectConstructor() {
          LongRange range = new LongRange(new Long(0), new Long(5));
          assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
          range = new LongRange(new Integer(0), new Long(5), new Long(1));
          assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
      }
  
  
      public void testReverseStep() {
          LongRange range = new LongRange(10, 0, -2);
          assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
          assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
      }
  
      public void testStep() {
          LongRange range = new LongRange(0, 10, 2);
          assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
          assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
      }
  
      public void testForwardRange() {
          LongRange range = new LongRange(0, 5);
          assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
          assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
      }
  
      public void testReverseRange() {
          LongRange range = new LongRange(5, 0);
          assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
          assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
      }
  
      public void testLongRange() {
          LongRange range = new LongRange(Long.MAX_VALUE - 3L, Long.MAX_VALUE);
          assertEquals("[9223372036854775804, 9223372036854775805, 9223372036854775806]",
range.toCollection().toString());
          assertEquals("[9223372036854775804, 9223372036854775805, 9223372036854775806]",
range.toCollection().toString());
      }
  
      public void testEquals() {
          LongRange range = new LongRange(1, 5);
          assertObjectsAreEqual(range, range);
          assertObjectsAreEqual(range, new LongRange(1, 5));
          assertObjectsAreEqual(range, new LongRange(1, 5, 1));
          assertObjectsAreEqual(range, new LongRange(new Integer(1), new Long(5)));
          assertObjectsAreEqual(range, new LongRange(new Long(1), new Short((short)5), new
Long(1)));
      }
  
  }
  
  
  1.2       +6 -4      jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/generator/util/IntegerRange.java
  
  Index: IntegerRange.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/generator/util/IntegerRange.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- IntegerRange.java	24 Nov 2003 23:39:17 -0000	1.1
  +++ IntegerRange.java	24 Nov 2003 23:59:21 -0000	1.2
  @@ -62,6 +62,8 @@
   
   
   /**
  + * A generator for the range <i>from</i> (inclusive) to <i>to</i>
(exclusive).
  + *
    * @since 1.0
    * @version $Revision$ $Date$
    * @author Jason Horman (jason@jhorman.org)
  @@ -99,11 +101,11 @@
   
       public void run(UnaryProcedure proc) {
           if(signOf(step) == -1) {
  -            for(int i=from; i >= to; i += step) {
  +            for(int i=from; i > to; i += step) {
                   proc.run(new Integer(i));
               }
           } else {
  -            for(int i=from; i <= to; i += step) {
  +            for(int i=from; i < to; i += step) {
                   proc.run(new Integer(i));
               }
           }
  
  
  
  1.1                  jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/generator/util/LongRange.java
  
  Index: LongRange.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/generator/util/LongRange.java,v
1.1 2003/11/24 23:59:21 rwaldhoff Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.functor.generator.util;
  
  import org.apache.commons.functor.UnaryProcedure;
  import org.apache.commons.functor.generator.BaseGenerator;
  
  
  /**
   * A generator for the range <i>from</i> (inclusive) to <i>to</i>
(exclusive).
   *
   * @since 1.0
   * @version $Revision: 1.1 $ $Date: 2003/11/24 23:59:21 $
   * @author Jason Horman (jason@jhorman.org)
   * @author Rodney Waldhoff
   */
  public final class LongRange extends BaseGenerator {
  
      // constructors
      //---------------------------------------------------------------
  
      public LongRange(Number from, Number to) {
          this(from.longValue(),to.longValue());
      }
  
      public LongRange(Number from, Number to, Number step) {
          this(from.longValue(),to.longValue(),step.longValue());
      }
          
      public LongRange(long from, long to) {
          this(from,to,defaultStep(from,to));
      }
  
      public LongRange(long from, long to, long step) {
          if(from != to && signOf(step) != signOf(to-from)) {
              throw new IllegalArgumentException("Will never reach " + to + " from " + from
+ " using step " + step);
          } else {
              this.from = from;
              this.to = to;
              this.step = step;
          }
      }
  
      // methods
      //---------------------------------------------------------------
  
      public void run(UnaryProcedure proc) {
          if(signOf(step) == -1L) {
              for(long i=from; i > to; i += step) {
                  proc.run(new Long(i));
              }
          } else {
              for(long i=from; i < to; i += step) {
                  proc.run(new Long(i));
              }
          }
      }
  
      public String toString() {
          return "LongRange<" + from + "," + to + "," + step + ">";
      }
  
      public boolean equals(Object obj) {
          if(obj instanceof LongRange) {
              LongRange that = (LongRange)obj;
              return this.from == that.from && this.to == that.to && this.step
== that.step;
          } else {
              return false;
          }
      }
  
      public int hashCode() {
          int hash = "LongRange".hashCode();
          hash <<= 2;
          hash ^= from;
          hash <<= 2;
          hash ^= to;
          hash <<= 2;
          hash ^= step;
          return hash;
      }
      
      // private methods
      //---------------------------------------------------------------
          
      private static long signOf(long value) {
          if(value < 0) {
              return -1;
          } else if(value > 0) {
              return 1;
          } else {
              return 0;
          }
      }
  
      private static long defaultStep(long from, long to) {
          if(from > to) {
              return -1L;
          } else {
              return 1L;
          }
      }
  
      // attributes
      //---------------------------------------------------------------
          
      private long from;
      private long to;
      private long step;
  
  
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message