Coverage Report - org.jfree.data.hc.DataRange
 
Classes in this File Line Coverage Branch Coverage Complexity
DataRange
100%
50/50
100%
22/22
4.636
 
 1  
 /* ======================================================================= 
 2  
  * A visualisation library extension for JFreeChart. Please see JFreeChart
 3  
  * for further information.
 4  
  * =======================================================================
 5  
  * Copyright (C) 2006  University of Helsinki, Department of Computer Science
 6  
  *
 7  
  * This library is free software; you can redistribute it and/or
 8  
  * modify it under the terms of the GNU Lesser General Public
 9  
  * License as published by the Free Software Foundation; either
 10  
  * version 2.1 of the License, or (at your option) any later version.
 11  
  *
 12  
  * This library is distributed in the hope that it will be useful,
 13  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15  
  * Lesser General Public License for more details.
 16  
  *
 17  
  * You should have received a copy of the GNU Lesser General Public
 18  
  * License along with this library; if not, write to the Free Software
 19  
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 20  
  * -----------------------------
 21  
  * Contact:  ohtu@cs.helsinki.fi
 22  
  * -----------------------------
 23  
  *
 24  
  */
 25  
 
 26  
 package org.jfree.data.hc;
 27  
 
 28  
 /**
 29  
  * A class that specifies an integer range.
 30  
  * @author  viski project
 31  
  */
 32  
 public class DataRange implements Cloneable{
 33  
 
 34  
     private int leftBound;
 35  
     private int rightBound;
 36  
 
 37  
     /**
 38  
      * Creates a new real number range.
 39  
      *
 40  
      * @param leftBound  the minimum value.
 41  
      * @param rightBound  the maximum value.
 42  
      */
 43  1094
     public DataRange(int leftBound, int rightBound) {
 44  
 
 45  1094
         if (rightBound < leftBound) {
 46  122
             this.leftBound = Integer.MAX_VALUE;
 47  122
             this.rightBound = Integer.MIN_VALUE;
 48  122
         } else {
 49  972
             this.leftBound = leftBound;
 50  972
             this.rightBound = rightBound;
 51  
         }
 52  
 
 53  1094
     }
 54  
 
 55  
     /**
 56  
      * Checks whether the range is empty. This is true, when
 57  
      * when the maximum variable is less than the minimum.
 58  
      *
 59  
      * @return  A boolean.
 60  
      */
 61  
     public boolean isEmpty() {
 62  
 
 63  4653
         return (this.rightBound<this.leftBound);
 64  
 
 65  
     }
 66  
 
 67  
     /**
 68  
      * Returns the minimum value.
 69  
      *
 70  
      * @return  The minimum value.
 71  
      * @throws RuntimeException  if the minimum value is null ie. has not been set.
 72  
      */
 73  
     public int getLeftBound() {
 74  
 
 75  1322
         if (this.isEmpty()) throw new RuntimeException("getLeftBound() was called on an empty DataRange.");
 76  1311
         return this.leftBound;
 77  
 
 78  
     }
 79  
     
 80  
     /**
 81  
      * Returns the maximum value.
 82  
      *
 83  
      * @return  The maximum value.
 84  
      * @throws RuntimeException  if the maximum value is null ie. has not been set.
 85  
      */
 86  
     public int getRightBound() {
 87  
 
 88  1325
         if (this.isEmpty()) throw new RuntimeException("getRightBound() was called on an empty DataRange.");
 89  1324
         return this.rightBound;
 90  
 
 91  
     }
 92  
     
 93  
     /**
 94  
      * Returns a string with the current bounds.
 95  
      *
 96  
      * @return  The bounds.
 97  
      */
 98  
     public String toString() {
 99  3
         if (this.isEmpty()) return "[]";
 100  2
         return "[" + this.leftBound + "," + this.rightBound + "]";
 101  
     }
 102  
 
 103  
     /**
 104  
      * Returns the length of the interval.
 105  
      *
 106  
      * @return  The length of the interval or zero, if the range is empty.
 107  
      */
 108  
     public int getWidth() {
 109  
 
 110  91
         if (this.isEmpty()) return 0;
 111  90
         else return this.rightBound-this.leftBound+1;
 112  
 
 113  
     }
 114  
 
 115  
     /**
 116  
      * Checks whether a datarange contains another datarange.
 117  
      *
 118  
      * @param that  the datarange.
 119  
      * @return  A boolean.
 120  
      */
 121  
     public boolean contains(DataRange that) {
 122  
 
 123  
         // if that is empty, it is contained by any range.
 124  
         // This implies that empty ranges contain other empty
 125  
         // ranges. Probably what we want.
 126  10
         if (that.isEmpty()) return true;
 127  
 
 128  
         // Empty ranges won't contain other empty ranges.
 129  8
         if (this.isEmpty()) return false;
 130  
 
 131  
         // does it contain index?
 132  7
         if (that.getLeftBound() < this.leftBound) return false;
 133  6
         if (that.getRightBound() > this.rightBound) return false;
 134  
 
 135  
         // yes.
 136  4
         return true;
 137  
     }
 138  
     
 139  
     /**
 140  
      * Checks whether an index is in range.
 141  
      *
 142  
      * @param index  the index.
 143  
      * @return  A boolean.
 144  
      */
 145  
     public boolean contains(int index) {
 146  
 
 147  
         // is the range empty?
 148  423
         if (this.isEmpty()) return false;
 149  
 
 150  
         // does it contain index?
 151  422
         if (index < this.leftBound) return false;
 152  393
         if (index > this.rightBound) return false;
 153  
 
 154  
         // yes.
 155  250
         return true;
 156  
     }
 157  
     
 158  
     /**
 159  
      * Imports the bounds of a DataRange-object to this object.
 160  
      * 
 161  
      * @param range  the range to be imported.
 162  
      */
 163  
     public void add(DataRange range) throws DataRangeMismatchException {
 164  
 
 165  
         int newLeft;
 166  
         int newRight;
 167  
 
 168  
         try {
 169  
 
 170  172
             newLeft = range.getLeftBound();
 171  168
             newRight = range.getRightBound();
 172  
             
 173  4
         } catch (Exception e) {
 174  
 
 175  
             // the range to add is empty, don't do a thing
 176  4
             return;
 177  
 
 178  168
         }
 179  
 
 180  
         // if this is empty, we simply copy the range to be added here.
 181  168
         if (this.isEmpty()) {
 182  
         
 183  8
             this.leftBound = newLeft;
 184  8
             this.rightBound = newRight;
 185  
 
 186  8
         } else {
 187  
 
 188  160
             if (newLeft < this.leftBound) {
 189  2
                 if (newRight < this.leftBound-1)
 190  1
                     throw new DataRangeMismatchException();
 191  1
                 this.leftBound = newLeft;
 192  
             }
 193  159
             if (newRight > this.rightBound) {
 194  156
                 if (newLeft > this.rightBound+1)
 195  1
                     throw new DataRangeMismatchException();
 196  155
                 this.rightBound = newRight;
 197  
             }
 198  
 
 199  
         }
 200  
 
 201  166
     }
 202  
 
 203  
     /**
 204  
      * Returns a new DataRange object with the same bounds.
 205  
      *
 206  
      * @return  A new DataRange.
 207  
      */
 208  
     public Object clone() {
 209  
 
 210  240
         return new DataRange(this.leftBound,this.rightBound);
 211  
 
 212  
     }
 213  
 
 214  
     /**
 215  
      * Checks whether two ranges are equal.
 216  
      *
 217  
      * @param range  the range to be tested with this one.
 218  
      * @return  A boolean.
 219  
      */
 220  
     public boolean equals(DataRange range) {
 221  440
         if (this.isEmpty() && range.isEmpty()) return true;
 222  421
         if (this.isEmpty() != range.isEmpty()) return false;
 223  341
         if (range.getLeftBound() != this.getLeftBound()) return false;
 224  319
         if (range.getRightBound() != this.getRightBound()) return false;
 225  243
         return true;
 226  
     }
 227  
 
 228  
 }