summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.datastore/src/main/java/net/wotonomy/datastore/DefaultDataView.java
blob: 1864cacdf5460d6240033a91482946aa5d8bd290 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Michael Powers

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.datastore;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Observable;

public class DefaultDataView extends Observable implements DataView {
	protected DataSoup backingSoup;
	protected List objectList;
	protected List keyList;
	protected List addedObjectList;
	protected List removedObjectList;
	protected List addedKeyList;
	protected List removedKeyList;
	protected Collection updatedObjects;
	protected boolean fullyLoaded;

	DefaultDataView(DataSoup aSoup, Collection aKeyList) {
		backingSoup = aSoup;
		objectList = new ArrayList();
		keyList = new ArrayList();
		addedObjectList = new ArrayList();
		removedObjectList = new ArrayList();
		addedKeyList = new ArrayList();
		removedKeyList = new ArrayList();
		updatedObjects = new LinkedList();
		fullyLoaded = false;

		setKeyList(aKeyList);
	}

	void setKeyList(Collection aCollection) {
		fullyLoaded = false;
		addedObjectList.clear();
		removedObjectList.clear();
		addedKeyList.clear();
		removedKeyList.clear();
		updatedObjects.clear();
		keyList.clear();
		objectList.clear();
		if ((aCollection == null) || (aCollection.size() == 0)) {
			return;
		}
		keyList.addAll(aCollection);
		for (int i = 0; i < keyList.size(); i++) {
			objectList.add(null);
		}
	}

	public Object get(int i) {
		if (i > keyList.size())
			return null;

		Object o = objectList.get(i);
		if (o == null) {
			Object key = keyList.get(i);
			if (key == null)
				return null; // FIXME!!

			// NOTE: this is the gateway for getting object from the soup
			o = backingSoup.getObjectByKey((DataKey) key);

			objectList.set(i, o);
		}
		return o;
	}

	public int indexOf(Object o) {
		if (o == null)
			return -1;
		for (int i = 0; i < size(); i++) {
			if (o.equals(get(i))) {
				return i;
			}
		}
		return -1;
	}

	private int indexOfIdenticalObject(Object o) {
		if (o == null)
			return -1;
		for (int i = 0; i < size(); i++) {
			if (o == get(i)) {
				return i;
			}
		}
		return -1;
	}

	public int lastIndexOf(Object o) {
		if (o == null)
			return -1;
		int lastIndex = -1;
		for (int i = 0; i < size(); i++) {
			if (o.equals(get(i))) {
				lastIndex = i;
			}
		}
		return lastIndex;
	}

	protected void loadAllObjects() {
		if (fullyLoaded)
			return;
		for (int i = 0; i < keyList.size(); i++) {
			get(i);
		}
		fullyLoaded = true;
	}

	// convenience to return the first object, or null.
	public Object getObject() {
		return get(0);
	}

	// marked object as updated
	public void update(Object anObject) {
		if (contains(anObject)) {
			if (!addedObjectList.contains(anObject)) {
				updatedObjects.add(anObject);
			}
		}

		// notification
		setChanged();
		notifyObservers(anObject);
	}

	// DefaultDataViews know their parent soup to perform the query
	// and take the subset
	public DataView query(String aProperty, Object beginKey, Object endKey) {
		return this;
	}

	public boolean commit() {
		int index;
		Object o;
		DataKey key;
		for (int i = 0; i < addedObjectList.size(); i++) {
			o = addedObjectList.get(i);
			key = backingSoup.addObject(o);
			index = indexOfIdenticalObject(o);
			keyList.set(index, key);
		}
		addedObjectList.clear();
		addedKeyList.clear();
		for (int i = 0; i < removedObjectList.size(); i++) {
			backingSoup.removeObject((DataKey) removedKeyList.get(i));
		}
		removedObjectList.clear();
		removedKeyList.clear();

		int i;
		Iterator it = updatedObjects.iterator();
		while (it.hasNext()) {
			i = objectList.indexOf(it.next());
			backingSoup.updateObject((DataKey) keyList.get(i), objectList.get(i));
		}
		updatedObjects.clear();

		// notification
		setChanged();
		notifyObservers(this);

		return true;
	}

	public DataKey getKeyForObject(Object anObject) {
		int index = indexOfIdenticalObject(anObject);
		if (index == -1)
			return null;
		return (DataKey) keyList.get(index);
	}

	public Object getObjectForKey(DataKey aKey) {
		int index = keyList.indexOf(aKey);
		if (index == -1)
			return null;
		return get(index);
	}

	// interface Collection

	public int size() {
		return keyList.size();
	}

	public boolean isEmpty() {
		return keyList.isEmpty();
	}

	public void clear() {
		setKeyList(null);
	};

	public int hashCode() {
		return keyList.hashCode();
	};

	public boolean equals(Object o) {
		if (!(o instanceof DefaultDataView))
			return false;
		return keyList.equals(((DefaultDataView) o).keyList);
	}

	public boolean contains(Object o) {
		loadAllObjects();
		return objectList.contains(o);
	}

	public boolean containsAll(Collection c) {
		loadAllObjects();
		return objectList.containsAll(c);
	}

	public boolean add(Object o) {
		// if previously removed, restore to list
		if (removedObjectList.contains(o)) {
			int index = removedObjectList.indexOf(o);
			removedObjectList.remove(index);
			Object key = removedKeyList.remove(index);
			objectList.add(o);
			keyList.add(key);

			// notification
			setChanged();
			notifyObservers(o);
			return true;
		}

		// register and add to lists
		Object key = backingSoup.registerTemporaryObject(o);
		addedObjectList.add(o);
		addedKeyList.add(key);
		objectList.add(o);
		keyList.add(key);

		// notification
		setChanged();
		notifyObservers(o);
		return true;
	}

	public Object remove(int index) {
		Object result = get(index);
		if (remove(result)) {
			return result;
		}
		return null;
	}

	public boolean remove(Object o) {
		loadAllObjects();

		int index = objectList.indexOf(o);
		if (index == -1)
			return false;

		objectList.remove(index);
		Object key = keyList.remove(index);

		if (updatedObjects.contains(o)) {
			updatedObjects.remove(o);
		}

		// if not previously added, track removal
		if (!(removedObjectList.contains(o))) {
			removedObjectList.add(o);
			removedKeyList.add(key);
		}

		// notification
		setChanged();
		notifyObservers(o);

		return true;
	}

	/**
	 * Set completely replaces the object at the specified index with the specified
	 * object. The new object is not marked as inserted, and the old object is not
	 * marked as deleted: the new object will be stored in the soup with the same
	 * key. The old object is returned.
	 */
	public Object set(int index, Object element) {
		Object result = objectList.set(index, element);
		update(element);
		return result;
	}

	public void add(int index, Object o) {
		// if previously removed, restore to list
		if (removedObjectList.contains(o)) {
			int i = removedObjectList.indexOf(o);
			removedObjectList.remove(i);
			Object key = removedKeyList.remove(i);
			objectList.add(index, o);
			keyList.add(index, key);

			// notification
			setChanged();
			notifyObservers(o);
			return;
		}

		// register and add to lists
		Object key = backingSoup.registerTemporaryObject(o);
		addedObjectList.add(o);
		addedKeyList.add(key);
		objectList.add(index, o);
		keyList.add(index, key);

		// notification
		setChanged();
		notifyObservers(o);
	}

	public boolean addAll(Collection c) {
		boolean result = true;
		Iterator it = c.iterator();
		while (it.hasNext()) {
			result = result && add(it.next());
		}
		return result;
	}

	public boolean addAll(int index, Collection c) {
		int originalSize = size();
		boolean result = true;
		Iterator it = c.iterator();
		while (it.hasNext()) {
			add(index, it.next());
		}
		return (originalSize + c.size() == size());
	}

	public boolean removeAll(Collection c) {
		boolean result = true;
		Iterator it = c.iterator();
		while (it.hasNext()) {
			result = result && remove(it.next());
		}
		return result;
	}

	public boolean retainAll(Collection c) {
		removeAll(new ArrayList(objectList));
		return addAll(c);
	}

	public List subList(int fromIndex, int toIndex) {
		List result = new LinkedList();
		for (int i = fromIndex; i < toIndex; i++) {
			result.add(get(i));
		}
		return result;
	}

	public Iterator iterator() {
		loadAllObjects();
		return objectList.iterator();

		/*
		 * // uncomment to enable on-demand loading return new Iterator() { int index =
		 * 0; public boolean hasNext() { return ( index + 1 < keyList.size() ); } public
		 * Object next() { return get( index++ ); } public void remove() { Object o =
		 * get( index ); if ( o != null ) DefaultDataView.this.remove( o ); } };
		 */
	}

	public ListIterator listIterator() {
		return new DataViewIterator(this);
	}

	public ListIterator listIterator(int index) {
		return new DataViewIterator(this, index);
	}

	public Object[] toArray() {
		loadAllObjects();
		return objectList.toArray();
	}

	public java.lang.Object[] toArray(Object[] array) {
		loadAllObjects();
		return objectList.toArray(array);
	}

	protected class DataViewIterator implements ListIterator {
		DataView theView;
		int currentIndex;

		// TODO: should track current object in addition to index
		// to track external changes to the view. (or should be listener)
		Object currentObject;

		public DataViewIterator(DataView aView) {
			this(aView, 0);
		}

		public DataViewIterator(DataView aView, int index) {
			theView = aView;
			if (theView.size() > index) {
				currentIndex = index;
				currentObject = theView.get(currentIndex);
			} else {
				index = -1;
				currentObject = null;
			}
		}

		public void add(Object o) {
			currentIndex++;
			theView.add(currentIndex, o);
		}

		public boolean hasNext() {
			return (theView.size() > currentIndex + 1);
		}

		public boolean hasPrevious() {
			return (currentIndex > -1);
		}

		public Object next() {
			return theView.get(++currentIndex);
		}

		public int nextIndex() {
			return currentIndex + 1;
		}

		public Object previous() {
			return theView.get(currentIndex--);
		}

		public int previousIndex() {
			return currentIndex;
		}

		public void remove() {
			theView.remove(currentIndex--);
		}

		public void set(Object o) {
			theView.set(currentIndex, o);
		}

	}
}

/*
 * $Log$ Revision 1.2 2006/02/19 16:26:19 cgruber Move non-unit-test code to
 * tests project Fix up code to work with proper imports Fix maven dependencies.
 *
 * Revision 1.1 2006/02/16 13:18:56 cgruber Check in all sources in
 * eclipse-friendly maven-enabled packages.
 *
 * Revision 1.2 2001/02/15 21:12:41 mpowers Added accessors for key throughout
 * the api. This breaks compatibility. insertObject now returns the permanent
 * key for the newly created object. The old way returned a copy of the object
 * which was an additional read that was often ignored. Now you can read it only
 * if you need it. Furthermore, there was not other way of getting the permanent
 * key.
 *
 * Revision 1.1.1.1 2000/12/21 15:47:14 mpowers Contributing wotonomy.
 *
 * Revision 1.2 2000/12/20 16:25:36 michael Added log to all files.
 *
 *
 */