Coverage Report - org.apache.tapestry5.internal.util.IntegerRange
 
Classes in this File Line Coverage Branch Coverage Complexity
IntegerRange
100%
19/19
90%
9/10
0
IntegerRange$RangeIterator
100%
13/13
100%
6/6
0
 
 1  
 // Copyright 2006 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.internal.util;
 16  
 
 17  
 import java.util.Iterator;
 18  
 
 19  
 /**
 20  
  * Represents a sequence of integer values, either ascending or descending. The sequence is always inclusive (of the
 21  
  * finish value).
 22  
  */
 23  176
 public final class IntegerRange implements Iterable<Integer>
 24  
 {
 25  
     private final int start;
 26  
 
 27  
     private final int finish;
 28  
 
 29  96
     private class RangeIterator implements Iterator<Integer>
 30  
     {
 31  
         private final int increment;
 32  
 
 33  28
         private int value = start;
 34  
 
 35  28
         private boolean hasNext = true;
 36  
 
 37  
         RangeIterator()
 38  28
         {
 39  28
             increment = start < finish ? +1 : -1;
 40  28
         }
 41  
 
 42  
         public boolean hasNext()
 43  
         {
 44  106
             return hasNext;
 45  
         }
 46  
 
 47  
         public Integer next()
 48  
         {
 49  96
             if (!hasNext) throw new IllegalStateException();
 50  
 
 51  92
             int result = value;
 52  
 
 53  92
             hasNext = value != finish;
 54  
 
 55  92
             value += increment;
 56  
 
 57  92
             return result;
 58  
         }
 59  
 
 60  
         public void remove()
 61  
         {
 62  2
             throw new UnsupportedOperationException();
 63  
         }
 64  
 
 65  
     }
 66  
 
 67  
     public IntegerRange(final int start, final int finish)
 68  46
     {
 69  46
         this.start = start;
 70  46
         this.finish = finish;
 71  46
     }
 72  
 
 73  
     public int getFinish()
 74  
     {
 75  2
         return finish;
 76  
     }
 77  
 
 78  
     public int getStart()
 79  
     {
 80  2
         return start;
 81  
     }
 82  
 
 83  
     @Override
 84  
     public String toString()
 85  
     {
 86  38
         return String.format("%d..%d", start, finish);
 87  
     }
 88  
 
 89  
     /**
 90  
      * The main puprose of a range object is to produce an Iterator. Since IntegerRange is iterable, it is useful with
 91  
      * the Tapestry Loop component, but also with the Java for loop!
 92  
      */
 93  
     public Iterator<Integer> iterator()
 94  
     {
 95  28
         return new RangeIterator();
 96  
     }
 97  
 
 98  
     @Override
 99  
     public int hashCode()
 100  
     {
 101  8
         final int PRIME = 31;
 102  
 
 103  8
         int result = PRIME + finish;
 104  
 
 105  8
         result = PRIME * result + start;
 106  
 
 107  8
         return result;
 108  
     }
 109  
 
 110  
     /**
 111  
      * Returns true if the other object is an IntegerRange with the same start and finish values.
 112  
      */
 113  
     @Override
 114  
     public boolean equals(Object obj)
 115  
     {
 116  22
         if (this == obj) return true;
 117  20
         if (obj == null) return false;
 118  18
         if (getClass() != obj.getClass()) return false;
 119  16
         final IntegerRange other = (IntegerRange) obj;
 120  16
         if (finish != other.finish) return false;
 121  
 
 122  12
         return start == other.start;
 123  
     }
 124  
 
 125  
 }