-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.util.ArrayList.js
200 lines (175 loc) · 6.85 KB
/
js.util.ArrayList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
js.util = (js.util || {});
// TODO: <generics>
// http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
js.util.ArrayList = new js.lang.Class() ({
/** Utility class providing common java-like array-list functionality to help keep code D-R-Y. **/
__init__ : function( array ) {
/** Initializer for ArrayList **/
this._array = (array ? array.slice() : []);
},
add : function( element ) {
/** Add given element to this list. **/
this._array.push( element );
},
addAll : function( collection ) {
/** Add given collection of items to this list.
(Collection being either another ArrayList or Dictionary, or a subclass of either.) **/
var that = this;
collection.iterate(function( k, v ) {
that.add( v );
});
},
clear : function() {
/** Clear contents of this list. **/
this._array = [];
},
contains : function( element, comparator ) {
/** Check if this list contains given element,
optionally using a callback to compare items. **/
return( this.indexOf( element, comparator ) !== -1 );
},
containsAll : function( collection, comparator ) {
/** Check if this list contains every item in given collection,
optionally using a callback to compare items. **/
var that = this, containsAll = true;
collection.iterate( function( k, v ) {
if( ! that.contains( v, comparator ) ) {
containsAll = false;
return( true );
}
});
return( containsAll );
},
get : function( index ) {
/** Retrieve an item at given index. **/
return( this._array[ index ] );
},
indexOf : function( element, comparator ) {
/** Get the index the given element exists in this list,
optionally using a callback to compare items. **/
var index = -1, compare = this.getComparator( comparator );
this.iterate( function( k, v ) {
if( compare( element, v ) ) {
index = k;
return( true );
}
});
return( index );
},
isEmpty : function() {
/** Check if this list contains zero elements. **/
return( this._array.length === 0 );
},
removeAt : function( index ) {
/** Remove element existing at given index in this list. **/
this._array.splice( index, 1 );
},
remove : function( element, comparator ) {
/** Remove given element from this list,
optionally using a callback to compare items. **/
var key, compare = this.getComparator( comparator );
this.iterate( function( k, v ) {
if( compare( element, v ) ) {
key = k;
return( true );
}
});
this.removeAt( key );
},
removeAll : function( collection, comparator ) {
/** Remove every item in given collection from this list,
optionally using a callback to compare items. **/
var that = this;
collection.iterate( function( k, v ) {
that.remove( v, comparator );
});
},
retainAll : function( collection, comparator ) {
/** Remove every item not in given collection from this list,
optionally using a callback to compare items. **/
var that = this;
this.iterate( function( k, v ) {
if( ! collection.contains( v, comparator ) ) {
that.remove( v, comparator );
}
});
},
set : function( index, element ) {
/** Assign given element at given index in this list. **/
this._array[ index ] = element;
},
size : function() {
/** Retrieve the size of this list. **/
return( this._array.length );
},
subList : function( fromIndex, toIndex ) {
/** Retrieve an ArrayList containing references to elements
from given fromIndex to given toIndex. **/
return( new js.util.ArrayList( this._array.slice( fromIndex, toIndex ) ) );
},
toArray : function() {
/** Retrieve an instance of Array containing the same list of references. **/
return( this._array.slice() );
},
getComparator : function( comparator ) {
/** Retrieve this instance's default comparator as fallback if given comparator is not defined. **/
return( (comparator !== undefined) ? comparator : this._defaultComparator );
},
_defaultComparator : function( a, b ) {
/** Default method of comparing two values within this list. **/
return( a === b );
},
iterate : function( callback ) {
/** Convenient, unified method of iterating elements in this list.
This pattern is common to all collection classes. **/
var array = this._array;
for( var i = (array.length - 1); i >= 0; i-- ) {
if( callback( i, array[ i ] ) ) {
break;
}
}
},
reduce : function( callback ) {
/** Reduce this list to a smaller list. Given callback is invoked for each element.
The element is removed if given callback returns false value (false, null, undefined). **/
var that = this;
this.iterate( function( k, v ) {
if( ! callback( k, v ) ) {
that.removeAt( k );
}
});
}
})
.Static({
Iterate : function( list, callback ) {
/** Iterate given list, invoking given callback for each element.
Helper method to avoid the requirement to instantiate this class for one-off use-cases. **/
js.util.ArrayList.prototype.iterate.call( { _array : list }, callback );
}
});
js.util.Set = new js.lang.Class( js.util.ArrayList ) ({
/** Utility class providing common java-like array-list functionality.
Elements are only added if they do not already exist within this list. **/
__init__ : function( array ) {
/** Initializer for Set. **/
js.util.ArrayList.__init__.call( this );
if( array ) {
this.addAll( new js.util.ArrayList(array) );
}
},
add : function( element, comparator ) {
/** Add given element to this list if not already exists,
optionally using a callback to compare items. **/
if( ! this.contains( element, comparator ) ) {
js.util.ArrayList.prototype.add.call( this, element );
}
},
addAll : function( collection, comparator ) {
/** Add given collection of items to this list if each not already exists.
(Collection being either another ArrayList or Dictionary, or a subclass of either.) **/
var that = this;
collection.iterate(function( k, v ) {
that.add( v, comparator );
});
}
});