KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > implementation > BasicList


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge.implementation;
12 import org.mmbase.bridge.*;
13 import java.util.*;
14 import org.mmbase.util.logging.*;
15
16 /**
17  * A list of objects.
18  * This is the base class for all basic implementations of the bridge lists.
19  *
20  * @author Pierre van Rooden
21  * @version $Id: BasicList.java,v 1.18 2005/12/29 19:23:54 michiel Exp $
22  */

23 public class BasicList extends ArrayList implements BridgeList {
24
25     private static final Logger log = Logging.getLoggerInstance(BasicList.class);
26
27     private Map properties = new HashMap();
28
29     // during inititializion of the list, you sometimes want to switch off
30
// also when everything is certainly converted
31
boolean autoConvert = true;
32
33     BasicList() {
34          super();
35     }
36
37     protected BasicList(Collection c) {
38          super(c);
39     }
40
41     public Object JavaDoc getProperty(Object JavaDoc key) {
42         return properties.get(key);
43     }
44
45     public void setProperty(Object JavaDoc key, Object JavaDoc value) {
46         properties.put(key,value);
47     }
48
49     /*
50      * converts the object in teh list to the excpected format
51      */

52     protected Object JavaDoc convert(Object JavaDoc o, int index) {
53         return o;
54     }
55
56     public boolean contains(Object JavaDoc o ) {
57         // make sure every element is of the right type, ArrayList implementation does _not_ call get.
58
convertAll();
59         return super.contains(o);
60     }
61
62     public boolean remove(Object JavaDoc o) {
63         // make sure every element is of the right type, otherwise 'equals' is very odd..
64
convertAll();
65         return super.remove(o);
66     }
67     public boolean removeAll(Collection c) {
68         // make sure every element is of the right type, otherwise 'equals' is very odd..
69
convertAll();
70         return super.removeAll(c);
71     }
72
73     /*
74      * validates that an object can be converted to the excpected format
75      */

76     protected Object JavaDoc validate(Object JavaDoc o) throws ClassCastException JavaDoc {
77         return o;
78     }
79
80     public Object JavaDoc get(int index) {
81         if (autoConvert) {
82             return convert(super.get(index), index);
83         } else {
84             return super.get(index);
85         }
86     }
87
88     public void sort() {
89         Collections.sort(this);
90     }
91
92     public void sort(Comparator comparator) {
93         Collections.sort(this,comparator);
94     }
95
96     public Object JavaDoc set(int index, Object JavaDoc o) {
97         return super.set(index,validate(o));
98     }
99
100     public void add(int index, Object JavaDoc o) {
101         autoConvert = true;
102         super.add(index,validate(o));
103     }
104
105     public boolean add(Object JavaDoc o) {
106         autoConvert = true;
107         return super.add(validate(o));
108     }
109
110     /**
111      * @since MMBase-1.6.2
112      */

113     protected void convertAll() {
114         log.debug("convert all");
115         for (int i = 0; i < size(); i++) {
116             convert(super.get(i), i);
117         }
118         autoConvert = false;
119     }
120
121
122     public Object JavaDoc[] toArray() { // needed when you e.g. want to sort the list.
123
// make sure every element is of the right type, otherwise sorting can happen on the wrong type.
124
if (autoConvert) convertAll();
125         return super.toArray();
126     }
127
128     protected class BasicIterator implements ListIterator {
129         protected ListIterator iterator;
130
131         protected BasicIterator() {
132             this.iterator = listIterator();
133         }
134
135         public boolean hasNext() {
136             return iterator.hasNext();
137         }
138
139         public boolean hasPrevious() {
140             return iterator.hasPrevious();
141         }
142
143         public int nextIndex() {
144             return iterator.nextIndex();
145         }
146
147         public int previousIndex() {
148             return iterator.previousIndex();
149         }
150
151         public void remove() {
152             iterator.remove();
153         }
154
155         // These have to be implemented with a check if o is of the right type.
156
public void set(Object JavaDoc o) {
157             iterator.set(o);
158         }
159
160         public void add(Object JavaDoc o) {
161             BasicList.this.autoConvert = true;
162             iterator.add(o);
163         }
164
165         // normally also e.g. set(Node n); and add(Node n) will be created in
166
// descendant class, because that is better for performance.
167

168         public Object JavaDoc next() {
169             return iterator.next();
170         }
171
172         public Object JavaDoc previous() {
173             return iterator.previous();
174         }
175
176     }
177
178 }
179
Popular Tags