View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.foundation;
20  
21  import java.util.Collection;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.ListIterator;
25  
26  /***
27  * NSMutableArray extends NSArray to allow modification.
28  *
29  * @author michael@mpowers.net
30  * @author $Author: cgruber $
31  * @version $Revision: 893 $
32  */
33  public class NSMutableArray extends NSArray
34  {
35      /***
36      * Returns an NSArray backed by the specified List.
37      * This is useful to "protect" an internal representation
38      * that is returned by a method of return type NSArray.
39      */
40      public static NSMutableArray mutableArrayBackedByList( List aList )
41      {
42          return new NSMutableArray( aList, null );
43      }
44      
45      NSMutableArray( List aList, Object ignored ) // differentiates
46      {
47          super( aList, ignored );
48      }
49      
50  	/***
51  	* Default constructor returns an empty array.
52  	*/
53      public NSMutableArray ()
54      {
55      	super();
56  //System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() );
57      }	
58  
59  	/***
60  	* Constructor with a size hint.
61  	*/
62      public NSMutableArray ( int aSize )
63      {
64      	super();
65  //System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() );
66      }	
67  
68  	/***
69  	* Produces an array containing only the specified object.
70  	*/
71      public NSMutableArray (Object anObject)
72      {
73      	super( anObject );
74  //System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() );
75      }
76  
77  	/***
78  	* Produces an array containing the specified objects.
79  	*/
80      public NSMutableArray (Object[] anArray)
81      {
82      	super( anArray );
83  //System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() );
84      }
85  
86  	/***
87  	* Produces an array containing the objects in the specified collection.
88  	*/
89      public NSMutableArray (Collection aCollection)
90      {
91      	super( aCollection );
92  //System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() );
93      }
94  
95  	/***
96  	* Removes the last object from the array.
97  	*/
98      public void removeLastObject ()
99      {
100 		list.remove( count() - 1 );    
101     }
102 
103 	/***
104 	* Removes the object at the specified index.
105 	*/
106     public void removeObjectAtIndex (int index)
107     {
108     	list.remove( index );
109     }
110 
111 	/***
112 	* Adds all objects in the specified collection.
113 	*/
114     public void addObjectsFromArray (Collection aCollection)
115     {
116     	list.addAll( aCollection );
117     }
118 
119 	/***
120 	* Removes all objects from the array.
121 	*/
122     public void removeAllObjects ()
123     {
124     	list.clear();
125     }
126 
127 	/***
128 	* Removes all objects equivalent to the specified object
129 	* within the range of specified indices.
130 	*/
131     public void removeObject (Object anObject, NSRange aRange)
132     {
133     	if ( ( anObject == null ) || ( aRange == null ) ) return;
134 	    
135 	    int loc = aRange.location();
136 	    int max = aRange.maxRange();
137 	    for ( int i = loc; i < max; i++ )
138 	    {
139 	    	if ( anObject.equals( list.get( i ) ) )
140 		    {
141 		    	list.remove( i );
142 			    i = i - 1;
143 			    max = max - 1;
144 		    }
145 	    }
146     }
147 
148 	/***
149 	* Removes all instances of the specified object within the 
150 	* range of specified indices, comparing by reference.
151 	*/
152     public void removeIdenticalObject (Object anObject, NSRange aRange)
153     {
154     	if ( ( anObject == null ) || ( aRange == null ) ) return;
155 	    
156 	    int loc = aRange.location();
157 	    int max = aRange.maxRange();
158 	    for ( int i = loc; i < max; i++ )
159 	    {
160 	    	if ( anObject == list.get( i ) )
161 		    {
162 		    	list.remove( i );
163 			    i = i - 1;
164 			    max = max - 1;
165 		    }
166 	    }
167     }
168 
169 	/***
170 	* Removes all objects in the specified collection from the array.
171 	*/
172     public void removeObjectsInArray (Collection aCollection)
173     {
174     	list.removeAll( aCollection );
175     }
176 
177 	/***
178 	* Removes all objects in the indices within the specified range 
179 	* from the array.
180 	*/
181     public void removeObjectsInRange (NSRange aRange)
182     {
183     	if ( aRange == null ) return;
184 	    
185 	    for ( int i = 0; i < aRange.length(); i++ )
186 	    {
187 	    	list.remove( aRange.location() );
188 	    }
189     }
190 
191 	/***
192 	* Replaces objects in the current range with objects from
193 	* the specified range of the specified array.  If currentRange
194 	* is larger than otherRange, the extra objects are removed.
195 	* If otherRange is larger than currentRange, the extra objects
196 	* are added.
197 	*/
198     public void replaceObjectsInRange (NSRange currentRange, 
199     	List otherArray, NSRange otherRange)
200 	{
201 		if ( ( currentRange == null ) || ( otherArray == null ) ||
202 			( otherRange == null ) ) return;
203 	
204 		// transform otherRange if out of bounds for array
205 		if ( otherRange.maxRange() > otherArray.size() )
206 		{
207 			// TODO: Test this logic.
208 			int loc = Math.min( otherRange.location(), otherArray.size() - 1 );
209 			otherRange = new NSRange( loc, otherArray.size() - loc ); 
210 		}
211 		
212 		Object o;
213 		List subList = list.subList( 
214 			currentRange.location(), currentRange.maxRange() );
215 		int otherIndex = otherRange.location();
216 		// TODO: Test this logic.
217 		for ( int i = 0; i < subList.size(); i++ )
218 		{
219 			if ( otherIndex < otherRange.maxRange() )
220 			{ // set object
221 				subList.set( i, otherArray.get( otherIndex ) ); 
222 			}
223 			else
224 			{ // remove extra elements from currentRange
225 				subList.remove( i );
226 				i--; 
227 			}
228 			otherIndex++;
229 		}
230 		// TODO: Test this logic.
231 		for ( int i = otherIndex; i < otherRange.maxRange(); i++ )
232 		{
233 			list.add( otherArray.get( i ) );
234 		}
235 	}
236 
237 	/***
238 	* Clears the current array and then populates it with the
239 	* contents of the specified collection.
240 	*/
241     public void setArray (Collection aCollection)
242     {
243     	list.clear();
244 	    list.addAll( aCollection );
245     }
246 
247 	/***
248 	* Sorts this array using the values from the specified selector.
249 	*/
250     public void sortUsingSelector (NSSelector aSelector)
251     {
252     	//TODO: implement 
253         throw new UnsupportedOperationException( "Not implemented yet." );
254     }
255 
256 	/***
257 	* Removes all objects equivalent to the specified object. 
258 	*/
259     public void removeObject (Object anObject)
260     {
261         list.remove( anObject );
262     }
263 
264 	/***
265 	* Removes all occurences of the specified object,
266 	* comparing by reference.
267 	*/
268     public void removeIdenticalObject (Object anObject)
269     {
270     	Iterator it = list.iterator();
271 	    while ( it.hasNext() )
272 	    {
273 	    	if ( it.next() == anObject )
274 		    {
275 		    	it.remove();
276 		    }
277 	    }	
278     }
279 
280 	/***
281 	* Inserts the specified object into this array at the
282 	* specified index.
283 	*/
284     public void insertObjectAtIndex (Object anObject, int anIndex)
285     {
286     	list.add( anIndex, anObject );
287     }
288     
289 	/***
290 	* Replaces the object at the specified index with the 
291 	* specified object.
292 	*/
293     public void replaceObjectAtIndex (int anIndex, Object anObject)
294     {
295     	list.set( anIndex, anObject );
296     }
297 
298 	/***
299 	* Adds the specified object to the end of this array.
300 	*/
301     public void addObject (Object anObject) 
302     {
303     	list.add( anObject );
304     }
305     
306     public Object clone()
307     {
308         return new NSMutableArray( list );
309     }
310     
311     public NSArray immutableClone() {
312         return new NSArray(this);
313     }
314 	
315     public NSMutableArray mutableClone() {
316         return new NSMutableArray(this);
317     }
318 	
319     // interface List: mutators
320 
321 	public void add(int index, Object element) 
322     {
323         list.add( index, element );
324     }
325     
326 	public boolean add(Object o)
327     {
328         return list.add(o);
329     }
330 
331 	public boolean addAll(Collection coll) 
332     {
333         return list.addAll(coll);
334     }
335 
336 	public boolean addAll(int index, Collection c) 
337     {
338         return list.addAll( index, c );
339     }
340     
341 	public void clear() 
342     {
343         list.clear();
344     }
345 
346 	public Iterator iterator() 
347     {
348         return list.iterator();
349     }
350 
351 	public ListIterator listIterator() 
352     {
353         return list.listIterator();
354     }    
355 
356 	public ListIterator listIterator(int index)
357     {
358         return list.listIterator();
359 	}
360 
361 	public Object remove(int index) 
362     {
363         return list.remove( index );
364     }
365     
366 	public boolean remove(Object o) 
367     {
368         return list.remove(o);
369     }
370 
371 	public boolean removeAll(Collection coll) 
372     {
373         return list.removeAll(coll);
374     }
375 
376 	public boolean retainAll(Collection coll) 
377     {
378         return list.retainAll(coll);
379     }
380 
381     public Object set(int index, Object element) 
382     {
383         return list.set( index, element );
384     }
385     
386     public List subList(int fromIndex, int toIndex) 
387     {
388         return list.subList( fromIndex, toIndex );
389     }
390     
391 }
392 
393 /*
394  * $Log$
395  * Revision 1.2  2006/02/16 13:15:00  cgruber
396  * Check in all sources in eclipse-friendly maven-enabled packages.
397  *
398  * Revision 1.8  2005/07/13 14:12:44  cgruber
399  * Add mutableClone() and immutableClone()  per. WebObjects 5.3 conformance.
400  *
401  * Revision 1.7  2003/08/06 23:07:52  chochos
402  * general code cleanup (mostly, removing unused imports)
403  *
404  * Revision 1.6  2003/01/16 22:47:30  mpowers
405  * Compatibility changes to support compiling woextensions source.
406  * (34 out of 56 classes compile!)
407  *
408  * Revision 1.5  2003/01/10 19:16:40  mpowers
409  * Implemented support for page caching.
410  *
411  * Revision 1.4  2002/10/24 21:15:36  mpowers
412  * New implementations of NSArray and subclasses.
413  *
414  * Revision 1.3  2002/10/24 18:16:30  mpowers
415  * Now enforcing NSArray's immutable nature.
416  *
417  * Revision 1.2  2001/01/11 20:34:26  mpowers
418  * Implemented EOSortOrdering and added support in framework.
419  * Added header-click to sort table columns.
420  *
421  * Revision 1.1.1.1  2000/12/21 15:47:31  mpowers
422  * Contributing wotonomy.
423  *
424  * Revision 1.3  2000/12/20 16:25:38  michael
425  * Added log to all files.
426  *
427  *
428  */
429