Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
QueueUtil |
|
| 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.utils |
31 | { | |
32 | import flash.errors.IllegalOperationError; | |
33 | ||
34 | import org.as3collections.IQueue; | |
35 | import org.as3collections.queues.TypedQueue; | |
36 | import org.as3collections.queues.UniqueQueue; | |
37 | ||
38 | /** | |
39 | * A utility class to work with implementations of the <code>IQueue</code> interface. | |
40 | * | |
41 | * @author Flávio Silva | |
42 | */ | |
43 | public class QueueUtil | |
44 | { | |
45 | /** | |
46 | * <code>QueueUtil</code> is a static class and shouldn't be instantiated. | |
47 | * | |
48 | * @throws IllegalOperationError <code>QueueUtil</code> is a static class and shouldn't be instantiated. | |
49 | */ | |
50 | public function QueueUtil() | |
51 | 1 | { |
52 | 1 | throw new IllegalOperationError("QueueUtil is a static class and shouldn't be instantiated."); |
53 | } | |
54 | ||
55 | /** | |
56 | * Returns a new <code>TypedQueue</code> with the <code>wrapList</code> argument wrapped. | |
57 | * | |
58 | * @example | |
59 | * | |
60 | * <listing version="3.0"> | |
61 | * import org.as3collections.IQueue; | |
62 | * import org.as3collections.queues.LinearQueue; | |
63 | * import org.as3collections.queues.TypedQueue; | |
64 | * import org.as3collections.utils.QueueUtil; | |
65 | * | |
66 | * var q1:IQueue = new LinearQueue([1, 5, 3, 7]); | |
67 | * | |
68 | * var queue1:IQueue = QueueUtil.getTypedQueue(q1); | |
69 | * | |
70 | * queue1 // [1,5,3,7] | |
71 | * queue1.size(): // 4 | |
72 | * queue1.isEmpty() // false | |
73 | * | |
74 | * queue1.poll() // 1 | |
75 | * queue1 // [5,3,7] | |
76 | * | |
77 | * queue1.offer(2) // true | |
78 | * queue1 // [5,3,7,2] | |
79 | * | |
80 | * queue1.offer(5) // true | |
81 | * queue1 // [5,3,7,2,5] | |
82 | * | |
83 | * queue1.offer("a") // false | |
84 | * queue1 // [5,3,7,2,5] | |
85 | * | |
86 | * queue1.add("a") // ClassCastError: Invalid element type. element: a | type: String | expected type: int | |
87 | * </listing> | |
88 | * | |
89 | * @param wrapQueue the target queue to be wrapped by the <code>TypedQueue</code>. | |
90 | * @param type the type of the elements allowed by the returned <code>TypedQueue</code>. | |
91 | * @throws ArgumentError if the <code>queue</code> argument is <code>null</code>. | |
92 | * @throws ArgumentError if the <code>type</code> argument is <code>null</code>. | |
93 | * @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. | |
94 | * @return a new <code>TypedQueue</code> with the <code>queue</code> argument wrapped. | |
95 | */ | |
96 | public static function getTypedQueue(wrapQueue:IQueue, type:*): TypedQueue | |
97 | { | |
98 | 1 | return new TypedQueue(wrapQueue, type); |
99 | } | |
100 | ||
101 | /** | |
102 | * Returns a new <code>UniqueQueue</code> with the <code>wrapQueue</code> argument wrapped. | |
103 | * | |
104 | * @example | |
105 | * | |
106 | * <listing version="3.0"> | |
107 | * import org.as3collections.IQueue; | |
108 | * import org.as3collections.queues.LinearQueue; | |
109 | * import org.as3collections.queues.UniqueQueue; | |
110 | * import org.as3collections.utils.QueueUtil; | |
111 | * | |
112 | * var q1:IQueue = new LinearQueue([1, 5, 3, 7]); | |
113 | * | |
114 | * var queue1:IQueue = new UniqueQueue(q1); // you can use this way | |
115 | * | |
116 | * //var queue1:IQueue = QueueUtil.getUniqueQueue(q1); // or you can use this way | |
117 | * | |
118 | * queue1 // [1,5,3,7] | |
119 | * queue1.size() // 4 | |
120 | * queue1.isEmpty() // false | |
121 | * | |
122 | * queue1.poll() // 1 | |
123 | * queue1 // [5,3,7] | |
124 | * | |
125 | * queue1.offer(2) // true | |
126 | * queue1 // [5,3,7,2] | |
127 | * | |
128 | * queue1.offer(5) // false | |
129 | * queue1 // [5,3,7,2] | |
130 | * | |
131 | * queue1.add(5) // Error: UniqueQueue is a unique queue and does not allow duplicated elements. Requested element: 5 | |
132 | * </listing> | |
133 | * | |
134 | * @param wrapQueue the target queue to be wrapped by the <code>UniqueQueue</code>. | |
135 | * @throws ArgumentError if the <code>queue</code> argument is <code>null</code>. | |
136 | * @return a new <code>UniqueQueue</code> with the <code>queue</code> argument wrapped. | |
137 | */ | |
138 | public static function getUniqueQueue(wrapQueue:IQueue): UniqueQueue | |
139 | { | |
140 | 1 | return new UniqueQueue(wrapQueue); |
141 | } | |
142 | ||
143 | /** | |
144 | * Returns a new <code>TypedQueue</code> that wraps a new <code>UniqueQueue</code> that wraps the <code>wrapQueue</code> argument. | |
145 | * <p>The result will be a unique and typed array queue, despite of the return type <code>TypedQueue</code>.</p> | |
146 | * | |
147 | * @example | |
148 | * | |
149 | * <listing version="3.0"> | |
150 | * import org.as3collections.IQueue; | |
151 | * import org.as3collections.queues.Queue; | |
152 | * import org.as3collections.queues.TypedQueue; | |
153 | * import org.as3collections.utils.QueueUtil; | |
154 | * | |
155 | * var q1:IQueue = new LinearQueue([1, 5, 3, 7]); | |
156 | * | |
157 | * var queue1:IQueue = QueueUtil.getUniqueTypedQueue(q1, int); | |
158 | * | |
159 | * queue1 // [1,5,3,7] | |
160 | * queue1.size() // 4 | |
161 | * queue1.isEmpty() // false | |
162 | * | |
163 | * queue1.poll() // 1 | |
164 | * queue1 // [5,3,7] | |
165 | * | |
166 | * queue1.offer(2) // true | |
167 | * queue1 // [5,3,7,2] | |
168 | * | |
169 | * queue1.offer(5) // false | |
170 | * queue1 // [5,3,7,2] | |
171 | * | |
172 | * queue1.add(5) // Error: UniqueQueue is a unique queue and does not allow duplicated elements. Requested element: 5 | |
173 | * | |
174 | * queue1.offer("a") // false | |
175 | * queue1 // [5,3,7,2] | |
176 | * | |
177 | * queue1.add("a") // ClassCastError: Invalid element type. element: a | type: String | expected type: int | |
178 | * </listing> | |
179 | * | |
180 | * @param wrapQueue the target queue to be wrapped. | |
181 | * @param type the type of the elements allowed by the returned <code>TypedQueue</code>. | |
182 | * @throws ArgumentError if the <code>queue</code> argument is <code>null</code>. | |
183 | * @throws ArgumentError if the <code>type</code> argument is <code>null</code>. | |
184 | * @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. | |
185 | * @return a new <code>TypedQueue</code> with the <code>queue</code> argument wrapped. | |
186 | */ | |
187 | public static function getUniqueTypedQueue(wrapQueue:IQueue, type:*): TypedQueue | |
188 | { | |
189 | 1 | return new TypedQueue(new UniqueQueue(wrapQueue), type); |
190 | } | |
191 | ||
192 | } | |
193 | ||
194 | } |