| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| UniqueList |
|
| 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.lists |
| 31 | { | |
| 32 | import org.as3collections.ICollection; | |
| 33 | import org.as3collections.IList; | |
| 34 | import org.as3collections.IListIterator; | |
| 35 | import org.as3collections.UniqueCollection; | |
| 36 | import org.as3collections.iterators.ListIterator; | |
| 37 | import org.as3collections.utils.CollectionUtil; | |
| 38 | ||
| 39 | /** | |
| 40 | * <code>UniqueList</code> works as a wrapper for a <code>IList</code> object. | |
| 41 | * It does not allow duplicated elements in the collection. | |
| 42 | * It stores the <code>wrapList</code> constructor's argument in the <code>wrappedList</code> variable. | |
| 43 | * So every method call to this class is forwarded to the <code>wrappedList</code> object. | |
| 44 | * The methods that need to be checked for duplication are previously validated before forward the call. | |
| 45 | * No error is thrown by the validation of duplication. | |
| 46 | * The calls that are forwarded to the <code>wrappedList</code> returns the return of the <code>wrappedList</code> call. | |
| 47 | * <p>You can also create unique and typed lists. See below the link "ListUtil.getUniqueTypedList()".</p> | |
| 48 | * | |
| 49 | * @example | |
| 50 | * | |
| 51 | * <listing version="3.0"> | |
| 52 | * import org.as3collections.IList; | |
| 53 | * import org.as3collections.IListIterator; | |
| 54 | * import org.as3collections.lists.ArrayList; | |
| 55 | * import org.as3collections.lists.UniqueList; | |
| 56 | * import org.as3collections.utils.ListUtil; | |
| 57 | * | |
| 58 | * var l1:IList = new ArrayList([3, 5, 1, 7]); | |
| 59 | * | |
| 60 | * var list1:IList = new UniqueList(l1); // you can use this way | |
| 61 | * | |
| 62 | * //var list1:IList = ListUtil.getUniqueList(l1); // or you can use this way | |
| 63 | * | |
| 64 | * list1 // [3,5,1,7] | |
| 65 | * list1.size() // 4 | |
| 66 | * | |
| 67 | * list1.addAt(1, 4) // true | |
| 68 | * list1 // [3,4,5,1,7] | |
| 69 | * list1.size() // 5 | |
| 70 | * | |
| 71 | * list1.addAt(2, 3) // false | |
| 72 | * list1 // [3,4,5,1,7] | |
| 73 | * list1.size() // 5 | |
| 74 | * | |
| 75 | * list1.add(5) // false | |
| 76 | * list1 // [3,4,5,1,7] | |
| 77 | * list1.size() // 5 | |
| 78 | * | |
| 79 | * var it:IListIterator = list1.listIterator(); | |
| 80 | * var e:int; | |
| 81 | * | |
| 82 | * while (it.hasNext()) | |
| 83 | * { | |
| 84 | * | |
| 85 | * e = it.next() | |
| 86 | * e // 3 | |
| 87 | * | |
| 88 | * e = it.next() | |
| 89 | * e // 4 | |
| 90 | * | |
| 91 | * e = it.next() | |
| 92 | * e // 5 | |
| 93 | * | |
| 94 | * if (e == 5) | |
| 95 | * { | |
| 96 | * it.add(0) | |
| 97 | * | |
| 98 | * list1 // [3,4,5,0,1,7] | |
| 99 | * list1.size() // 6 | |
| 100 | * } | |
| 101 | * | |
| 102 | * e = it.next() | |
| 103 | * e // 1 | |
| 104 | * | |
| 105 | * if (e == 1) | |
| 106 | * { | |
| 107 | * it.add(3) | |
| 108 | * | |
| 109 | * list1 // [3,4,5,0,1,7] | |
| 110 | * list1.size() // 6 | |
| 111 | * } | |
| 112 | * | |
| 113 | * e = it.next() | |
| 114 | * e // 7 | |
| 115 | * } | |
| 116 | * | |
| 117 | * list1 // [3,4,5,0,1,7] | |
| 118 | * list1.size() // 6 | |
| 119 | * | |
| 120 | * var l2:IList = new ArrayList([1, 2, 3, 4, 5, 1, 3, 5]); | |
| 121 | * | |
| 122 | * var list2:IList = new UniqueList(l2); // you can use this way | |
| 123 | * | |
| 124 | * //var list2:IList = ListUtil.getUniqueList(l2); // or you can use this way | |
| 125 | * | |
| 126 | * list2 // [1,2,3,4,5] | |
| 127 | * list2.size() // 5 | |
| 128 | * </listing> | |
| 129 | * | |
| 130 | * @see org.as3collections.utils.ListUtil#getUniqueList() ListUtil.getUniqueList() | |
| 131 | * @see org.as3collections.utils.ListUtil#getUniqueTypedList() ListUtil.getUniqueTypedList() | |
| 132 | * @author Flávio Silva | |
| 133 | */ | |
| 134 | public class UniqueList extends UniqueCollection implements IList | |
| 135 | { | |
| 136 | /** | |
| 137 | * Returns the return of the call <code>wrappedList.modCount</code>. | |
| 138 | */ | |
| 139 | public function get modCount(): int { return wrappedList.modCount; } | |
| 140 | ||
| 141 | /** | |
| 142 | * @private | |
| 143 | */ | |
| 144 | protected function get wrappedList(): IList { return wrappedCollection as IList; } | |
| 145 | ||
| 146 | /** | |
| 147 | * Constructor, creates a new <code>UniqueList</code> object. | |
| 148 | * | |
| 149 | * @param wrapList the target list to wrap. | |
| 150 | * @throws ArgumentError if the <code>wrappedList</code> argument is <code>null</code>. | |
| 151 | */ | |
| 152 | public function UniqueList(wrapList:IList) | |
| 153 | { | |
| 154 | 1 | super(wrapList); |
| 155 | 1 | } |
| 156 | ||
| 157 | /** | |
| 158 | * If the specified collection is empty returns <code>false</code>. | |
| 159 | * Otherwise, it clones the specified collection, removes from the cloned collection all elements that already are in the <code>wrappedList</code> and removes all duplicates. | |
| 160 | * Then it forwards the call to <code>wrappedList.addAllAt</code> sending the cloned (and filtered) collection. | |
| 161 | * | |
| 162 | * @param index index at which to insert the first element from the specified collection. | |
| 163 | * @param collection the collection to forward to <code>wrappedList.addAllAt</code>. | |
| 164 | * @throws ArgumentError if the specified collection contains a <code>null</code> element and <code>wrappedList</code> does not permit <code>null</code> elements, or if the specified collection is <code>null</code>. | |
| 165 | * @return <code>false</code> if the specified collection is <code>null</code> or empty. Otherwise returns the return of the call <code>wrappedList.addAllAt</code>. | |
| 166 | */ | |
| 167 | public function addAllAt(index:int, collection:ICollection): Boolean | |
| 168 | { | |
| 169 | 1 | if (!collection) throw new ArgumentError("The 'collection' argument must not be 'null'."); |
| 170 | 1 | if (collection.isEmpty()) return false; |
| 171 | ||
| 172 | 1 | var c:ICollection = collection.clone(); |
| 173 | 1 | filterCollection(c); |
| 174 | ||
| 175 | 1 | if (c.isEmpty()) return false; |
| 176 | ||
| 177 | 1 | return wrappedList.addAllAt(index, c); |
| 178 | } | |
| 179 | ||
| 180 | /** | |
| 181 | * If <code>wrappedList.contains(element)</code> returns <code>true</code> then returns <code>false</code>. | |
| 182 | * Otherwise, it forwards the call to <code>wrappedList.addAt</code>. | |
| 183 | * | |
| 184 | * @param index index at which the specified element is to be inserted. | |
| 185 | * @param element the element to be added. | |
| 186 | * @return <code>false</code> if <code>wrappedList.contains(element)</code> returns <code>true</code>. Otherwise returns the return of the call <code>wrappedList.addAt</code>. | |
| 187 | */ | |
| 188 | public function addAt(index:int, element:*): Boolean | |
| 189 | { | |
| 190 | 1 | if (wrappedList.contains(element)) return false; |
| 191 | 1 | return wrappedList.addAt(index, element); |
| 192 | } | |
| 193 | ||
| 194 | /** | |
| 195 | * Creates and return a new <code>UniqueList</code> object with the clone of the <code>wrappedList</code> object. | |
| 196 | * | |
| 197 | * @return a new <code>UniqueList</code> object with the clone of the <code>wrappedList</code> object. | |
| 198 | */ | |
| 199 | override public function clone(): * | |
| 200 | { | |
| 201 | 1 | return new UniqueList(wrappedList.clone()); |
| 202 | } | |
| 203 | ||
| 204 | /** | |
| 205 | * This method uses <code>CollectionUtil.equalConsideringOrder</code> method to perform equality, sending this list and <code>other</code> argument. | |
| 206 | * | |
| 207 | * @param other the object to be compared for equality. | |
| 208 | * @return <code>true</code> if the arbitrary evaluation considers the objects equal. | |
| 209 | * @see org.as3collections.utils.CollectionUtil#equalConsideringOrder() CollectionUtil.equalConsideringOrder() | |
| 210 | */ | |
| 211 | override public function equals(other:*): Boolean | |
| 212 | { | |
| 213 | 1 | return CollectionUtil.equalConsideringOrder(this, other); |
| 214 | } | |
| 215 | ||
| 216 | /** | |
| 217 | * Forwards the call to <code>wrappedList.getAt</code>. | |
| 218 | * | |
| 219 | * @param index | |
| 220 | * @return the return of the call <code>wrappedList.getAt</code>. | |
| 221 | */ | |
| 222 | public function getAt(index:int): * | |
| 223 | { | |
| 224 | 1 | return wrappedList.getAt(index); |
| 225 | } | |
| 226 | ||
| 227 | /** | |
| 228 | * Forwards the call to <code>wrappedList.indexOf</code>. | |
| 229 | * | |
| 230 | * @param element | |
| 231 | * @param fromIndex | |
| 232 | * @return the return of the call <code>wrappedList.indexOf</code>. | |
| 233 | */ | |
| 234 | public function indexOf(element:*, fromIndex:int = 0): int | |
| 235 | { | |
| 236 | 1 | return wrappedList.indexOf(element, fromIndex); |
| 237 | } | |
| 238 | ||
| 239 | /** | |
| 240 | * Forwards the call to <code>wrappedList.lastIndexOf</code>. | |
| 241 | * | |
| 242 | * @param element | |
| 243 | * @param fromIndex | |
| 244 | * @return the return of the call <code>wrappedList.lastIndexOf</code>. | |
| 245 | */ | |
| 246 | public function lastIndexOf(element:*, fromIndex:int = 0x7fffffff): int | |
| 247 | { | |
| 248 | 1 | return wrappedList.lastIndexOf(element, fromIndex); |
| 249 | } | |
| 250 | ||
| 251 | /** | |
| 252 | * Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. | |
| 253 | * The specified index indicates the first element that would be returned by an initial call to <code>next</code>. | |
| 254 | * An initial call to <code>previous</code> would return the element with the specified index minus one. | |
| 255 | * <p>This implementation returns an <code>ListIterator</code> object.</p> | |
| 256 | * | |
| 257 | * @param index index of first element to be returned from the list iterator (by a call to the <code>next</code> method) | |
| 258 | * @return a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. | |
| 259 | * @see org.as3collections.iterators.ListIterator ListIterator | |
| 260 | */ | |
| 261 | public function listIterator(index:int = 0): IListIterator | |
| 262 | { | |
| 263 | 1 | return new ListIterator(this, index); |
| 264 | } | |
| 265 | ||
| 266 | /** | |
| 267 | * Forwards the call to <code>wrappedList.removeAt</code>. | |
| 268 | * | |
| 269 | * @param index | |
| 270 | * @return the return of the call <code>wrappedList.removeAt</code>. | |
| 271 | */ | |
| 272 | public function removeAt(index:int): * | |
| 273 | { | |
| 274 | 1 | return wrappedList.removeAt(index); |
| 275 | } | |
| 276 | ||
| 277 | /** | |
| 278 | * Forwards the call to <code>wrappedList.removeRange</code>. | |
| 279 | * | |
| 280 | * @param fromIndex | |
| 281 | * @param toIndex | |
| 282 | * @return the return of the call <code>wrappedList.removeRange</code>. | |
| 283 | */ | |
| 284 | public function removeRange(fromIndex:int, toIndex:int): ICollection | |
| 285 | { | |
| 286 | 1 | return wrappedList.removeRange(fromIndex, toIndex); |
| 287 | } | |
| 288 | ||
| 289 | /** | |
| 290 | * Forwards the call to <code>wrappedList.reverse</code>. | |
| 291 | */ | |
| 292 | public function reverse(): void | |
| 293 | { | |
| 294 | 1 | wrappedList.reverse(); |
| 295 | 1 | } |
| 296 | ||
| 297 | /** | |
| 298 | * If <code>wrappedList.contains(element)</code> returns <code>true</code> then returns <code>false</code>. Otherwise, it forwards the call to <code>wrappedList.setAt</code>. | |
| 299 | * | |
| 300 | * @param index | |
| 301 | * @param element | |
| 302 | * @return <code>false</code> if <code>wrappedList.contains(element)</code> returns <code>true</code>. Otherwise returns the return of the call <code>wrappedList.setAt</code>. | |
| 303 | */ | |
| 304 | public function setAt(index:int, element:*): * | |
| 305 | { | |
| 306 | 1 | if (wrappedList.contains(element)) return false; |
| 307 | 1 | return wrappedList.setAt(index, element); |
| 308 | } | |
| 309 | ||
| 310 | /** | |
| 311 | * Returns a new <code>UniqueList(wrappedList.subList(fromIndex, toIndex))</code>. | |
| 312 | * <p>Modifications in the returned <code>UniqueList</code> object does not affect this list.</p> | |
| 313 | * | |
| 314 | * @param fromIndex the index to start retrieving elements (inclusive). | |
| 315 | * @param toIndex the index to stop retrieving elements (exclusive). | |
| 316 | * @throws org.as3collections.errors.IndexOutOfBoundsError if <code>fromIndex</code> or <code>toIndex</code> is out of range <code>(index < 0 || index > size())</code>. | |
| 317 | * @return a new <code>UniqueList(wrappedList.subList(fromIndex, toIndex))</code>. | |
| 318 | */ | |
| 319 | public function subList(fromIndex:int, toIndex:int): IList | |
| 320 | { | |
| 321 | 1 | return new UniqueList(wrappedList.subList(fromIndex, toIndex)); |
| 322 | } | |
| 323 | ||
| 324 | } | |
| 325 | ||
| 326 | } |