Coverage Report - org.as3coreaddendum.system.comparators.NumberComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
NumberComparator
100%
20/20
N/A
0
 
 1  
 /*
 2  
  * Licensed under the MIT License
 3  
  * 
 4  
  * Copyright 2010 (c) Flávio Silva, http://flsilva.com
 5  
  *
 6  
  * Permission is hereby granted, free of charge, to any person
 7  
  * obtaining a copy of this software and associated documentation
 8  
  * files (the "Software"), to deal in the Software without
 9  
  * restriction, including without limitation the rights to use,
 10  
  * copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  
  * copies of the Software, and to permit persons to whom the
 12  
  * Software is furnished to do so, subject to the following
 13  
  * conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be
 16  
  * included in all copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 19  
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 20  
  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 21  
  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 22  
  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 23  
  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 24  
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 25  
  * OTHER DEALINGS IN THE SOFTWARE.
 26  
  * 
 27  
  * http://www.opensource.org/licenses/mit-license.php
 28  
  */
 29  
 
 30  1
 package org.as3coreaddendum.system.comparators
 31  
 {
 32  
         import org.as3coreaddendum.system.IComparator;
 33  
 
 34  
         /**
 35  
          * A comparator for <code>Number</code> objects.
 36  
          * 
 37  
          * @example
 38  
          * 
 39  
          * <listing version="3.0">
 40  
          * import org.as3coreaddendum.system.comparators.NumberComparator;
 41  
          * 
 42  
          * var c:NumberComparator = new NumberComparator();
 43  
          * 
 44  
          * c.compare(0, 0);              // 0
 45  
          * c.compare(1, 1);              // 0
 46  
          * c.compare(0, 1);              // -1
 47  
          * c.compare(94, 56);            // 1
 48  
          * c.compare(-38, -27);          // -1
 49  
          * c.compare(2.5, 2.5);          // 0
 50  
          * c.compare(2.54, 2.54);        // 0
 51  
          * c.compare(2.54, 2.56);        // -1
 52  
          * c.compare(2.567, 2.567);      // 0
 53  
          * c.compare(2.5678, 2.5677);    // 1
 54  
          * c.compare(2.5678, 2.5674);    // 1
 55  
          * c.compare(2.5674, 2.5678);    // -1
 56  
          * </listing>
 57  
          * 
 58  
          * <listing version="3.0">
 59  
          * import org.as3coreaddendum.system.comparators.NumberComparator;
 60  
          * 
 61  
          * var c1:NumberComparator = new NumberComparator(true, 1);
 62  
          * 
 63  
          * c1.compare(2.5, 2.5);          // 0
 64  
          * c1.compare(2.54, 2.54);        // 0
 65  
          * c1.compare(2.54, 2.56);        // -1
 66  
          * c1.compare(2.567, 2.567);      // 0
 67  
          * c1.compare(2.5678, 2.5677);    // 0
 68  
          * c1.compare(2.5678, 2.5674);    // 0
 69  
          * c1.compare(2.5674, 2.5678);    // 0
 70  
          * </listing>
 71  
          * 
 72  
          * <listing version="3.0">
 73  
          * import org.as3coreaddendum.system.comparators.NumberComparator;
 74  
          * 
 75  
          * var c2:NumberComparator = new NumberComparator(true, 2);
 76  
          * 
 77  
          * c2.compare(2.5, 2.5);          // 0
 78  
          * c2.compare(2.54, 2.54);        // 0
 79  
          * c2.compare(2.54, 2.56);        // -1
 80  
          * c2.compare(2.567, 2.567);      // 0
 81  
          * c2.compare(2.5678, 2.5677);    // 0
 82  
          * c2.compare(2.5678, 2.5674);    // 0
 83  
          * c2.compare(2.5674, 2.5678);    // 0
 84  
          * </listing>
 85  
          * 
 86  
          * <listing version="3.0">
 87  
          * import org.as3coreaddendum.system.comparators.NumberComparator;
 88  
          * 
 89  
          * var c3:NumberComparator = new NumberComparator(true, 3);
 90  
          * 
 91  
          * c3.compare(2.5, 2.5);          // 0
 92  
          * c3.compare(2.54, 2.54);        // 0
 93  
          * c3.compare(2.54, 2.56);        // -1
 94  
          * c3.compare(2.567, 2.567);      // 0
 95  
          * c3.compare(2.5678, 2.5677);    // 0
 96  
          * c3.compare(2.5678, 2.5674);    // 1
 97  
          * c3.compare(2.5674, 2.5678);    // -1
 98  
          * </listing>
 99  
          * 
 100  
          * <listing version="3.0">
 101  
          * import org.as3coreaddendum.system.comparators.NumberComparator;
 102  
          * 
 103  
          * var c4:NumberComparator = new NumberComparator(true, 4);
 104  
          * 
 105  
          * c4.compare(2.5, 2.5);          // 0
 106  
          * c4.compare(2.54, 2.54);        // 0
 107  
          * c4.compare(2.54, 2.56);        // -1
 108  
          * c4.compare(2.567, 2.567);      // 0
 109  
          * c4.compare(2.5678, 2.5677);    // 1
 110  
          * c4.compare(2.5678, 2.5674);    // 1
 111  
          * c4.compare(2.5674, 2.5678);    // -1
 112  
          * </listing>
 113  
          * 
 114  
          * @see org.as3coreaddendum.system.IComparator IComparator
 115  
          * @author Flávio Silva
 116  
          */
 117  
         public class NumberComparator implements IComparator
 118  
         {
 119  
                 private var _decimalPlaces: int;
 120  
                 private var _useDecimalPlaces: Boolean;
 121  
 
 122  
                 /**
 123  
                  * Defines the number of decimal places to be considered in the comparison. Must be an integer between 0 and 20.
 124  
                  * 
 125  
                  * @throws         RangeError if is assigned a value out of the 0-20 range.
 126  
                  */
 127  
                 public function get decimalPlaces(): int { return _decimalPlaces; }
 128  
 
 129  
                 public function set decimalPlaces(value:int): void
 130  
                 {
 131  1
                         if (value < 0 || value > 20) throw new RangeError("The property 'decimalPlaces' must be an integer between 0 and 20.");
 132  1
                         _decimalPlaces = value;
 133  1
                 }
 134  
 
 135  
                 /**
 136  
                  * Defines if both numbers should be rounded by the <code>decimalPlaces</code> property.
 137  
                  */
 138  
                 public function get useDecimalPlaces(): Boolean { return _useDecimalPlaces; }
 139  
 
 140  
                 public function set useDecimalPlaces(value:Boolean): void { _useDecimalPlaces = value; }
 141  
 
 142  
                 /**
 143  
                  * Constructor, creates a new <code>NumberComparator</code> object.
 144  
                  * 
 145  
                  * @param useDecimalPlaces         Indicates if both numbers should be rounded by the <code>decimalPlaces</code> property.
 146  
                  * @param decimalPlaces         The number of decimal places to be considered in the comparison. Must be an integer between 0 and 20.
 147  
                  * @throws         ArgumentError         if <code>useDecimalPlaces</code> argument is sent <code>false</code> but some value is sent to <code>decimalPlaces</code> argument.
 148  
                  * @throws         RangeError                 if the <code>decimalPlaces</code> argument is out of the 0-20 range.
 149  
                  */
 150  
                 public function NumberComparator(useDecimalPlaces:Boolean = false, decimalPlaces:int = 0)
 151  1
                 {
 152  1
                         if (useDecimalPlaces == false && decimalPlaces != 0) throw new ArgumentError("To send the 'decimalPlaces' argument you must send <code>true</code> to the 'useDecimalPlaces' argument.");
 153  1
                         if (decimalPlaces < 0 || decimalPlaces > 20) throw new RangeError("The argument 'decimalPlaces' must be an integer between 0 and 20.");
 154  
                         
 155  1
                         this.useDecimalPlaces = useDecimalPlaces;
 156  1
                         this.decimalPlaces = decimalPlaces;
 157  1
                 }
 158  
 
 159  
                 /**
 160  
                  * Performs the comparison between the two arguments.
 161  
                  * 
 162  
                  * @param         o1        The first <code>Number</code> object to be compared.
 163  
                  * @param         o2        The second <code>Number</code> object to be compared.
 164  
                  * @throws         ArgumentError if any of the arguments is not of type <code>Number</code>.
 165  
                  * @return         A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
 166  
                  */
 167  
                 public function compare(o1:*, o2:*): int
 168  
                 {
 169  1
                         if (!(o1 is Number) || !(o2 is Number)) throw new ArgumentError("Both arguments must be of type 'Number'.");
 170  
                         
 171  1
                         if (_useDecimalPlaces)
 172  
                         {
 173  1
                                 o1 = Number((o1 as Number).toFixed(_decimalPlaces));
 174  1
                                 o2 = Number((o2 as Number).toFixed(_decimalPlaces));
 175  
                         }
 176  
                         
 177  1
                         if (o1 == o2)
 178  
                         {
 179  1
                                 return 0;
 180  
                         }
 181  1
                         else if (o1 < o2)
 182  
                         {
 183  1
                                 return -1;
 184  
                         }
 185  
                         else
 186  
                         {
 187  1
                                 return 1;
 188  
                         }
 189  
                 }
 190  
 
 191  
                 /**
 192  
                  * Returns the string representation of this object.
 193  
                  * 
 194  
                  * @return         the string representation of the this object.
 195  
                  */
 196  
                 public function toString():String
 197  
                 {
 198  1
                         return "[NumberComparator: useDecimalPlaces = " + useDecimalPlaces + " | decimalPlaces = " + decimalPlaces + "]";
 199  
                 }
 200  
 
 201  
         }
 202  
 
 203  
 }