KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > smartValueObject > container > AbstractSmartContainer


1 package org.bsf.smartValueObject.container;
2
3 import org.bsf.smartValueObject.Versionable;
4 import org.bsf.smartValueObject.VersionableFilters;
5
6 import java.util.Iterator JavaDoc;
7
8 /**
9  * Skeleton implementation of a versionable container. All other
10  * container should extend this to avoid code duplication.
11  *
12  * @see org.bsf.smartValueObject.container.SmartContainer
13  */

14 public abstract class AbstractSmartContainer implements SmartContainer, Versionable {
15     /** The version instance for this container. */
16     private Versionable version;
17     /** Number of created objects. */
18     private int created = 0;
19     /** Number of deleted objects. */
20     private int deleted = 0;
21
22     /**
23      * Initialize the container with a version.
24      * @param v the version object to use.
25      */

26     public AbstractSmartContainer(Versionable v) {
27         this.version = v;
28     }
29
30     /**
31      * Add object to container.
32      * @param o the object to be added.
33      * @return
34      */

35     protected abstract boolean addToContainer(Object JavaDoc o);
36
37     /**
38      * Adds object to container with key.
39      * @param key the key to use.
40      * @param o the object to be added.
41      * @return
42      * @throws java.lang.UnsupportedOperationException if the method is not supported.
43      */

44     protected abstract Object JavaDoc addToContainer(Object JavaDoc key, Object JavaDoc o);
45
46     /**
47      * Gets object from container.
48      * @param key the key of the object.
49      * @return
50      * @throws java.lang.UnsupportedOperationException if the method is not supported.
51      */

52     protected abstract Object JavaDoc getFromContainer(Object JavaDoc key);
53
54     /**
55      * Removes object from container.
56      * @param o the object to be removed.
57      * @return
58      */

59     protected abstract boolean removeFromContainer(Object JavaDoc o);
60
61     /**
62      * Removes key from container.
63      * @param key the key to be removed.
64      * @return
65      * @throws java.lang.UnsupportedOperationException if the method is not supported.
66      */

67     protected abstract Object JavaDoc removeKeyFromContainer(Object JavaDoc key);
68
69     /**
70      * Verifies if the container has the specified object.
71      * @param o
72      * @return
73      */

74     protected abstract boolean containerContains(Object JavaDoc o);
75
76     /**
77      * Verifies if the container has the specified key.
78      * @param key
79      * @return
80      */

81     protected abstract boolean containerContainsKey(Object JavaDoc key);
82
83     /**
84      * The 'raw' size of the container.
85      * @return size of the container.
86      */

87     protected abstract int containerSize();
88
89     /**
90      * Gets the standard iterator for this container.
91      * @return iterator.
92      */

93     protected abstract Iterator JavaDoc containerIterator();
94
95     /**
96      * Deletes all elements from the container.
97      */

98     protected abstract void containerClear();
99
100     /**
101      * Retrieves objects as an array.
102      * @return
103      * @throws java.lang.UnsupportedOperationException if the method is not supported.
104      */

105     protected abstract Object JavaDoc[] toObjectArray();
106
107     /**
108      * Removes object while respecting versioning.
109      * @param o the object to be removed.
110      * @return
111      */

112     protected boolean removeObject(Object JavaDoc o) {
113         if (o instanceof Versionable) {
114             Versionable v = (Versionable) o;
115             if (v.isCreated()) {
116                 created--;
117                 return removeFromContainer(o);
118             }
119             v.delete();
120             deleted++;
121             touch();
122             return true;
123         } else {
124            return removeFromContainer(o);
125         }
126     }
127
128     /**
129      * Removes object by key, w/ versioning.
130      * @param key
131      * @return
132      */

133     protected Object JavaDoc removeObjectByKey(Object JavaDoc key) {
134         Object JavaDoc o = getFromContainer(key);
135         if (o == null) {
136             // nop
137
} else if (o instanceof Versionable) {
138             Versionable v = (Versionable) o;
139             if (v.isCreated()) {
140                 created--;
141                 return removeKeyFromContainer(key);
142             }
143             v.delete();
144             deleted++;
145             touch();
146         } else {
147             return removeKeyFromContainer(key);
148         }
149         return o;
150     }
151
152     /**
153      * Adds object w/ versioning.
154      * @param o
155      * @return
156      */

157     protected boolean addObject(Object JavaDoc o) {
158         if (o instanceof Versionable) {
159             Versionable v = (Versionable) o;
160
161             // v.create();
162
if (v.isCreated())
163                 created++;
164
165             touch();
166         }
167         return addToContainer(o);
168     }
169
170     /**
171      * Add object via key, w/ versioning.
172      * @param key
173      * @param o
174      * @return
175      */

176     protected Object JavaDoc addObject(Object JavaDoc key, Object JavaDoc o) {
177         if (o instanceof Versionable) {
178             Versionable v = (Versionable) o;
179             //v.create();
180
if (v.isCreated())
181                 created++;
182
183             touch();
184         }
185         return addToContainer(key, o);
186     }
187
188     /**
189      * Checks if container has specified object, respects
190      * versioning.
191      * @param o
192      * @return
193      */

194     protected boolean containsObject(Object JavaDoc o) {
195         if (containerContains(o)) {
196             if (o instanceof Versionable) {
197                 return !((Versionable) o).isDeleted();
198             } else {
199                 return true;
200             }
201         } else {
202             return false;
203         }
204     }
205
206     /**
207      * Implementation for Collection interface.
208      * @param o
209      * @return
210      * @see java.util.Collection#add
211      */

212     public boolean add(Object JavaDoc o) {
213         return addObject(o);
214     }
215
216     /**
217      * Implementation for Collection interface.
218      * @param o
219      * @return
220      * @see java.util.Collection#contains
221      */

222     public boolean contains(Object JavaDoc o) {
223         return containsObject(o);
224     }
225
226     /**
227      * Implementation for Map interface.
228      * @param o
229      * @return
230      * @see java.util.Map#containsValue
231      */

232     public boolean containsValue(Object JavaDoc o) {
233         return containsObject(o);
234     }
235
236     /**
237      * Implementation for Map interface.
238      * @param key
239      * @return
240      * @see java.util.Map#containsValue
241      */

242     public boolean containsKey(Object JavaDoc key) {
243         if (!containerContainsKey(key)) {
244             return false;
245         }
246
247         Object JavaDoc o = getFromContainer(key);
248         if (o instanceof Versionable) {
249             return !((Versionable) o).isDeleted();
250         } else {
251             return true;
252         }
253     }
254
255     /**
256      * Implementation for Collection interface.
257      * @return
258      * @see java.util.Collection#iterator
259      */

260     public Iterator JavaDoc iterator() {
261         return new SmartIterator(containerIterator(),
262                 VersionableFilters.EXISTING);
263     }
264
265     /**
266      * Implementation for Collection/Map/... interface.
267      *
268      * @see java.util.Collection#clear
269      * @see java.util.Map#clear
270      */

271     public void clear() {
272         Iterator JavaDoc it = containerIterator();
273         while (it.hasNext()) {
274             removeObject(it.next());
275         }
276     }
277
278     /**
279      * Implementaion for Collection/Map/... interface.
280      * @return
281      * @see java.util.Collection#isEmpty
282      * @see java.util.Map#isEmpty
283      */

284     public boolean isEmpty() {
285         return (containerSize()-getDeleted() == 0);
286     }
287
288     /**
289      * Implementation for Map interface.
290      * @param key
291      * @param value
292      * @return
293      * @see java.util.Map#put
294      */

295     public Object JavaDoc put (Object JavaDoc key, Object JavaDoc value) {
296         return addObject(key, value);
297     }
298
299     /**
300      * Implementation for Map interface.
301      * @param key
302      * @return
303      * @see java.util.Map#get
304      */

305     public Object JavaDoc get(Object JavaDoc key) {
306         Object JavaDoc o = getFromContainer(key);
307         if (o == null)
308             return null;
309
310         if (o instanceof Versionable) {
311             return ((Versionable) o).isDeleted() ? null : o;
312         } else {
313             return o;
314         }
315     }
316
317     /**
318      * Implementation for Collection/... interface.
319      * @return
320      * @see java.util.Collection#toArray
321      */

322     public Object JavaDoc[] toArray() {
323         Object JavaDoc[] a = toObjectArray();
324         Object JavaDoc[] b = new Object JavaDoc[size()];
325
326         for (int i = 0,j = 0; i < a.length; i++) {
327             Object JavaDoc o = a[i];
328             if (o instanceof Versionable) {
329                 if (((Versionable) o).isDeleted())
330                     continue;
331             }
332             b[j++] = a[i];
333         }
334         return b;
335     }
336
337     /**
338      * Implementation for Collection/... interface.
339      * @return
340      * @see java.util.Collection#toArray
341      */

342     public Object JavaDoc[] toArray(Object JavaDoc a[]) {
343         int size = size();
344         if (a.length < size) {
345             a = (Object JavaDoc[]) java.lang.reflect.Array.newInstance(
346                     a.getClass().getComponentType(), size);
347         }
348
349         System.arraycopy(toArray(), 0, a, 0, size);
350         if (a.length > size) {
351             a[size] = null;
352         }
353         return a;
354     }
355
356
357     //- SmartContainer
358
public int getCreated() {
359         return created;
360     }
361
362     public int getDeleted() {
363         return deleted;
364     }
365
366     public Iterator JavaDoc getIterator() {
367         return containerIterator();
368     }
369
370     public int size() {
371         return containerSize()-getDeleted();
372     }
373
374     public abstract Object JavaDoc getContainer();
375
376     //- Versionable
377
public void touch() {
378         version.touch();
379     }
380
381     public void touch(String JavaDoc s) {
382         version.touch(s);
383     }
384
385     public boolean isDirty() {
386         return version.isDirty();
387     }
388
389     public boolean isCreated() {
390         return version.isCreated();
391     }
392
393     public boolean isDeleted() {
394         return version.isDeleted();
395     }
396
397     public void markClean() {
398         created = deleted = 0;
399         version.markClean();
400     }
401
402     public long getVersionId() {
403         return version.getVersionId();
404     }
405
406     public void setVersionId(long id) {
407         version.setVersionId(id);
408     }
409
410     public void delete() {}
411     public void create() {}
412 }
413
Popular Tags