| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LinearQueue |
|
| 0.0;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.AbstractQueue; | |
| 33 | import org.as3collections.IIterator; | |
| 34 | import org.as3collections.iterators.ArrayIterator; | |
| 35 | ||
| 36 | /** | |
| 37 | * <code>LinearQueue</code> orders elements in a FIFO (first-in-first-out) manner. | |
| 38 | * <p><code>LinearQueue</code> does not allow <code>null</code> elements.</p> | |
| 39 | * | |
| 40 | * @example | |
| 41 | * | |
| 42 | * <b>Example 1</b> | |
| 43 | * | |
| 44 | * <listing version="3.0"> | |
| 45 | * import org.as3collections.IQueue; | |
| 46 | * import org.as3collections.queues.LinearQueue; | |
| 47 | * | |
| 48 | * var queue:IQueue = new LinearQueue(); | |
| 49 | * | |
| 50 | * queue // [] | |
| 51 | * queue.size() // 0 | |
| 52 | * queue.isEmpty() // true | |
| 53 | * | |
| 54 | * queue.peek() // null | |
| 55 | * queue.element() // NoSuchElementError: The queue is empty. | |
| 56 | * | |
| 57 | * queue.offer(3) // true | |
| 58 | * queue // [3] | |
| 59 | * queue.size() // 1 | |
| 60 | * queue.isEmpty() // false | |
| 61 | * | |
| 62 | * queue.offer("a") // true | |
| 63 | * queue // [3,a] | |
| 64 | * | |
| 65 | * queue.offer(1) // true | |
| 66 | * queue // [3,a,1] | |
| 67 | * | |
| 68 | * queue.offer(7) // true | |
| 69 | * queue // [3,a,1,7] | |
| 70 | * | |
| 71 | * queue.offer(null) // false | |
| 72 | * queue.add(null) // ArgumentError: The 'element' argument must not be 'null'. | |
| 73 | * queue // [3,a,1,7] | |
| 74 | * | |
| 75 | * queue.peek() // 3 | |
| 76 | * queue.element() // 3 | |
| 77 | * queue: // [3,a,1,7] | |
| 78 | * | |
| 79 | * queue.poll() // 3 | |
| 80 | * queue // [a,1,7] | |
| 81 | * | |
| 82 | * queue.dequeue() // a | |
| 83 | * queue // [1,7] | |
| 84 | * | |
| 85 | * queue.remove(10) // false | |
| 86 | * queue // [1,7] | |
| 87 | * | |
| 88 | * queue.remove(7) // true | |
| 89 | * queue // [1] | |
| 90 | * | |
| 91 | * queue.clear() | |
| 92 | * queue // [] | |
| 93 | * queue.size() // 0 | |
| 94 | * queue.isEmpty() // true | |
| 95 | * | |
| 96 | * queue.poll() // null | |
| 97 | * queue.dequeue() // NoSuchElementError: The queue is empty. | |
| 98 | * </listing> | |
| 99 | * | |
| 100 | * <b>Example 2</b> | |
| 101 | * | |
| 102 | * <listing version="3.0"> | |
| 103 | * import org.as3collections.IQueue; | |
| 104 | * import org.as3collections.queues.LinearQueue; | |
| 105 | * | |
| 106 | * var queue1:IQueue = new LinearQueue([1, 5, 3, 7]); | |
| 107 | * | |
| 108 | * queue1 // [1,5,3,7] | |
| 109 | * queue1.size() // 4 | |
| 110 | * queue1.isEmpty() // false | |
| 111 | * | |
| 112 | * var queue2:IQueue = queue1.clone(); | |
| 113 | * | |
| 114 | * queue2 // [1,5,3,7] | |
| 115 | * queue2.size() // 4 | |
| 116 | * queue2.isEmpty() // false | |
| 117 | * | |
| 118 | * queue2.equals(queue1) // true | |
| 119 | * queue1.equals(queue2) // true | |
| 120 | * | |
| 121 | * queue2.poll() // 1 | |
| 122 | * queue2 // [5,3,7] | |
| 123 | * | |
| 124 | * queue2.equals(queue1) // false | |
| 125 | * queue1.equals(queue2) // false | |
| 126 | * queue2.equals(queue2) // true | |
| 127 | * | |
| 128 | * queue1.clear() | |
| 129 | * queue1 // [] | |
| 130 | * | |
| 131 | * queue2.clear() | |
| 132 | * queue2: // [] | |
| 133 | * | |
| 134 | * queue2.equals(queue1) // true | |
| 135 | * </listing> | |
| 136 | * | |
| 137 | * @author Flávio Silva | |
| 138 | */ | |
| 139 | public class LinearQueue extends AbstractQueue | |
| 140 | { | |
| 141 | /** | |
| 142 | * Constructor, creates a new <code>LinearQueue</code> object. | |
| 143 | * | |
| 144 | * @param source an array to fill the queue. | |
| 145 | */ | |
| 146 | public function LinearQueue(source:Array = null) | |
| 147 | { | |
| 148 | 1 | super(source); |
| 149 | 1 | } |
| 150 | ||
| 151 | /** | |
| 152 | * Removes all of the elements from this queue. The queue will be empty after this method returns. | |
| 153 | */ | |
| 154 | override public function clear(): void | |
| 155 | { | |
| 156 | 1 | if (isEmpty()) return; |
| 157 | 1 | data.splice(0); |
| 158 | 1 | _totalEquatable = 0; |
| 159 | 1 | } |
| 160 | ||
| 161 | /** | |
| 162 | * Creates and return a new <code>LinearQueue</code> object containing all elements in this queue (in the same order). | |
| 163 | * | |
| 164 | * @return a new <code>LinearQueue</code> object containing all elements in this queue (in the same order). | |
| 165 | */ | |
| 166 | override public function clone(): * | |
| 167 | { | |
| 168 | 1 | return new LinearQueue(data); |
| 169 | } | |
| 170 | ||
| 171 | /** | |
| 172 | * Returns an iterator over a set of elements. | |
| 173 | * <p>This implementation returns an <code>ArrayIterator</code> object.</p> | |
| 174 | * | |
| 175 | * @return an iterator over a set of elements. | |
| 176 | * @see org.as3collections.iterators.ArrayIterator ArrayIterator | |
| 177 | */ | |
| 178 | override public function iterator(): IIterator | |
| 179 | { | |
| 180 | 1 | return new ArrayIterator(data); |
| 181 | } | |
| 182 | ||
| 183 | /** | |
| 184 | * Inserts the specified element into this queue if it is possible to do so immediately without violating restrictions. | |
| 185 | * When using a restricted queue (like <code>TypedQueue</code> and <code>UniqueQueue</code>), this method is generally preferable to <code>add</code>, which can fail to insert an element only by throwing an error. | |
| 186 | * <p>This implementation adds the element to the tail of the queue.</p> | |
| 187 | * | |
| 188 | * @param element the element to add. | |
| 189 | * @return <code>true</code> if the element was added to this queue, else <code>false</code>. | |
| 190 | */ | |
| 191 | override public function offer(element:*): Boolean | |
| 192 | { | |
| 193 | 1 | if (element == null) return false; |
| 194 | ||
| 195 | 1 | data.push(element); |
| 196 | 1 | elementAdded(element); |
| 197 | 1 | return true; |
| 198 | } | |
| 199 | ||
| 200 | /** | |
| 201 | * Retrieves, but does not remove, the head of this queue, or returns <code>null</code> if this queue is empty. | |
| 202 | * | |
| 203 | * @return the head of this queue, or <code>null</code> if this queue is empty. | |
| 204 | */ | |
| 205 | override public function peek(): * | |
| 206 | { | |
| 207 | 1 | if (isEmpty()) return null; |
| 208 | 1 | return data[0]; |
| 209 | } | |
| 210 | ||
| 211 | /** | |
| 212 | * Retrieves and removes the head of this queue, or returns <code>null</code> if this queue is empty. | |
| 213 | * | |
| 214 | * @return the head of this queue, or <code>null</code> if this queue is empty. | |
| 215 | */ | |
| 216 | override public function poll(): * | |
| 217 | { | |
| 218 | 1 | if (isEmpty()) return null; |
| 219 | ||
| 220 | 1 | var e:* = data.shift(); |
| 221 | 1 | elementRemoved(e); |
| 222 | ||
| 223 | 1 | return e; |
| 224 | } | |
| 225 | ||
| 226 | } | |
| 227 | ||
| 228 | } |