Coverage Report - org.apache.commons.functor.generator.util.LongRange
 
Classes in this File Line Coverage Branch Coverage Complexity
LongRange
100%
36/36
88%
23/26
2.5
 
 1  
 /*
 2  
  * Licensed under the Apache License, Version 2.0 (the "License");
 3  
  * you may not use this file except in compliance with the License.
 4  
  * You may obtain a copy of the License at
 5  
  *
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  *
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 
 15  
 package org.apache.commons.functor.generator.util;
 16  
 
 17  
 import org.apache.commons.functor.UnaryProcedure;
 18  
 import org.apache.commons.functor.generator.BaseGenerator;
 19  
 
 20  
 /**
 21  
  * A generator for the range <i>from</i> (inclusive) to <i>to</i> (exclusive).
 22  
  *
 23  
  * @since 1.0
 24  
  * @version $Revision: 1156805 $ $Date: 2011-08-11 22:15:35 +0200 (Thu, 11 Aug 2011) $
 25  
  * @author Jason Horman (jason@jhorman.org)
 26  
  * @author Rodney Waldhoff
 27  
  */
 28  
 public final class LongRange extends BaseGenerator<Long> {
 29  
     // attributes
 30  
     //---------------------------------------------------------------
 31  
 
 32  
     private final long from;
 33  
     private final long to;
 34  
     private final long step;
 35  
 
 36  
     // constructors
 37  
     //---------------------------------------------------------------
 38  
     /**
 39  
      * Create a new LongRange.
 40  
      * @param from start
 41  
      * @param to end
 42  
      */
 43  
     public LongRange(Number from, Number to) {
 44  4
         this(from.longValue(), to.longValue());
 45  4
     }
 46  
 
 47  
     /**
 48  
      * Create a new LongRange.
 49  
      * @param from start
 50  
      * @param to end
 51  
      * @param step increment
 52  
      */
 53  
     public LongRange(Number from, Number to, Number step) {
 54  4
         this(from.longValue(), to.longValue(), step.longValue());
 55  4
     }
 56  
 
 57  
     /**
 58  
      * Create a new LongRange.
 59  
      * @param from start
 60  
      * @param to end
 61  
      */
 62  
     public LongRange(long from, long to) {
 63  26
         this(from, to, defaultStep(from, to));
 64  26
     }
 65  
 
 66  
     /**
 67  
      * Create a new LongRange.
 68  
      * @param from start
 69  
      * @param to end
 70  
      * @param step increment
 71  
      */
 72  52
     public LongRange(long from, long to, long step) {
 73  52
         if (from != to && signOf(step) != signOf(to - from)) {
 74  6
             throw new IllegalArgumentException("Will never reach " + to + " from " + from + " using step " + step);
 75  
         }
 76  46
         this.from = from;
 77  46
         this.to = to;
 78  46
         this.step = step;
 79  46
     }
 80  
 
 81  
     // methods
 82  
     //---------------------------------------------------------------
 83  
     /**
 84  
      * {@inheritDoc}
 85  
      */
 86  
     public void run(UnaryProcedure<? super Long> proc) {
 87  28
         if (signOf(step) == -1L) {
 88  70
             for (long i = from; i > to; i += step) {
 89  60
                 proc.run(i);
 90  
             }
 91  
         } else {
 92  110
             for (long i = from; i < to; i += step) {
 93  92
                 proc.run(i);
 94  
             }
 95  
         }
 96  28
     }
 97  
 
 98  
     /**
 99  
      * {@inheritDoc}
 100  
      */
 101  
     public String toString() {
 102  26
         return "LongRange<" + from + "," + to + "," + step + ">";
 103  
     }
 104  
 
 105  
     /**
 106  
      * {@inheritDoc}
 107  
      */
 108  
     public boolean equals(Object obj) {
 109  28
         if (obj == this) {
 110  6
             return true;
 111  
         }
 112  22
         if (!(obj instanceof LongRange)) {
 113  2
             return false;
 114  
         }
 115  20
         LongRange that = (LongRange) obj;
 116  20
         return this.from == that.from && this.to == that.to && this.step == that.step;
 117  
     }
 118  
 
 119  
     /**
 120  
      * {@inheritDoc}
 121  
      */
 122  
     public int hashCode() {
 123  30
         int hash = "LongRange".hashCode();
 124  30
         hash <<= 2;
 125  30
         hash ^= from;
 126  30
         hash <<= 2;
 127  30
         hash ^= to;
 128  30
         hash <<= 2;
 129  30
         hash ^= step;
 130  30
         return hash;
 131  
     }
 132  
 
 133  
     // private methods
 134  
     //---------------------------------------------------------------
 135  
     /**
 136  
      * Get <code>value/|value|</code> (0L when value == 0L).
 137  
      * @param value to test
 138  
      * @return long
 139  
      */
 140  
     private static long signOf(long value) {
 141  120
         return value < 0L ? -1L : value > 0L ? 1L : 0L;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Calculate default step to get from <code>from</code> to <code>to</code>.
 146  
      * @param from start
 147  
      * @param to end
 148  
      * @return long
 149  
      */
 150  
     private static long defaultStep(long from, long to) {
 151  26
         return from > to ? -1L : 1L;
 152  
     }
 153  
 
 154  
 }