Coverage Report - org.as3collections.lists.TypedSortedList
 
Classes in this File Line Coverage Branch Coverage Complexity
TypedSortedList
100%
17/17
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.lists 
 31  
 {
 32  
         import org.as3collections.IList;
 33  
         import org.as3collections.ISortedList;
 34  
         import org.as3coreaddendum.system.IComparator;
 35  
         import org.as3utils.ReflectionUtil;
 36  
 
 37  
         /**
 38  
          * <code>TypedSortedList</code> works as a wrapper for a <code>ISortedList</code> object.
 39  
          * Since ActionScript 3.0 does not support typed arrays, <code>TypedSortedList</code> is a way to create typed lists.
 40  
          * It stores the <code>wrapList</code> constructor's argument internaly.
 41  
          * So every method call to this class is forwarded to the <code>wrappedList</code> object.
 42  
          * The methods that need to be checked for the type of the elements are previously validated before forward the call.
 43  
          * If the type of an element requested to be added to this list is incompatible with the type of the list a <code>org.as3coreaddendum.errors.ClassCastError</code> is thrown.
 44  
          * The calls that are forwarded to the <code>wrappedList</code> returns the return of the <code>wrappedList</code> call.
 45  
          * <p><code>TypedSortedList</code> does not allow <code>null</code> elements.</p>
 46  
          * <p>You can also create unique and typed sorted lists.
 47  
          * See below the link "ListUtil.getUniqueTypedSortedList()".</p>
 48  
          * 
 49  
          * @example
 50  
          * 
 51  
          * <listing version="3.0">
 52  
          * import org.as3collections.ISortedList;
 53  
          * import org.as3collections.IListIterator;
 54  
          * import org.as3collections.lists.SortedArrayList;
 55  
          * import org.as3collections.lists.TypedSortedList;
 56  
          * import org.as3collections.utils.ListUtil;
 57  
          * 
 58  
          * var l1:ISortedList = new SortedArrayList([3, 5, 7], null, Array.NUMERIC);
 59  
          * 
 60  
          * var list1:ISortedList = new TypedSortedList(l1, int); // you can use this way
 61  
          * 
 62  
          * //var list1:ISortedList = ListUtil.getTypedSortedList(l1, int); // or you can use this way
 63  
          * 
 64  
          * list1                          // [3,5,7]
 65  
          * list1.size()                   // 3
 66  
          * 
 67  
          * list1.add(8)                   // true
 68  
          * list1                          // [3,5,7,8]
 69  
          * list1.size()                   // 4
 70  
          * 
 71  
          * list1.remove("abc")            // false
 72  
          * list1                          // [3,4,5,7,8]
 73  
          * list1.size()                   // 5
 74  
          * 
 75  
          * var it:IListIterator = list1.listIterator();
 76  
          * var e:int;
 77  
          * 
 78  
          * while (it.hasNext())
 79  
          * {
 80  
          * 
 81  
          *     e = it.next()
 82  
          *     e                          // 3
 83  
          * 
 84  
          *     e = it.next()
 85  
          *     e:                         // 4
 86  
          * 
 87  
          *     e = it.next()
 88  
          *     e                          // 5
 89  
          * 
 90  
          *     if (e == 5)
 91  
          *     {
 92  
          *         it.add("ghi")          // ClassCastError: Invalid element type. element: ghi | type: String | expected type: int
 93  
          *     }
 94  
          * }
 95  
          * 
 96  
          * list1.setAt(0, [1,2])          // ClassCastError: Invalid element type. element: 1,2 | type: Array | expected type: int
 97  
          * </listing>
 98  
          * 
 99  
          * @see org.as3collections.utils.ListUtil#getTypedSortedList() ListUtil.getTypedSortedList()
 100  
          * @see org.as3collections.utils.ListUtil#getUniqueTypedSortedList() ListUtil.getUniqueTypedSortedList()
 101  
          * @author Flávio Silva
 102  
          */
 103  
         public class TypedSortedList extends TypedList implements ISortedList
 104  
         {
 105  
                 /**
 106  
                  * Defines the <code>wrappedList</code> comparator object to be used automatically to sort.
 107  
                  * <p>If this value change the <code>wrappedList</code> is automatically reordered with the new value.</p>
 108  
                  */
 109  
                 public function get comparator(): IComparator { return wrappedSortedList.comparator; }
 110  
 
 111  
                 public function set comparator(value:IComparator): void { wrappedSortedList.comparator = value; }
 112  
 
 113  
                 /**
 114  
                  * Defines the <code>wrappedList</code> options to be used automatically to sort.
 115  
                  * <p>If this value change the list is automatically reordered with the new value.</p>
 116  
                  */
 117  
                 public function get options(): uint { return wrappedSortedList.options; }
 118  
 
 119  
                 public function set options(value:uint): void { wrappedSortedList.options = value; }
 120  
                 
 121  
                 /**
 122  
                  * @private
 123  
                  */
 124  
                 protected function get wrappedSortedList(): ISortedList { return wrappedList as ISortedList; }
 125  
                 
 126  
                 /**
 127  
                  * Constructor, creates a new <code>TypedList</code> object.
 128  
                  * 
 129  
                  * @param         wrapList         the target list to wrap.
 130  
                  * @param         type                 the type of the elements allowed by this list.
 131  
                  * @throws         ArgumentError          if the <code>wrapList</code> argument is <code>null</code>.
 132  
                  * @throws         ArgumentError          if the <code>type</code> argument is <code>null</code>.
 133  
                  * @throws         org.as3coreaddendum.errors.ClassCastError                  if the types of one or more elements in the <code>wrapList</code> argument are incompatible with the <code>type</code> argument.
 134  
                  */
 135  
                 public function TypedSortedList(wrapList:ISortedList, type:*)
 136  
                 {
 137  1
                         super(wrapList, type);
 138  1
                 }
 139  
 
 140  
                 /**
 141  
                  * Creates and return a new <code>TypedSortedList</code> object with the clone of the <code>wrappedMap</code> object.
 142  
                  * 
 143  
                  * @return         a new <code>TypedSortedList</code> object with the clone of the <code>wrappedMap</code> object.
 144  
                   */
 145  
                 override public function clone(): *
 146  
                 {
 147  1
                         return new TypedSortedList(wrappedSortedList.clone(), type);
 148  
                 }
 149  
                 
 150  
                 /**
 151  
                  * Performs an arbitrary, specific evaluation of equality between this object and the <code>other</code> object.
 152  
                  * <p>This implementation considers two differente objects equal if:</p>
 153  
                  * <p>
 154  
                  * <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>
 155  
                  * <li>object A contains all elements of object B</li>
 156  
                  * <li>object B contains all elements of object A</li>
 157  
                  * <li>elements have exactly the same order</li>
 158  
                  * <li>object A and object B has the same type of comparator</li>
 159  
                  * <li>object A and object B has the same options</li>
 160  
                  * </ul></p>
 161  
                  * <p>This implementation takes care of the order of the elements in the list.
 162  
                  * So, for two lists are equal the order of elements returned by the iterator must be equal.</p>
 163  
                  * 
 164  
                  * @param          other         the object to be compared for equality.
 165  
                  * @return         <code>true</code> if the arbitrary evaluation considers the objects equal.
 166  
                  */
 167  
                 override public function equals(other:*): Boolean
 168  
                 {
 169  1
                         if (this == other) return true;
 170  
                         
 171  1
                         if (!ReflectionUtil.classPathEquals(this, other)) return false;
 172  
                         
 173  1
                         var l:ISortedList = other as ISortedList;
 174  
                         
 175  1
                         if (options != l.options) return false;
 176  1
                         if (!comparator && l.comparator) return false;
 177  1
                         if (comparator && !l.comparator) return false;
 178  1
                         if (!ReflectionUtil.classPathEquals(comparator, l.comparator)) return false;
 179  
                         
 180  1
                         return super.equals(other);
 181  
                 }
 182  
 
 183  
                 /**
 184  
                  * Forwards the call to <code>wrappedList.sort</code>.
 185  
                  * 
 186  
                  * @param compare
 187  
                  * @param options
 188  
                  * @return
 189  
                  */
 190  
                 public function sort(compare:Function = null, options:uint = 0): Array
 191  
                 {
 192  1
                         return wrappedSortedList.sort(compare, options);
 193  
                 }
 194  
 
 195  
                 /**
 196  
                  * Forwards the call to <code>wrappedList.sortOn</code>.
 197  
                  * 
 198  
                  * @param fieldName
 199  
                  * @param options
 200  
                  * @return
 201  
                  */
 202  
                 public function sortOn(fieldName:*, options:* = null): Array
 203  
                 {
 204  1
                         return wrappedSortedList.sortOn(fieldName, options);
 205  
                 }
 206  
                 
 207  
                 /**
 208  
                  * Forwards the call to <code>wrappedList.subList</code>.
 209  
                  * 
 210  
                  * @param fromIndex
 211  
                  * @param toIndex
 212  
                  * @return
 213  
                  */
 214  
                 override public function subList(fromIndex:int, toIndex:int): IList
 215  
                 {
 216  1
                         var subList:IList = wrappedSortedList.subList(fromIndex, toIndex);
 217  1
                         var sortedSubList:ISortedList = new SortedArrayList(subList.toArray(), comparator, options);
 218  
                         
 219  1
                         return new TypedSortedList(sortedSubList, type);
 220  
                 }
 221  
 
 222  
         }
 223  
 
 224  
 }