summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMutableArray.java
blob: c75de0158905d50063ae491fe8abffde7e84af89 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.foundation;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * NSMutableArray extends NSArray to allow modification.
 *
 * @author michael@mpowers.net
 * @author $Author: cgruber $
 * @version $Revision: 893 $
 */
public class NSMutableArray extends NSArray {
	/**
	 * Returns an NSArray backed by the specified List. This is useful to "protect"
	 * an internal representation that is returned by a method of return type
	 * NSArray.
	 */
	public static NSMutableArray mutableArrayBackedByList(List aList) {
		return new NSMutableArray(aList, null);
	}

	NSMutableArray(List aList, Object ignored) // differentiates
	{
		super(aList, ignored);
	}

	/**
	 * Default constructor returns an empty array.
	 */
	public NSMutableArray() {
		super();
//System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() );
	}

	/**
	 * Constructor with a size hint.
	 */
	public NSMutableArray(int aSize) {
		super();
//System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() );
	}

	/**
	 * Produces an array containing only the specified object.
	 */
	public NSMutableArray(Object anObject) {
		super(anObject);
//System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() );
	}

	/**
	 * Produces an array containing the specified objects.
	 */
	public NSMutableArray(Object[] anArray) {
		super(anArray);
//System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() );
	}

	/**
	 * Produces an array containing the objects in the specified collection.
	 */
	public NSMutableArray(Collection aCollection) {
		super(aCollection);
//System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() );
	}

	/**
	 * Removes the last object from the array.
	 */
	public void removeLastObject() {
		list.remove(count() - 1);
	}

	/**
	 * Removes the object at the specified index.
	 */
	public void removeObjectAtIndex(int index) {
		list.remove(index);
	}

	/**
	 * Adds all objects in the specified collection.
	 */
	public void addObjectsFromArray(Collection aCollection) {
		list.addAll(aCollection);
	}

	/**
	 * Removes all objects from the array.
	 */
	public void removeAllObjects() {
		list.clear();
	}

	/**
	 * Removes all objects equivalent to the specified object within the range of
	 * specified indices.
	 */
	public void removeObject(Object anObject, NSRange aRange) {
		if ((anObject == null) || (aRange == null))
			return;

		int loc = aRange.location();
		int max = aRange.maxRange();
		for (int i = loc; i < max; i++) {
			if (anObject.equals(list.get(i))) {
				list.remove(i);
				i = i - 1;
				max = max - 1;
			}
		}
	}

	/**
	 * Removes all instances of the specified object within the range of specified
	 * indices, comparing by reference.
	 */
	public void removeIdenticalObject(Object anObject, NSRange aRange) {
		if ((anObject == null) || (aRange == null))
			return;

		int loc = aRange.location();
		int max = aRange.maxRange();
		for (int i = loc; i < max; i++) {
			if (anObject == list.get(i)) {
				list.remove(i);
				i = i - 1;
				max = max - 1;
			}
		}
	}

	/**
	 * Removes all objects in the specified collection from the array.
	 */
	public void removeObjectsInArray(Collection aCollection) {
		list.removeAll(aCollection);
	}

	/**
	 * Removes all objects in the indices within the specified range from the array.
	 */
	public void removeObjectsInRange(NSRange aRange) {
		if (aRange == null)
			return;

		for (int i = 0; i < aRange.length(); i++) {
			list.remove(aRange.location());
		}
	}

	/**
	 * Replaces objects in the current range with objects from the specified range
	 * of the specified array. If currentRange is larger than otherRange, the extra
	 * objects are removed. If otherRange is larger than currentRange, the extra
	 * objects are added.
	 */
	public void replaceObjectsInRange(NSRange currentRange, List otherArray, NSRange otherRange) {
		if ((currentRange == null) || (otherArray == null) || (otherRange == null))
			return;

		// transform otherRange if out of bounds for array
		if (otherRange.maxRange() > otherArray.size()) {
			// TODO: Test this logic.
			int loc = Math.min(otherRange.location(), otherArray.size() - 1);
			otherRange = new NSRange(loc, otherArray.size() - loc);
		}

		Object o;
		List subList = list.subList(currentRange.location(), currentRange.maxRange());
		int otherIndex = otherRange.location();
		// TODO: Test this logic.
		for (int i = 0; i < subList.size(); i++) {
			if (otherIndex < otherRange.maxRange()) { // set object
				subList.set(i, otherArray.get(otherIndex));
			} else { // remove extra elements from currentRange
				subList.remove(i);
				i--;
			}
			otherIndex++;
		}
		// TODO: Test this logic.
		for (int i = otherIndex; i < otherRange.maxRange(); i++) {
			list.add(otherArray.get(i));
		}
	}

	/**
	 * Clears the current array and then populates it with the contents of the
	 * specified collection.
	 */
	public void setArray(Collection aCollection) {
		list.clear();
		list.addAll(aCollection);
	}

	/**
	 * Sorts this array using the values from the specified selector.
	 */
	public void sortUsingSelector(NSSelector aSelector) {
		// TODO: implement
		throw new UnsupportedOperationException("Not implemented yet.");
	}

	/**
	 * Removes all objects equivalent to the specified object.
	 */
	public void removeObject(Object anObject) {
		list.remove(anObject);
	}

	/**
	 * Removes all occurences of the specified object, comparing by reference.
	 */
	public void removeIdenticalObject(Object anObject) {
		Iterator it = list.iterator();
		while (it.hasNext()) {
			if (it.next() == anObject) {
				it.remove();
			}
		}
	}

	/**
	 * Inserts the specified object into this array at the specified index.
	 */
	public void insertObjectAtIndex(Object anObject, int anIndex) {
		list.add(anIndex, anObject);
	}

	/**
	 * Replaces the object at the specified index with the specified object.
	 */
	public void replaceObjectAtIndex(int anIndex, Object anObject) {
		list.set(anIndex, anObject);
	}

	/**
	 * Adds the specified object to the end of this array.
	 */
	public void addObject(Object anObject) {
		list.add(anObject);
	}

	public Object clone() {
		return new NSMutableArray(list);
	}

	public NSArray immutableClone() {
		return new NSArray(this);
	}

	public NSMutableArray mutableClone() {
		return new NSMutableArray(this);
	}

	// interface List: mutators

	public void add(int index, Object element) {
		list.add(index, element);
	}

	public boolean add(Object o) {
		return list.add(o);
	}

	public boolean addAll(Collection coll) {
		return list.addAll(coll);
	}

	public boolean addAll(int index, Collection c) {
		return list.addAll(index, c);
	}

	public void clear() {
		list.clear();
	}

	public Iterator iterator() {
		return list.iterator();
	}

	public ListIterator listIterator() {
		return list.listIterator();
	}

	public ListIterator listIterator(int index) {
		return list.listIterator();
	}

	public Object remove(int index) {
		return list.remove(index);
	}

	public boolean remove(Object o) {
		return list.remove(o);
	}

	public boolean removeAll(Collection coll) {
		return list.removeAll(coll);
	}

	public boolean retainAll(Collection coll) {
		return list.retainAll(coll);
	}

	public Object set(int index, Object element) {
		return list.set(index, element);
	}

	public List subList(int fromIndex, int toIndex) {
		return list.subList(fromIndex, toIndex);
	}

}

/*
 * $Log$ Revision 1.2 2006/02/16 13:15:00 cgruber Check in all sources in
 * eclipse-friendly maven-enabled packages.
 *
 * Revision 1.8 2005/07/13 14:12:44 cgruber Add mutableClone() and
 * immutableClone() per. WebObjects 5.3 conformance.
 *
 * Revision 1.7 2003/08/06 23:07:52 chochos general code cleanup (mostly,
 * removing unused imports)
 *
 * Revision 1.6 2003/01/16 22:47:30 mpowers Compatibility changes to support
 * compiling woextensions source. (34 out of 56 classes compile!)
 *
 * Revision 1.5 2003/01/10 19:16:40 mpowers Implemented support for page
 * caching.
 *
 * Revision 1.4 2002/10/24 21:15:36 mpowers New implementations of NSArray and
 * subclasses.
 *
 * Revision 1.3 2002/10/24 18:16:30 mpowers Now enforcing NSArray's immutable
 * nature.
 *
 * Revision 1.2 2001/01/11 20:34:26 mpowers Implemented EOSortOrdering and added
 * support in framework. Added header-click to sort table columns.
 *
 * Revision 1.1.1.1 2000/12/21 15:47:31 mpowers Contributing wotonomy.
 *
 * Revision 1.3 2000/12/20 16:25:38 michael Added log to all files.
 *
 *
 */