Coverage Report - org.as3collections.queues.UniqueQueue
 
Classes in this File Line Coverage Branch Coverage Complexity
UniqueQueue
100%
13/13
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.UniqueCollection;
 34  
         import org.as3collections.utils.CollectionUtil;
 35  
         import org.as3utils.ReflectionUtil;
 36  
 
 37  
         import flash.errors.IllegalOperationError;
 38  
 
 39  
         /**
 40  
          * <code>UniqueQueue</code> works as a wrapper for a queue.
 41  
          * It does not allow duplicated elements in the queue.
 42  
          * It stores the <code>wrapQueue</code> constructor's argument in the <code>wrappedQueue</code> variable.
 43  
          * So every method call to this class is forwarded to the <code>wrappedQueue</code> object.
 44  
          * The methods that need to be checked for duplication are previously validated before forward the call.
 45  
          * The calls that are forwarded to the <code>wrappedQueue</code> returns the return of the <code>wrappedQueue</code> call.
 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.UniqueQueue;
 54  
          * import org.as3collections.utils.QueueUtil;
 55  
          * 
 56  
          * var q1:IQueue = new LinearQueue([1, 5, 3, 7]);
 57  
          * 
 58  
          * var queue1:IQueue = new UniqueQueue(q1); // you can use this way
 59  
          * 
 60  
          * //var queue1:IQueue = QueueUtil.getUniqueQueue(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)             // false
 73  
          * queue1                      // [5,3,7,2]
 74  
          * 
 75  
          * queue1.add(5)               // Error: UniqueQueue is a unique queue and does not allow duplicated elements. Requested element: 5
 76  
          * </listing>
 77  
          * 
 78  
          * @see org.as3collections.utils.QueueUtil#getUniqueQueue() QueueUtil.getUniqueQueue()
 79  
          * @see org.as3collections.utils.QueueUtil#getUniqueTypedQueue() QueueUtil.getUniqueTypedQueue()
 80  
          * @author Flávio Silva
 81  
          */
 82  
         public class UniqueQueue extends UniqueCollection implements IQueue
 83  
         {
 84  
                 /**
 85  
                  * @private
 86  
                  */
 87  
                 protected function get wrappedQueue(): IQueue { return wrappedCollection as IQueue; }
 88  
 
 89  
                 /**
 90  
                  * Constructor, creates a new <code>UniqueQueue</code> object.
 91  
                  * 
 92  
                  * @param         wrapQueue         the target queue to wrap.
 93  
                  * @throws         ArgumentError          if the <code>wrapQueue</code> argument is <code>null</code>.
 94  
                  */
 95  
                 public function UniqueQueue(wrapQueue:IQueue)
 96  
                 {
 97  1
                         super(wrapQueue);
 98  1
                 }
 99  
 
 100  
                 /**
 101  
                  * Inserts the specified element into this queue if it is possible to do so immediately without violating restrictions.
 102  
                  * This method differs from <code>offer</code> only in that it throws an error if the element cannot be inserted.
 103  
                  * <p>This implementation returns the result of <code>offer</code> unless the element cannot be inserted.</p>
 104  
                  * <p>If <code>wrappedQueue.contains(element)</code> returns <code>true</code> an <code>IllegalOperationError</code> is thrown.</p>
 105  
                  * 
 106  
                  * @param          element         the element to be added.
 107  
                  * @throws         ArgumentError          if the specified element is <code>null</code>.
 108  
                  * @throws         org.as3coreaddendum.errors.ClassCastError                  if the class of the specified element prevents it from being added to this queue.
 109  
                  * @throws         flash.errors.IllegalOperationError                          if <code>wrappedQueue.contains(element)</code> returns <code>true</code>.
 110  
                  * @return         <code>true</code> if this queue changed as a result of the call.
 111  
                  */
 112  
                 override public function add(element:*): Boolean
 113  
                 {
 114  1
                         if (wrappedQueue.contains(element)) throw new IllegalOperationError(ReflectionUtil.getClassName(this) + " is a unique queue and does not allow duplicated elements. Requested element: " + element);
 115  1
                         return super.add(element);
 116  
                 }
 117  
 
 118  
                 /**
 119  
                  * Creates and return a new <code>UniqueQueue</code> object with the clone of the <code>wrappedQueue</code> object.
 120  
                  * 
 121  
                  * @return         a new <code>UniqueQueue</code> object with the clone of the <code>wrappedQueue</code> object.
 122  
                   */
 123  
                 override public function clone(): *
 124  
                 {
 125  1
                         return new UniqueQueue(wrappedQueue.clone());
 126  
                 }
 127  
 
 128  
                 /**
 129  
                  * Forwards the call to <code>wrappedQueue.dequeue</code>.
 130  
                  * 
 131  
                  * @return         the return of the call <code>wrappedQueue.dequeue</code>.
 132  
                  */
 133  
                 public function dequeue(): *
 134  
                 {
 135  1
                         return wrappedQueue.dequeue();
 136  
                 }
 137  
 
 138  
                 /**
 139  
                  * Forwards the call to <code>wrappedQueue.element</code>.
 140  
                  * 
 141  
                  * @return         the return of the call <code>wrappedQueue.element</code>.
 142  
                  */
 143  
                 public function element(): *
 144  
                 {
 145  1
                         return wrappedQueue.element();
 146  
                 }
 147  
 
 148  
                 /**
 149  
                  * This method uses <code>CollectionUtil.equalConsideringOrder</code> method to perform equality, sending this list and <code>other</code> argument.
 150  
                  * 
 151  
                  * @param          other         the object to be compared for equality.
 152  
                  * @return         <code>true</code> if the arbitrary evaluation considers the objects equal.
 153  
                  * @see         org.as3collections.utils.CollectionUtil#equalConsideringOrder() CollectionUtil.equalConsideringOrder()
 154  
                  */
 155  
                 override public function equals(other:*): Boolean
 156  
                 {
 157  1
                         return CollectionUtil.equalConsideringOrder(this, other);
 158  
                 }
 159  
 
 160  
                 /**
 161  
                  * If <code>wrappedQueue.contains(element)</code> returns <code>true</code> then returns <code>false</code>.
 162  
                  * Otherwise, it forwards the call to <code>wrappedQueue.offer</code>.
 163  
                  * 
 164  
                  * @param          element         the element to forward to <code>wrappedQueue.offer</code>.
 165  
                  * @return         <code>false</code> if <code>wrappedQueue.contains(element)</code> returns <code>true</code>. Otherwise returns the return of the call <code>wrappedQueue.offer</code>.
 166  
                  */
 167  
                 public function offer(element:*): Boolean
 168  
                 {
 169  1
                         if (wrappedQueue.contains(element)) return false;
 170  1
                         return wrappedQueue.offer(element);
 171  
                 }
 172  
 
 173  
                 /**
 174  
                  * Forwards the call to <code>wrappedQueue.peek</code>.
 175  
                  * 
 176  
                  * @return         the return of the call <code>wrappedQueue.peek</code>.
 177  
                  */
 178  
                 public function peek(): *
 179  
                 {
 180  1
                         return wrappedQueue.peek();
 181  
                 }
 182  
 
 183  
                 /**
 184  
                  * Forwards the call to <code>wrappedQueue.poll</code>.
 185  
                  * 
 186  
                  * @return         the return of the call <code>wrappedQueue.poll</code>.
 187  
                  */
 188  
                 public function poll(): *
 189  
                 {
 190  1
                         return wrappedQueue.poll();
 191  
                 }
 192  
 
 193  
         }
 194  
 
 195  
 }