Coverage Report - org.as3collections.maps.TypedSortedMap
 
Classes in this File Line Coverage Branch Coverage Complexity
TypedSortedMap
100%
24/24
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.as3collections.maps
 31  
 {
 32  
         import org.as3collections.IListMap;
 33  
         import org.as3collections.IMap;
 34  
         import org.as3collections.ISortedMap;
 35  
         import org.as3collections.SortMapBy;
 36  
         import org.as3coreaddendum.system.IComparator;
 37  
         import org.as3utils.ReflectionUtil;
 38  
 
 39  
         /**
 40  
          * <code>TypedSortedMap</code> works as a wrapper for a map.
 41  
          * It stores the <code>wrapMap</code> constructor's argument in the <code>wrappedMap</code> variable.
 42  
          * So every method call to this class is forwarded to the <code>wrappedMap</code> object.
 43  
          * The methods that need to be checked for the type of the keys and values are previously validated with the <code>validateKeyType</code>, <code>validateValueType</code> or <code>validateMap</code> method before forward the call.
 44  
          * If the type of a key or value requested to be inserted to this map is incompatible with the type of the map a <code>org.as3coreaddendum.errors.ClassCastError</code> is thrown.
 45  
          * The calls that are forwarded to the <code>wrappedMap</code> returns the return of the <code>wrappedMap</code> call.
 46  
          * <p><code>TypedSortedMap</code> does not allow <code>null</code> keys or values.</p>
 47  
          * 
 48  
          * @example
 49  
          * 
 50  
          * <listing version="3.0">
 51  
          * import org.as3collections.ISortedMap;
 52  
          * import org.as3collections.maps.SortedArrayListMap;
 53  
          * import org.as3collections.maps.TypedSortedMap;
 54  
          * 
 55  
          * var map1:ISortedMap = new SortedArrayListMap();
 56  
          * 
 57  
          * map1.put("e", 1)            // null
 58  
          * map1.put("d", 2)            // null
 59  
          * map1.put("c", 3)            // null
 60  
          * map1.put("b", 4)            // null
 61  
          * map1.put("a", 5)            // null
 62  
          * 
 63  
          * map1                        // {a=5,b=4,c=3,d=2,e=1}
 64  
          * map1.size()                 // 5
 65  
          * 
 66  
          * var map2:ISortedMap = new TypedSortedMap(map1, String, Number); // you can use this way
 67  
          * 
 68  
          * //var map2:ISortedMap = MapUtil.getTypedSortedMap(map1, String, Number); // or you can use this way
 69  
          * 
 70  
          * map2                        // {a=5,b=4,c=3,d=2,e=1}
 71  
          * map2.size()                 // 5
 72  
          * 
 73  
          * map2.equals(map1)           // false
 74  
          * 
 75  
          * map2.put("f", 6)            // null
 76  
          * map2                        // {a=5,b=4,c=3,d=2,e=1,f=6}
 77  
          * map2.size()                 // 6
 78  
          * 
 79  
          * map2.put("g", "h")          // ClassCastError: Invalid value type. value: h | type: String | expected value type: Number
 80  
          * map2.put(7, 8)              // ClassCastError: Invalid key type. key: 7 | type: int | expected key type: String
 81  
          * </listing>
 82  
          * 
 83  
          * @see org.as3collections.utils.MapUtil#getTypedSortedMap() MapUtil.getTypedSortedMap()
 84  
          * @author Flávio Silva
 85  
          */
 86  
         public class TypedSortedMap extends TypedListMap implements ISortedMap
 87  
         {
 88  
                 /**
 89  
                  * Defines the <code>wrappedMap</code> comparator object to be used automatically to sort.
 90  
                  * <p>If this value change the <code>wrappedMap</code> is automatically reordered with the new value.</p>
 91  
                  */
 92  
                 public function get comparator(): IComparator { return wrappedSortedMap.comparator; }
 93  
 
 94  
                 public function set comparator(value:IComparator): void { wrappedSortedMap.comparator = value; }
 95  
 
 96  
                 /**
 97  
                  * Defines the <code>wrappedMap</code> options to be used automatically to sort.
 98  
                  * <p>If this value change the map is automatically reordered with the new value.</p>
 99  
                  */
 100  
                 public function get options(): uint { return wrappedSortedMap.options; }
 101  
 
 102  
                 public function set options(value:uint): void { wrappedSortedMap.options = value; }
 103  
 
 104  
                 /**
 105  
                  * Defines whether the <code>wrappedMap</code> should be sorted by its keys or values. The default is <code>SortMapBy.KEY</code>.
 106  
                  * <p>If this value change the <code>wrappedMap</code> is automatically reordered with the new value.</p>
 107  
                  */
 108  
                 public function get sortBy(): SortMapBy { return wrappedSortedMap.sortBy; }
 109  
 
 110  
                 public function set sortBy(value:SortMapBy): void { wrappedSortedMap.sortBy = value; }
 111  
 
 112  
                 /**
 113  
                  * @private
 114  
                  */
 115  
                 protected function get wrappedSortedMap(): ISortedMap { return wrappedMap as ISortedMap; }
 116  
 
 117  
                 /**
 118  
                  * Constructor, creates a new <code>TypedSortedMap</code> object.
 119  
                  * 
 120  
                  * @param         wrapMap         the target map to wrap.
 121  
                  * @param         typeKeys        the type of the keys allowed by this map.
 122  
                  * @param         typeValues        the type of the values allowed by this map.
 123  
                  * @throws         ArgumentError          if the <code>wrapMap</code> argument is <code>null</code>.
 124  
                  * @throws         ArgumentError          if the <code>typeKeys</code> argument is <code>null</code>.
 125  
                  * @throws         ArgumentError          if the <code>typeValues</code> argument is <code>null</code>.
 126  
                  * @throws         org.as3coreaddendum.errors.ClassCastError                  if the types of one or more keys or values in the <code>wrapMap</code> argument are incompatible with the <code>typeKeys</code> or <code>typeValues</code> argument.
 127  
                  */
 128  
                 public function TypedSortedMap(wrapMap:ISortedMap, typeKeys:*, typeValues:*): void
 129  
                 {
 130  1
                         super(wrapMap, typeKeys, typeValues);
 131  1
                 }
 132  
 
 133  
                 /**
 134  
                  * Creates and return a new <code>TypedSortedMap</code> object with the clone of the <code>wrappedMap</code> object.
 135  
                  * 
 136  
                  * @return         a new <code>TypedSortedMap</code> object with the clone of the <code>wrappedMap</code> object.
 137  
                   */
 138  
                 override public function clone(): *
 139  
                 {
 140  1
                         return new TypedSortedMap(wrappedSortedMap.clone(), typeKeys, typeValues);
 141  
                 }
 142  
                 
 143  
                 /**
 144  
                  * Performs an arbitrary, specific evaluation of equality between this object and the <code>other</code> object.
 145  
                  * <p>This implementation considers two differente objects equal if:</p>
 146  
                  * <p>
 147  
                  * <ul><li>object A and object B are instances of the same class (i.e. if they have <b>exactly</b> the same type)</li>
 148  
                  * <li>object A contains all mappings of object B</li>
 149  
                  * <li>object B contains all mappings of object A</li>
 150  
                  * <li>mappings have exactly the same order</li>
 151  
                  * <li>object A and object B has the same type of comparator</li>
 152  
                  * <li>object A and object B has the same options</li>
 153  
                  * <li>object A and object B has the same sortBy</li>
 154  
                  * </ul></p>
 155  
                  * <p>This implementation takes care of the order of the mappings in the map.
 156  
                  * So, for two maps are equal the order of mappings returned by the iterator must be equal.</p>
 157  
                  * 
 158  
                  * @param          other         the object to be compared for equality.
 159  
                  * @return         <code>true</code> if the arbitrary evaluation considers the objects equal.
 160  
                  */
 161  
                 override public function equals(other:*): Boolean
 162  
                 {
 163  1
                         if (this == other) return true;
 164  
                         
 165  1
                         if (!ReflectionUtil.classPathEquals(this, other)) return false;
 166  
                         
 167  1
                         var m:ISortedMap = other as ISortedMap;
 168  
                         
 169  1
                         if (sortBy != m.sortBy) return false;
 170  1
                         if (options != m.options) return false;
 171  1
                         if (!comparator && m.comparator) return false;
 172  1
                         if (comparator && !m.comparator) return false;
 173  1
                         if (!ReflectionUtil.classPathEquals(comparator, m.comparator)) return false;
 174  
                         
 175  1
                         return super.equals(other);
 176  
                 }
 177  
                 
 178  
                 /**
 179  
                  * Forwards the call to <code>wrappedMap.headMap</code>.
 180  
                  * 
 181  
                  * @param toKey
 182  
                  * @return
 183  
                  */
 184  
                 override public function headMap(toKey:*): IListMap
 185  
                 {
 186  1
                         var headMap:IMap = wrappedSortedMap.headMap(toKey);
 187  1
                         var sortedSubMap:ISortedMap = new SortedArrayListMap(headMap, comparator, options);
 188  
                         
 189  1
                         return new TypedSortedMap(sortedSubMap, typeKeys, typeValues);
 190  
                 }
 191  
 
 192  
                 /**
 193  
                  * Forwards the call to <code>wrappedMap.sort</code>.
 194  
                  * 
 195  
                  * @param compare
 196  
                  * @param options
 197  
                  * @return
 198  
                  */
 199  
                 public function sort(compare:Function = null, options:uint = 0): Array
 200  
                 {
 201  1
                         return wrappedSortedMap.sort(compare, options);
 202  
                 }
 203  
 
 204  
                 /**
 205  
                  * Forwards the call to <code>wrappedMap.sortOn</code>.
 206  
                  * 
 207  
                  * @param fieldName
 208  
                  * @param options
 209  
                  * @return
 210  
                  */
 211  
                 public function sortOn(fieldName:*, options:* = null): Array
 212  
                 {
 213  1
                         return wrappedSortedMap.sortOn(fieldName, options);
 214  
                 }
 215  
                 
 216  
                 /**
 217  
                  * Forwards the call to <code>wrappedMap.subMap</code>.
 218  
                  * 
 219  
                  * @param fromIndex
 220  
                  * @param toIndex
 221  
                  * @return
 222  
                  */
 223  
                 override public function subMap(fromIndex:int, toIndex:int): IListMap
 224  
                 {
 225  1
                         var subMap:IMap = wrappedSortedMap.subMap(fromIndex, toIndex);
 226  1
                         var sortedSubMap:ISortedMap = new SortedArrayListMap(subMap, comparator, options);
 227  
                         
 228  1
                         return new TypedSortedMap(sortedSubMap, typeKeys, typeValues);
 229  
                 }
 230  
 
 231  
                 /**
 232  
                  * Forwards the call to <code>wrappedMap.tailMap</code>.
 233  
                  * 
 234  
                  * @param fromKey
 235  
                  * @return
 236  
                  */
 237  
                 override public function tailMap(fromKey:*): IListMap
 238  
                 {
 239  1
                         var tailMap:IMap = wrappedSortedMap.tailMap(fromKey);
 240  1
                         var sortedSubMap:ISortedMap = new SortedArrayListMap(tailMap, comparator, options);
 241  
                         
 242  1
                         return new TypedSortedMap(sortedSubMap, typeKeys, typeValues);
 243  
                 }
 244  
 
 245  
         }
 246  
 
 247  
 }