Coverage Report - org.as3collections.UniqueCollection
 
Classes in this File Line Coverage Branch Coverage Complexity
UniqueCollection
100%
30/30
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
 31  
 {
 32  
         import org.as3collections.utils.CollectionUtil;
 33  
 
 34  
         /**
 35  
          * <code>UniqueCollection</code> works as a wrapper for a collection.
 36  
          * <p>It does not allow duplicate elements in the collection.</p>
 37  
          * <p>It stores the <code>wrapCollection</code> constructor's argument internally.
 38  
          * So every method call to this class is forwarded to the <code>wrappedCollection</code> object.
 39  
          * The methods that need to be checked for duplication are previously validated before forward the call.
 40  
          * No error is thrown by the validation of duplication.
 41  
          * The calls that are forwarded to the <code>wrappedCollection</code> returns the return of the <code>wrappedCollection</code> call.</p>
 42  
          * 
 43  
          * @author Flávio Silva
 44  
          */
 45  
         public class UniqueCollection implements ICollection
 46  
         {
 47  
                 private var _wrappedCollection: ICollection;
 48  
 
 49  
                 /**
 50  
                  * @inheritDoc
 51  
                  */
 52  
                 public function get allEquatable(): Boolean { return _wrappedCollection.allEquatable; }
 53  
 
 54  
                 /**
 55  
                  * @private
 56  
                  */
 57  
                 protected function get wrappedCollection(): ICollection { return _wrappedCollection; }
 58  
                 
 59  
                 /**
 60  
                  * Constructor, creates a new <code>UniqueCollection</code> object.
 61  
                  * 
 62  
                  * @param         wrapCollection         the target collection to wrap.
 63  
                  * @throws         ArgumentError          if the <code>wrapCollection</code> argument is <code>null</code>.
 64  
                  */
 65  
                 public function UniqueCollection(wrapCollection:ICollection)
 66  1
                 {
 67  1
                         if (!wrapCollection) throw new ArgumentError("The 'wrapCollection' argument must not be 'null'.");
 68  1
                         _wrappedCollection = CollectionUtil.removeDuplicate(wrapCollection);
 69  1
                 }
 70  
 
 71  
                 /**
 72  
                  * If <code>wrappedCollection.contains(element)</code> returns <code>true</code>, then this method returns <code>false</code>. Otherwise, it forwards the call to <code>wrappedCollection.add</code>.
 73  
                  * 
 74  
                  * @param          element         the element to forward to <code>wrappedCollection.add</code>.
 75  
                  * @return         <code>false</code> if <code>wrappedCollection.contains(element)</code> returns <code>true</code>. Otherwise returns the return of the call <code>wrappedCollection.add</code>.
 76  
                  */
 77  
                 public function add(element:*): Boolean
 78  
                 {
 79  1
                         if (_wrappedCollection.contains(element)) return false;
 80  1
                         return _wrappedCollection.add(element);
 81  
                 }
 82  
 
 83  
                 /**
 84  
                  * If the specified collection is empty returns <code>false</code>. Otherwise, it clones the specified collection, removes all elements that already are in the <code>wrappedCollection</code> and removes all duplicates. Then it forwards the call to <code>wrappedCollection.addAll</code> sending the cloned and filtered collection.
 85  
                  * 
 86  
                  * @param          collection         the collection to forward to <code>wrappedCollection.addAll</code>.
 87  
                  * @throws         ArgumentError           if the specified collection contains a <code>null</code> element and <code>wrappedCollection</code> does not permit <code>null</code> elements, or if the specified collection is <code>null</code>.
 88  
                  * @return         <code>false</code> if the specified collection is <code>null</code> or empty. Otherwise returns the return of the call <code>wrappedCollection.addAll</code>.
 89  
                  */
 90  
                 public function addAll(collection:ICollection): Boolean
 91  
                 {
 92  1
                         if (!collection) throw new ArgumentError("The 'collection' argument must not be 'null'.");
 93  1
                         if (collection.isEmpty()) return false;
 94  
                         
 95  1
                         var c:ICollection = collection.clone();
 96  1
                         filterCollection(c);
 97  
                         
 98  1
                         if (c.isEmpty()) return false;
 99  
                         
 100  1
                         return _wrappedCollection.addAll(c);
 101  
                 }
 102  
 
 103  
                 /**
 104  
                  * Forwards the call to <code>wrappedCollection.clear</code>.
 105  
                  */
 106  
                 public function clear(): void
 107  
                 {
 108  1
                         _wrappedCollection.clear();
 109  1
                 }
 110  
 
 111  
                 /**
 112  
                  * Creates and return a new <code>UniqueCollection</code> object with the clone of the <code>wrappedCollection</code> object.
 113  
                  * 
 114  
                  * @return         a new <code>UniqueCollection</code> object with the clone of the <code>wrappedCollection</code> object.
 115  
                   */
 116  
                 public function clone(): *
 117  
                 {
 118  1
                         return new UniqueCollection(_wrappedCollection.clone());
 119  
                 }
 120  
 
 121  
                 /**
 122  
                  * Forwards the call to <code>wrappedCollection.contains</code>.
 123  
                  * 
 124  
                  * @param          o
 125  
                  * @return         the return of the call <code>wrappedCollection.contains</code>.
 126  
                  */
 127  
                 public function contains(o:*): Boolean
 128  
                 {
 129  1
                         return _wrappedCollection.contains(o);
 130  
                 }
 131  
 
 132  
                 /**
 133  
                  * Forwards the call to <code>wrappedCollection.containsAll</code>.
 134  
                  * 
 135  
                  * @param collection
 136  
                  * @return         the return of the call <code>wrappedCollection.containsAll</code>.
 137  
                  */
 138  
                 public function containsAll(collection:ICollection): Boolean
 139  
                 {
 140  1
                         return _wrappedCollection.containsAll(collection);
 141  
                 }
 142  
 
 143  
                 /**
 144  
                  * This method uses <code>CollectionUtil.equalNotConsideringOrder</code> method to perform equality, sending this collection and <code>other</code> argument.
 145  
                  * 
 146  
                  * @param          other         the object to be compared for equality.
 147  
                  * @return         <code>true</code> if the arbitrary evaluation considers the objects equal.
 148  
                  * @see         org.as3collections.utils.CollectionUtil#equalNotConsideringOrder() CollectionUtil.equalNotConsideringOrder()
 149  
                  */
 150  
                 public function equals(other:*): Boolean
 151  
                 {
 152  1
                         return CollectionUtil.equalNotConsideringOrder(this, other);
 153  
                 }
 154  
 
 155  
                 /**
 156  
                  * Forwards the call to <code>wrappedCollection.isEmpty</code>.
 157  
                  * 
 158  
                  * @return         the return of the call <code>wrappedCollection.isEmpty</code>.
 159  
                   */
 160  
                 public function isEmpty(): Boolean
 161  
                 {
 162  1
                         return _wrappedCollection.isEmpty();
 163  
                 }
 164  
 
 165  
                 /**
 166  
                  * Forwards the call to <code>wrappedCollection.iterator</code>.
 167  
                  * 
 168  
                  * @return         the return of the call <code>wrappedCollection.iterator</code>.
 169  
                   */
 170  
                 public function iterator(): IIterator
 171  
                 {
 172  1
                         return _wrappedCollection.iterator();
 173  
                 }
 174  
 
 175  
                 /**
 176  
                  * Forwards the call to <code>wrappedCollection.remove</code>.
 177  
                  * 
 178  
                  * @param o
 179  
                  * @return         the return of the call <code>wrappedCollection.remove</code>.
 180  
                  */
 181  
                 public function remove(o:*): Boolean
 182  
                 {
 183  1
                         return _wrappedCollection.remove(o);
 184  
                 }
 185  
 
 186  
                 /**
 187  
                  * Forwards the call to <code>wrappedCollection.removeAll</code>.
 188  
                  * 
 189  
                  * @param collection
 190  
                  * @return         the return of the call <code>wrappedCollection.removeAll</code>.
 191  
                  */
 192  
                 public function removeAll(collection:ICollection): Boolean
 193  
                 {
 194  1
                         return _wrappedCollection.removeAll(collection);
 195  
                 }
 196  
 
 197  
                 /**
 198  
                  * Forwards the call to <code>wrappedCollection.retainAll</code>.
 199  
                  * 
 200  
                  * @param collection
 201  
                  * @return         the return of the call <code>wrappedCollection.retainAll</code>.
 202  
                  */
 203  
                 public function retainAll(collection:ICollection): Boolean
 204  
                 {
 205  1
                         return _wrappedCollection.retainAll(collection);
 206  
                 }
 207  
 
 208  
                 /**
 209  
                  * Forwards the call to <code>wrappedCollection.size</code>.
 210  
                  * 
 211  
                  * @return         the return of the call <code>wrappedCollection.size</code>.
 212  
                   */
 213  
                 public function size(): int
 214  
                 {
 215  1
                         return _wrappedCollection.size();
 216  
                 }
 217  
 
 218  
                 /**
 219  
                  * Forwards the call to <code>wrappedCollection.toArray</code>.
 220  
                  * 
 221  
                  * @return         the return of the call <code>wrappedCollection.toArray</code>.
 222  
                   */
 223  
                 public function toArray(): Array
 224  
                 {
 225  1
                         return _wrappedCollection.toArray();
 226  
                 }
 227  
 
 228  
                 /**
 229  
                  * Returns the string representation of this instance.
 230  
                  * <p>This method uses <code>CollectionUtil.toString</code> method.</p>
 231  
                  * 
 232  
                  * @return the string representation of this instance.
 233  
                   */
 234  
                 public function toString():String 
 235  
                 {
 236  1
                         return CollectionUtil.toString(this);
 237  
                 }
 238  
 
 239  
                 /**
 240  
                  * @private
 241  
                   */
 242  
                 protected function filterCollection(collection:ICollection):void
 243  
                 {
 244  1
                         collection.removeAll(_wrappedCollection);
 245  1
                         CollectionUtil.removeDuplicate(collection);
 246  1
                 }
 247  
 
 248  
         }
 249  
 
 250  
 }