Coverage Report - org.as3collections.queues.TypedQueue
 
Classes in this File Line Coverage Branch Coverage Complexity
TypedQueue
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.queues 
 31  
 {
 32  
         import org.as3collections.IQueue;
 33  
         import org.as3collections.TypedCollection;
 34  
         import org.as3collections.utils.CollectionUtil;
 35  
         import org.as3utils.ReflectionUtil;
 36  
 
 37  
         /**
 38  
          * <code>TypedQueue</code> works as a wrapper for a queue.
 39  
          * Since ActionScript 3.0 does not support typed arrays, <code>TypedQueue</code> is a way to create typed queues.
 40  
          * It stores the <code>wrapQueue</code> constructor's argument in the <code>wrappedQueue</code> variable.
 41  
          * So every method call to this class is forwarded to the <code>wrappedQueue</code> object.
 42  
          * The methods that need to be checked for the type of the elements are previously validated with the <code>validateType</code> or <code>validateCollection</code> method 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, the method <code>offer</code> returns <code>false</code> and the method <code>add</code> throws <code>org.as3coreaddendum.errors.ClassCastError</code>.
 44  
          * The calls that are forwarded to the <code>wrappedQueue</code> returns the return of the <code>wrappedQueue</code> call.
 45  
          * <p><code>TypedQueue</code> does not allow <code>null</code> elements.</p>
 46  
          * <p>You can also create unique and typed queues. See below the link "QueueUtil.getUniqueTypedQueue()".</p>
 47  
          * 
 48  
          * @example
 49  
          * 
 50  
          * <listing version="3.0">
 51  
          * import org.as3collections.IQueue;
 52  
          * import org.as3collections.queues.LinearQueue;
 53  
          * import org.as3collections.queues.TypedQueue;
 54  
          * import org.as3collections.utils.QueueUtil;
 55  
          * 
 56  
          * var q1:IQueue = new LinearQueue([1, 5, 3, 7]);
 57  
          * 
 58  
          * var queue1:IQueue = new TypedQueue(q1, int); // you can use this way
 59  
          * 
 60  
          * //var queue1:IQueue = QueueUtil.getTypedQueue(q1); // or you can use this way
 61  
          * 
 62  
          * queue1                      // [1,5,3,7]
 63  
          * queue1.size():              // 4
 64  
          * queue1.isEmpty()            // false
 65  
          * 
 66  
          * queue1.poll()               // 1
 67  
          * queue1                      // [5,3,7]
 68  
          * 
 69  
          * queue1.offer(2)             // true
 70  
          * queue1                      // [5,3,7,2]
 71  
          * 
 72  
          * queue1.offer(5)             // true
 73  
          * queue1                      // [5,3,7,2,5]
 74  
          * 
 75  
          * queue1.offer("a")           // false
 76  
          * queue1                      // [5,3,7,2,5]
 77  
          * 
 78  
          * queue1.add("a")             // ClassCastError: Invalid element type. element: a | type: String | expected type: int
 79  
          * </listing>
 80  
          * 
 81  
          * @see org.as3collections.utils.QueueUtil#getTypedQueue() QueueUtil.getTypedQueue()
 82  
          * @see org.as3collections.utils.QueueUtil#getUniqueTypedQueue() QueueUtil.getUniqueTypedQueue()
 83  
          * @author Flávio Silva
 84  
          */
 85  
         public class TypedQueue extends TypedCollection implements IQueue
 86  
         {
 87  
                 /**
 88  
                  * @private
 89  
                  */
 90  
                 protected function get wrappedQueue(): IQueue { return wrappedCollection as IQueue; }
 91  
 
 92  
                 /**
 93  
                  * Constructor, creates a new <code>TypedQueue</code> object.
 94  
                  * 
 95  
                  * @param         wrapQueue         the target queue to wrap.
 96  
                  * @param         type                 the type of the elements allowed by this queue.
 97  
                  * @throws         ArgumentError          if the <code>wrapQueue</code> argument is <code>null</code>.
 98  
                  * @throws         ArgumentError          if the <code>type</code> argument is <code>null</code>.
 99  
                  * @throws         org.as3coreaddendum.errors.ClassCastError                  if the types of one or more elements in the <code>wrapQueue</code> argument are incompatible with the <code>type</code> argument.
 100  
                  */
 101  
                 public function TypedQueue(wrapQueue:IQueue, type:*)
 102  
                 {
 103  1
                         super(wrapQueue, type);
 104  1
                 }
 105  
                 
 106  
                 /**
 107  
                  * If the <code>element</code> argument is <code>null</code> throws <code>ArgumentError</code>.
 108  
                  * Otherwise the element is validated with the <code>validateType</code> method to be forwarded to <code>wrappedCollection.add</code>.
 109  
                  * 
 110  
                  * @param          element         the element to forward to <code>wrappedCollection.add</code>.
 111  
                  * @throws         ArgumentError          if the <code>element</code> argument is <code>null</code>.
 112  
                  * @throws         org.as3coreaddendum.errors.ClassCastError                  if the type of the element is incompatible with the type of this collection.
 113  
                  * @return         the return of the call <code>wrappedCollection.add</code>.
 114  
                  */
 115  
                 override public function add(element:*): Boolean
 116  
                 {
 117  1
                         if (!element) throw new ArgumentError("The 'element' argument must not be 'null'.");
 118  1
                         return super.add(element);
 119  
                 }
 120  
 
 121  
                 /**
 122  
                  * Creates and return a new <code>TypedQueue</code> object with the clone of the <code>wrappedQueue</code> object.
 123  
                  * 
 124  
                  * @return         a new <code>TypedQueue</code> object with the clone of the <code>wrappedQueue</code> object.
 125  
                   */
 126  
                 override public function clone(): *
 127  
                 {
 128  1
                         return new TypedQueue(wrappedQueue.clone(), type);
 129  
                 }
 130  
 
 131  
                 /**
 132  
                  * Forwards the call to <code>wrappedQueue.dequeue</code>.
 133  
                  * 
 134  
                  * @return         the return of the call <code>wrappedQueue.dequeue</code>.
 135  
                  */
 136  
                 public function dequeue(): *
 137  
                 {
 138  1
                         return wrappedQueue.dequeue();
 139  
                 }
 140  
 
 141  
                 /**
 142  
                  * Forwards the call to <code>wrappedQueue.element</code>.
 143  
                  * 
 144  
                  * @return         the return of the call <code>wrappedQueue.element</code>.
 145  
                  */
 146  
                 public function element(): *
 147  
                 {
 148  1
                         return wrappedQueue.element();
 149  
                 }
 150  
 
 151  
                 /**
 152  
                  * This method first checks if <code>other</code> argument is a <code>TypedQueue</code>.
 153  
                  * If not it returns <code>false</code>. If <code>true</code> it checks the <code>type</code> property of both queues.
 154  
                  * If they are different it returns <code>false</code>.
 155  
                  * Otherwise it uses <code>CollectionUtil.equalConsideringOrder</code> method to perform equality, sending this queue and <code>other</code> argument.
 156  
                  * 
 157  
                  * @param          other         the object to be compared for equality.
 158  
                  * @return         <code>true</code> if the arbitrary evaluation considers the objects equal.
 159  
                  * @see         org.as3collections.utils.CollectionUtil#equalConsideringOrder() CollectionUtil.equalConsideringOrder()
 160  
                  */
 161  
                 override public function equals(other:*): Boolean
 162  
                 {
 163  1
                         if (this == other) return true;
 164  1
                         if (!ReflectionUtil.classPathEquals(this, other)) return false;
 165  
                         
 166  1
                         var otherList:TypedQueue = other as TypedQueue;
 167  1
                         if (type != otherList.type) return false;
 168  
                         
 169  1
                         return CollectionUtil.equalConsideringOrder(this, other);
 170  
                 }
 171  
 
 172  
                 /**
 173  
                  * If <code>isValidType(element)</code> returns <code>false</code> then returns <code>false</code>.
 174  
                  * Otherwise, it forwards the call to <code>wrappedQueue.offer</code>.
 175  
                  * 
 176  
                  * @param          element         the element to forward to <code>wrappedQueue.offer</code>.
 177  
                  * @return         <code>false</code> if <code>isValidType(element)</code> returns <code>false</code>. Otherwise returns the return of the call <code>wrappedQueue.offer</code>.
 178  
                  */
 179  
                 public function offer(element:*): Boolean
 180  
                 {
 181  1
                         if (!isValidElement(element)) return false;
 182  1
                         return wrappedQueue.offer(element);
 183  
                 }
 184  
 
 185  
                 /**
 186  
                  * Forwards the call to <code>wrappedQueue.peek</code>.
 187  
                  * 
 188  
                  * @return         the return of the call <code>wrappedQueue.peek</code>.
 189  
                  */
 190  
                 public function peek(): *
 191  
                 {
 192  1
                         return wrappedQueue.peek();
 193  
                 }
 194  
 
 195  
                 /**
 196  
                  * Forwards the call to <code>wrappedQueue.poll</code>.
 197  
                  * 
 198  
                  * @return         the return of the call <code>wrappedQueue.poll</code>.
 199  
                  */
 200  
                 public function poll(): *
 201  
                 {
 202  1
                         return wrappedQueue.poll();
 203  
                 }
 204  
 
 205  
         }
 206  
 
 207  
 }