Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UniqueSortedList |
|
| 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.IList; | |
33 | import org.as3collections.ISortedList; | |
34 | import org.as3coreaddendum.system.IComparator; | |
35 | import org.as3utils.ReflectionUtil; | |
36 | ||
37 | /** | |
38 | * <code>UniqueSortedList</code> works as a wrapper for a <code>ISortedList</code> object. | |
39 | * It does not allow duplicated elements in the collection. | |
40 | * It stores the <code>wrapList</code> constructor's argument in the <code>wrappedList</code> variable. | |
41 | * So every method call to this class is forwarded to the <code>wrappedList</code> object. | |
42 | * The methods that need to be checked for duplication are previously validated before forward the call. | |
43 | * No error is thrown by the validation of duplication. | |
44 | * The calls that are forwarded to the <code>wrappedList</code> returns the return of the <code>wrappedList</code> call. | |
45 | * <p>You can also create unique and typed sorted lists. | |
46 | * See below the link "ListUtil.getUniqueTypedSortedList()".</p> | |
47 | * | |
48 | * @example | |
49 | * | |
50 | * <listing version="3.0"> | |
51 | * import org.as3collections.ISortedList; | |
52 | * import org.as3collections.IListIterator; | |
53 | * import org.as3collections.lists.SortedArrayList; | |
54 | * import org.as3collections.lists.UniqueSortedList; | |
55 | * import org.as3collections.utils.ListUtil; | |
56 | * | |
57 | * var l1:ISortedList = new SortedArrayList([3, 5, 1, 7], null, Array.NUMERIC); | |
58 | * | |
59 | * var list1:ISortedList = new UniqueSortedList(l1); // you can use this way | |
60 | * | |
61 | * //var list1:ISortedList = ListUtil.getUniqueSortedList(l1); // or you can use this way | |
62 | * | |
63 | * list1 // [1,3,5,7] | |
64 | * list1.size() // 4 | |
65 | * | |
66 | * list1.addAt(1, 4) // true | |
67 | * list1 // [1,3,4,5,7] | |
68 | * list1.size() // 5 | |
69 | * | |
70 | * list1.addAt(2, 3) // false | |
71 | * list1 // [1,3,4,5,7] | |
72 | * list1.size() // 5 | |
73 | * | |
74 | * list1.add(5) // false | |
75 | * list1 // [1,3,4,5,7] | |
76 | * list1.size() // 5 | |
77 | * </listing> | |
78 | * | |
79 | * @see org.as3collections.utils.ListUtil#getUniqueSortedList() ListUtil.getUniqueSortedList() | |
80 | * @see org.as3collections.utils.ListUtil#getUniqueTypedSortedList() ListUtil.getUniqueTypedSortedList() | |
81 | * @author Flávio Silva | |
82 | */ | |
83 | public class UniqueSortedList extends UniqueList implements ISortedList | |
84 | { | |
85 | /** | |
86 | * Defines the <code>wrappedList</code> comparator object to be used automatically to sort. | |
87 | * <p>If this value change the <code>wrappedList</code> is automatically reordered with the new value.</p> | |
88 | */ | |
89 | public function get comparator(): IComparator { return wrappedSortedList.comparator; } | |
90 | ||
91 | public function set comparator(value:IComparator): void { wrappedSortedList.comparator = value; } | |
92 | ||
93 | /** | |
94 | * Defines the <code>wrappedList</code> options to be used automatically to sort. | |
95 | * <p>If this value change the list is automatically reordered with the new value.</p> | |
96 | */ | |
97 | public function get options(): uint { return wrappedSortedList.options; } | |
98 | ||
99 | public function set options(value:uint): void { wrappedSortedList.options = value; } | |
100 | ||
101 | /** | |
102 | * @private | |
103 | */ | |
104 | protected function get wrappedSortedList(): ISortedList { return wrappedList as ISortedList; } | |
105 | ||
106 | /** | |
107 | * Constructor, creates a new <code>TypedList</code> object. | |
108 | * | |
109 | * @param wrapList the target list to wrap. | |
110 | * @param type the type of the elements allowed by this list. | |
111 | * @throws ArgumentError if the <code>wrapList</code> argument is <code>null</code>. | |
112 | * @throws ArgumentError if the <code>type</code> argument is <code>null</code>. | |
113 | * @throws org.as3coreaddendum.errors.ClassCastError if the types of one or more elements in the <code>wrapList</code> argument are incompatible with the <code>type</code> argument. | |
114 | */ | |
115 | public function UniqueSortedList(wrapList:ISortedList) | |
116 | { | |
117 | 1 | super(wrapList); |
118 | 1 | } |
119 | ||
120 | /** | |
121 | * Creates and return a new <code>UniqueSortedList</code> object with the clone of the <code>wrappedMap</code> object. | |
122 | * | |
123 | * @return a new <code>UniqueSortedList</code> object with the clone of the <code>wrappedMap</code> object. | |
124 | */ | |
125 | override public function clone(): * | |
126 | { | |
127 | 1 | return new UniqueSortedList(wrappedSortedList.clone()); |
128 | } | |
129 | ||
130 | /** | |
131 | * Performs an arbitrary, specific evaluation of equality between this object and the <code>other</code> object. | |
132 | * <p>This implementation considers two differente objects equal if:</p> | |
133 | * <p> | |
134 | * <ul><li>object A and object B are instances of the same class (i.e. if they have <b>exactly</b> the same type)</li> | |
135 | * <li>object A contains all elements of object B</li> | |
136 | * <li>object B contains all elements of object A</li> | |
137 | * <li>elements have exactly the same order</li> | |
138 | * <li>object A and object B has the same type of comparator</li> | |
139 | * <li>object A and object B has the same options</li> | |
140 | * </ul></p> | |
141 | * <p>This implementation takes care of the order of the elements in the list. | |
142 | * So, for two lists are equal the order of elements returned by the iterator must be equal.</p> | |
143 | * | |
144 | * @param other the object to be compared for equality. | |
145 | * @return <code>true</code> if the arbitrary evaluation considers the objects equal. | |
146 | */ | |
147 | override public function equals(other:*): Boolean | |
148 | { | |
149 | 1 | if (this == other) return true; |
150 | ||
151 | 1 | if (!ReflectionUtil.classPathEquals(this, other)) return false; |
152 | ||
153 | 1 | var l:ISortedList = other as ISortedList; |
154 | ||
155 | 1 | if (options != l.options) return false; |
156 | 1 | if (!comparator && l.comparator) return false; |
157 | 1 | if (comparator && !l.comparator) return false; |
158 | 1 | if (!ReflectionUtil.classPathEquals(comparator, l.comparator)) return false; |
159 | ||
160 | 1 | return super.equals(other); |
161 | } | |
162 | ||
163 | /** | |
164 | * Forwards the call to <code>wrappedList.sort</code>. | |
165 | * | |
166 | * @param compare | |
167 | * @param options | |
168 | * @return | |
169 | */ | |
170 | public function sort(compare:Function = null, options:uint = 0): Array | |
171 | { | |
172 | 1 | return wrappedSortedList.sort(compare, options); |
173 | } | |
174 | ||
175 | /** | |
176 | * Forwards the call to <code>wrappedList.sortOn</code>. | |
177 | * | |
178 | * @param fieldName | |
179 | * @param options | |
180 | * @return | |
181 | */ | |
182 | public function sortOn(fieldName:*, options:* = null): Array | |
183 | { | |
184 | 1 | return wrappedSortedList.sortOn(fieldName, options); |
185 | } | |
186 | ||
187 | /** | |
188 | * Forwards the call to <code>wrappedList.subList</code>. | |
189 | * | |
190 | * @param fromIndex | |
191 | * @param toIndex | |
192 | * @return | |
193 | */ | |
194 | override public function subList(fromIndex:int, toIndex:int): IList | |
195 | { | |
196 | 1 | var subList:IList = wrappedSortedList.subList(fromIndex, toIndex); |
197 | 1 | var sortedSubList:ISortedList = new SortedArrayList(subList.toArray(), comparator, options); |
198 | ||
199 | 1 | return new UniqueSortedList(sortedSubList); |
200 | } | |
201 | ||
202 | } | |
203 | ||
204 | } |