Coverage Report - org.as3coreaddendum.system.comparators.PropertyComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyComparator
100%
16/16
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  
         import org.as3coreaddendum.system.IComparator;
 32  
 
 33  
         /**
 34  
          * This comparator uses the received <code>comparator</code> object to compare the values recovered from the <code>property</code> of each of the two objects.
 35  
          * 
 36  
          * @example
 37  
          * 
 38  
          * <listing version="3.0">
 39  
          * import org.as3coreaddendum.system.comparators.AlphabeticComparator;
 40  
          * import org.as3coreaddendum.system.comparators.NumberComparator;
 41  
          * import org.as3coreaddendum.system.comparators.PropertyComparator;
 42  
          *
 43  
          * var a:Object = { id: 1, name: "TestA" };
 44  
          * var b:Object = { id: 0, name: "TestB" };
 45  
          * var c:Object = { id: 2, name: "TestC" };
 46  
          *  
 47  
          * var p1:PropertyComparator = new PropertyComparator("id", new NumberComparator());
 48  
          * 
 49  
          * p1.compare(a, a)    // 0
 50  
          * p1.compare(a, b)    // 1
 51  
          * p1.compare(b, a)    // -1
 52  
          * p1.compare(b, c)    // -1
 53  
          * p1.compare(c, a)    // 1
 54  
          *
 55  
          * var p2:PropertyComparator = new PropertyComparator("name", new AlphabeticComparator());
 56  
          *  
 57  
          * p2.compare(a, a)    // 0
 58  
          * p2.compare(a, b)    // -1
 59  
          * p2.compare(b, a)    // 1
 60  
          * p2.compare(b, c)    // -1
 61  
          * p2.compare(c, a)    // 1
 62  
          * </listing>
 63  
          * 
 64  
          * @author Flávio Silva
 65  
          */
 66  
         public class PropertyComparator implements IComparator
 67  
         {
 68  
                 private var _comparator: IComparator;
 69  
                 private var _property: String;
 70  
 
 71  
                 /**
 72  
                  * Defines the comparator object used in the comparison.
 73  
                  * 
 74  
                  * @throws         ArgumentError if is assigned a <code>null</code> value.
 75  
                  */
 76  
                 public function get comparator(): IComparator { return _comparator; }
 77  
 
 78  
                 public function set comparator(value:IComparator): void
 79  
                 {
 80  1
                         if (value == null) throw new ArgumentError("The property 'comparator' must not be 'null'.");
 81  1
                         _comparator = value;
 82  1
                 }
 83  
 
 84  
                 /**
 85  
                  * Defines the name of the property to be recovered and compared between the objects.
 86  
                  * 
 87  
                  * @throws         ArgumentError if is assigned a <code>null</code> value or an empty <code>String</code>.
 88  
                  */
 89  
                 public function get property(): String { return _property; }
 90  
 
 91  
                 public function set property(value:String): void
 92  
                 {
 93  1
                         if (value == null || value == "") throw new ArgumentError("The property 'property' must not be 'null' nor an empty 'String'.");
 94  1
                         _property = value;
 95  1
                 }
 96  
 
 97  
                 /**
 98  
                  * Constructor, creates a new <code>PropertyComparator</code> object.
 99  
                  * 
 100  
                  * @param         property         The name of the property to be recovered and compared between the objects.
 101  
                  * @param         comparator         The comparator object used in the comparison.
 102  
                  * @throws         ArgumentError if the <code>property</code> argument is <code>null</code> or an empty <code>String</code>.
 103  
                  * @throws         ArgumentError if the <code>comparator</code> argument is <code>null</code>.
 104  
                  */
 105  
                 public function PropertyComparator(property:String, comparator:IComparator)
 106  1
                 {
 107  1
                         if (property == null || property == "") throw new ArgumentError("The 'property' argument must not be 'null' nor an empty 'String'.");
 108  1
                         if (comparator == null) throw new ArgumentError("The 'comparator' argument must not be 'null'.");
 109  
                         
 110  1
                         _property = property;
 111  1
                         _comparator = comparator;
 112  1
                 }
 113  
 
 114  
                 /**
 115  
                  * Performs the comparison between the values recovered from the properties of each of the two objects.
 116  
                  * 
 117  
                  * @param         o1         The first object which has the property recovered to be compared. Can be an instance of any custom class.
 118  
                  * @param         o2         The second object which has the property recovered to be compared. Can be an instance of any custom class.
 119  
                  * @throws         ArgumentError if any argument is <code>null</code>.
 120  
                  * @return         A negative integer, zero, or a positive integer as the first property is less than, equal to, or greater than the second.
 121  
                  */
 122  
                 public function compare(o1:*, o2:*): int
 123  
                 {
 124  1
                         if (o1 == null || o2 == null) throw new ArgumentError("Both arguments must not be 'null'.");
 125  
                         
 126  1
                         return _comparator.compare(o1[_property], o2[_property]);
 127  
                 }
 128  
                 
 129  
                 /**
 130  
                  * Returns the string representation of this object.
 131  
                  * 
 132  
                  * @return         the string representation of the this object.
 133  
                  */
 134  
                 public function toString():String
 135  
                 {
 136  1
                         return "[PropertyComparator: property = " + property + "]";
 137  
                 }
 138  
 
 139  
         }
 140  
 
 141  
 }