Coverage Report - org.as3coreaddendum.system.comparators.AlphabeticalComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
AlphabeticalComparator
100%
36/36
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 alphabetic order of <code>String</code> objects.
 36  
          * 
 37  
          * @example
 38  
          * 
 39  
          * <listing version="3.0">
 40  
          * import org.as3coreaddendum.system.comparators.AlphabeticComparator;
 41  
          * 
 42  
          * var c1:AlphabeticComparator = new AlphabeticComparator(AlphabeticComparison.LOWER_CASE_FIRST);
 43  
          * 
 44  
          * c1.compare("a", "a")                      // 0
 45  
          * c1.compare("a", "b")                      // -1
 46  
          * c1.compare("b", "a")                      // 1
 47  
          * c1.compare("a", "A")                      // -1
 48  
          * c1.compare("A", "a")                      // 1
 49  
          * c1.compare("comparison", "comparison")    // 0
 50  
          * c1.compare("comparison", "comParison")    // -1
 51  
          * c1.compare("comParison", "comparison")    // 1
 52  
          * c1.compare("comparison", "Comparison")    // -1
 53  
          * c1.compare("Comparison", "comparison")    // 1
 54  
          * c1.compare("between", "strings")          // -1
 55  
          * c1.compare("between", "Strings")          // -1
 56  
          * c1.compare("Between", "strings")          // -1
 57  
          * c1.compare("Between", "Strings")          // -1
 58  
          * c1.compare("between", "between")          // 0
 59  
          * c1.compare("between", "betweena")         // -1
 60  
          * c1.compare("betweena", "betweenA")        // -1
 61  
          * c1.compare("betweenA", "betweena")        // 1
 62  
          * c1.compare("TestA", "TestB")              // -1
 63  
          * c1.compare("TestB", "TestA")              // 1
 64  
          * </listing>
 65  
          * 
 66  
          * <listing version="3.0">
 67  
          * import org.as3coreaddendum.system.comparators.AlphabeticComparator;
 68  
          * 
 69  
          * var c2:AlphabeticComparator = new AlphabeticComparator(AlphabeticComparison.UPPER_CASE_FIRST);
 70  
          * 
 71  
          * c2.compare("a", "a")                      // 0
 72  
          * c2.compare("a", "b")                      // -1
 73  
          * c2.compare("b", "a")                      // 1
 74  
          * c2.compare("a", "A")                      // 1
 75  
          * c2.compare("A", "a")                      // -1
 76  
          * c2.compare("comparison", "comparison")    // 0
 77  
          * c2.compare("comparison", "comParison")    // 1
 78  
          * c2.compare("comParison", "comparison")    // -1
 79  
          * c2.compare("comparison", "Comparison")    // 1
 80  
          * c2.compare("Comparison", "comparison")    // -1
 81  
          * c2.compare("between", "strings")          // -1
 82  
          * c2.compare("between", "Strings")          // -1
 83  
          * c2.compare("Between", "strings")          // -1
 84  
          * c2.compare("Between", "Strings")          // -1
 85  
          * c2.compare("between", "between")          // 0
 86  
          * c2.compare("between", "betweena")         // -1
 87  
          * c2.compare("betweena", "betweenA")        // 1
 88  
          * c2.compare("betweenA", "betweena")        // -1
 89  
          * c2.compare("TestA", "TestB")              // -1
 90  
          * c2.compare("TestB", "TestA")              // 1
 91  
          * </listing>
 92  
          * 
 93  
          * <listing version="3.0">
 94  
          * import org.as3coreaddendum.system.comparators.AlphabeticComparator;
 95  
          * 
 96  
          * var c3:AlphabeticComparator = new AlphabeticComparator(AlphabeticalComparison.CASE_INSENSITIVE);
 97  
          * 
 98  
          * c3.compare("a", "a")                      // 0
 99  
          * c3.compare("a", "b")                      // -1
 100  
          * c3.compare("b", "a")                      // 1
 101  
          * c3.compare("a", "A")                      // 0
 102  
          * c3.compare("A", "a")                      // 0
 103  
          * c3.compare("comparison", "comparison")    // 0
 104  
          * c3.compare("comparison", "comParison")    // 0
 105  
          * c3.compare("comParison", "comparison")    // 0
 106  
          * c3.compare("comparison", "Comparison")    // 0
 107  
          * c3.compare("Comparison", "comparison")    // 0
 108  
          * c3.compare("between", "strings")          // -1
 109  
          * c3.compare("between", "Strings")          // -1
 110  
          * c3.compare("Between", "strings")          // -1
 111  
          * c3.compare("Between", "Strings")          // -1
 112  
          * c3.compare("between", "between")          // 0
 113  
          * c3.compare("between", "betweena")         // -1
 114  
          * c3.compare("betweena", "betweenA")        // 0
 115  
          * c3.compare("betweenA", "betweena")        // 0
 116  
          * c3.compare("TestA", "TestB")              // -1
 117  
          * c3.compare("TestB", "TestA")              // 1
 118  
          * </listing>
 119  
          * 
 120  
          * @see         org.as3coreaddendum.system.comparators.AlphabeticalComparison AlphabeticalComparison
 121  
          * @author         Flávio Silva
 122  
          */
 123  
         public class AlphabeticalComparator implements IComparator
 124  
         {
 125  
                 private var _comparison: AlphabeticalComparison;
 126  
 
 127  
                 /**
 128  
                  * Defines the type of comparison to be used.
 129  
                  * @throws         ArgumentError If is assigned a <code>null</code> value.
 130  
                  * @see         org.as3coreaddendum.system.comparators.AlphabeticalComparison AlphabeticalComparison
 131  
                  */
 132  
                 public function get comparison(): AlphabeticalComparison { return _comparison; }
 133  
 
 134  
                 public function set comparison(value:AlphabeticalComparison): void
 135  
                 {
 136  1
                         if (value == null) throw new ArgumentError("The property 'comparison' must not be 'null'.");
 137  1
                         _comparison = value;
 138  1
                 }
 139  
 
 140  
                 /**
 141  
                  * Constructor, creates a new <code>AlphabeticComparator</code> object.
 142  
                  * 
 143  
                  * @param         comparison                Indicates which type of comparison will be used.
 144  
                  * @throws         ArgumentError         If the 'comparison' argument is <code>null</code>.
 145  
                  * @see         org.as3coreaddendum.system.comparators.AlphabeticalComparison AlphabeticalComparison
 146  
                  */
 147  
                 public function AlphabeticalComparator(comparison:AlphabeticalComparison)
 148  1
                 {
 149  1
                         if (comparison == null) throw new ArgumentError("The property 'comparison' must not be 'null'.");
 150  1
                         this.comparison = comparison;
 151  1
                 }
 152  
 
 153  
                 /**
 154  
                  * Performs the comparison between the two arguments.
 155  
                  * 
 156  
                  * @param         o1        The first <code>String</code> object to be compared.
 157  
                  * @param         o2        The second <code>String</code> object to be compared.
 158  
                  * @throws         ArgumentError         if any of the arguments is <code>null</code>.
 159  
                  * @return        A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
 160  
                  */
 161  
                 public function compare(o1:*, o2:*): int
 162  
                 {
 163  1
                         if (o1 == null || o2 == null) throw new ArgumentError("Both arguments must not be 'null'.");
 164  
                         
 165  1
                         o1 = String(o1);
 166  1
                         o2 = String(o2);
 167  
                         
 168  1
                         if (_comparison == AlphabeticalComparison.CASE_INSENSITIVE)
 169  
                         {
 170  1
                                 o1 = (o1 as String).toLowerCase();
 171  1
                                 o2 = (o2 as String).toLowerCase();
 172  
                         }
 173  
                         
 174  1
                         if (o1 == o2) return 0;
 175  
                         
 176  
                         var c1a:String;
 177  
                         var c2a:String;
 178  
                         var c1b:String;
 179  
                         var c2b:String;
 180  
                         var lc:int;
 181  1
                         var i:int = 0;
 182  1
                         var j:int = Math.min((o1 as String).length, (o2 as String).length);
 183  
                         
 184  1
                         while (i < j)
 185  
                         {
 186  1
                                 c1a = (o1 as String).charAt(i);
 187  1
                                 c2a = (o2 as String).charAt(i);
 188  
                                 
 189  1
                                 c1b = c1a.toLowerCase();
 190  1
                                 c2b = c2a.toLowerCase();
 191  
                                 
 192  1
                                 lc = c1b.localeCompare(c2b);
 193  
                                 
 194  1
                                 if (lc < 0)
 195  
                                 {
 196  1
                                         return -1;
 197  
                                 }
 198  1
                                 else if (lc > 0)
 199  
                                 {
 200  1
                                         return 1;
 201  
                                 }
 202  1
                                 else if (c1a != c2a)
 203  
                                 {
 204  1
                                         if (_comparison == AlphabeticalComparison.LOWER_CASE_FIRST)
 205  
                                         {
 206  1
                                                 return (c1a == c1a.toUpperCase()) ? 1 : -1;
 207  
                                         }
 208  
                                         else
 209  
                                         {
 210  1
                                                 return (c1a == c1a.toUpperCase()) ? -1 : 1;
 211  
                                         }
 212  
                                 }
 213  
                                 
 214  1
                                 i++;
 215  
                         }
 216  
                         
 217  1
                         if ((o1 as String).length < (o2 as String).length)
 218  
                         {
 219  1
                                 return -1;
 220  
                         }
 221  
                         else
 222  
                         {
 223  1
                                 return 1;
 224  
                         }
 225  
                 }
 226  
                 
 227  
                 /**
 228  
                  * Returns the string representation of this object.
 229  
                  * 
 230  
                  * @return         the string representation of the this object.
 231  
                  */
 232  
                 public function toString():String
 233  
                 {
 234  1
                         return "[AlphabeticComparator: comparison = " + comparison + "]";
 235  
                 }
 236  
 
 237  
         }
 238  
 
 239  
 }