KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > CmsIdentifiableObjectContainer


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/util/CmsIdentifiableObjectContainer.java,v $
3  * Date : $Date: 2006/11/20 09:23:07 $
4  * Version: $Revision: 1.10 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.util;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * Default implementation of a named object container. <p>
43  *
44  * It can handle relative or absolute orderings and unique names.<p>
45  *
46  * @author Michael Moossen
47  *
48  * @version $Revision: 1.10 $
49  *
50  * @since 6.0.0
51  */

52 public class CmsIdentifiableObjectContainer implements I_CmsIdentifiableObjectContainer {
53
54     // TODO: This class should really be in a workplace package!
55
int todo = 0;
56     
57     /**
58      * Internal class just for taking care of the positions in the container.<p>
59      *
60      * @author Michael Moossen
61      *
62      * @version $Revision: 1.10 $
63      *
64      * @since 6.0.0
65      */

66     private class CmsIdObjectElement {
67
68         /** Identifiable object. */
69         private final Object JavaDoc m_object;
70
71         /** Relative position. */
72         private final float m_position;
73
74         /**
75          * Default Constructor.<p>
76          *
77          * @param object the object
78          * @param position the relative position
79          *
80          */

81         public CmsIdObjectElement(Object JavaDoc object, float position) {
82
83             m_object = object;
84             m_position = position;
85         }
86
87         /**
88          * Returns the object.<p>
89          *
90          * @return the object
91          */

92         public Object JavaDoc getObject() {
93
94             return m_object;
95         }
96
97         /**
98          * Returns the position.<p>
99          *
100          * @return the position
101          */

102         public float getPosition() {
103
104             return m_position;
105         }
106
107     }
108
109     /** Cache for element list. */
110     private List JavaDoc m_cache;
111
112     /** List of objects. */
113     private final List JavaDoc m_objectList = new ArrayList JavaDoc();
114
115     /** Map of objects only used if uniqueIds flag set. */
116     private final Map JavaDoc m_objectsById = new HashMap JavaDoc();
117
118     /** Flag for managing absolute and relative ordering. */
119     private final boolean m_relativeOrdered;
120
121     /** Flag for managing uniqueness check. */
122     private final boolean m_uniqueIds;
123
124     /**
125      * Default Constructor.<p>
126      *
127      * @param uniqueIds if the list show check for unique ids
128      * @param relativeOrdered if the list show use relative ordering, instead of absolute ordering
129      */

130     public CmsIdentifiableObjectContainer(boolean uniqueIds, boolean relativeOrdered) {
131
132         m_uniqueIds = uniqueIds;
133         m_relativeOrdered = relativeOrdered;
134     }
135
136     /**
137      * @see org.opencms.util.I_CmsIdentifiableObjectContainer#addIdentifiableObject(java.lang.String, java.lang.Object)
138      */

139     public void addIdentifiableObject(String JavaDoc id, Object JavaDoc idObject) {
140
141         m_cache = null;
142         if (m_uniqueIds && m_objectsById.get(id) != null) {
143             removeObject(id);
144         }
145         if (m_relativeOrdered) {
146             float pos = 1;
147             if (!m_objectList.isEmpty()) {
148                 pos = ((CmsIdObjectElement)m_objectList.get(m_objectList.size() - 1)).getPosition() + 1;
149             }
150             m_objectList.add(new CmsIdObjectElement(idObject, pos));
151         } else {
152             m_objectList.add(idObject);
153         }
154         if (m_uniqueIds) {
155             m_objectsById.put(id, idObject);
156         } else {
157             Object JavaDoc prevObj = m_objectsById.get(id);
158             if (prevObj == null) {
159                 List JavaDoc list = new ArrayList JavaDoc();
160                 list.add(idObject);
161                 m_objectsById.put(id, list);
162             } else {
163                 ((List JavaDoc)prevObj).add(idObject);
164             }
165         }
166
167     }
168
169     /**
170      * @see org.opencms.util.I_CmsIdentifiableObjectContainer#addIdentifiableObject(java.lang.String, java.lang.Object, float)
171      */

172     public void addIdentifiableObject(String JavaDoc id, Object JavaDoc idObject, float position) {
173
174         m_cache = null;
175         if (m_uniqueIds && m_objectsById.get(id) != null) {
176             removeObject(id);
177         }
178         if (m_relativeOrdered) {
179             int pos = 0;
180             Iterator JavaDoc itElems = m_objectList.iterator();
181             while (itElems.hasNext()) {
182                 CmsIdObjectElement element = (CmsIdObjectElement)itElems.next();
183                 if (element.getPosition() > position) {
184                     break;
185                 }
186                 pos++;
187             }
188             m_objectList.add(pos, new CmsIdObjectElement(idObject, position));
189         } else {
190             m_objectList.add((int)position, idObject);
191         }
192         if (m_uniqueIds) {
193             m_objectsById.put(id, idObject);
194         } else {
195             Object JavaDoc prevObj = m_objectsById.get(id);
196             if (prevObj == null) {
197                 List JavaDoc list = new ArrayList JavaDoc();
198                 list.add(idObject);
199                 m_objectsById.put(id, list);
200             } else {
201                 ((List JavaDoc)prevObj).add(idObject);
202             }
203         }
204
205     }
206
207     /**
208      * @see org.opencms.util.I_CmsIdentifiableObjectContainer#clear()
209      */

210     public void clear() {
211
212         m_cache = null;
213         m_objectList.clear();
214         m_objectsById.clear();
215     }
216
217     /**
218      * @see org.opencms.util.I_CmsIdentifiableObjectContainer#elementList()
219      */

220     public List JavaDoc elementList() {
221
222         if (m_cache != null) {
223             return m_cache;
224         }
225         if (m_relativeOrdered) {
226             List JavaDoc objectList = new ArrayList JavaDoc();
227             Iterator JavaDoc itObjs = m_objectList.iterator();
228             while (itObjs.hasNext()) {
229                 CmsIdObjectElement object = (CmsIdObjectElement)itObjs.next();
230                 objectList.add(object.getObject());
231             }
232             m_cache = Collections.unmodifiableList(objectList);
233         } else {
234             m_cache = Collections.unmodifiableList(m_objectList);
235         }
236         return m_cache;
237     }
238
239     /**
240      * Returns the object with the given id.<p>
241      *
242      * If <code>uniqueIds</code> is set to <code>false</code> an <code>{@link Object}</code>
243      * containing a <code>{@link List}</code> with all the objects with the given id is returned.<p>
244      *
245      * If the container no contains any object with the given id, <code>null</code> is returned.<p>
246      *
247      * @see org.opencms.util.I_CmsIdentifiableObjectContainer#getObject(String)
248      */

249     public Object JavaDoc getObject(String JavaDoc id) {
250
251         return m_objectsById.get(id);
252     }
253
254     /**
255      * Removes an object with the given id.<p>
256      *
257      * if {@link #m_uniqueIds} is set, it will remove at most one object.
258      * otherwise it will remove all elements with the given id.<p>
259      *
260      * @param id the id of the object to remove
261      */

262     public synchronized void removeObject(String JavaDoc id) {
263
264         m_cache = null;
265         if (m_relativeOrdered) {
266             if (m_uniqueIds) {
267                 Object JavaDoc o = getObject(id);
268                 Iterator JavaDoc itObjs = m_objectList.iterator();
269                 while (itObjs.hasNext()) {
270                     CmsIdObjectElement object = (CmsIdObjectElement)itObjs.next();
271                     if (object.getObject() == o) {
272                         itObjs.remove();
273                         break;
274                     }
275                 }
276                 m_objectsById.remove(id);
277             } else {
278                 Iterator JavaDoc itRemove = ((List JavaDoc)getObject(id)).iterator();
279                 while (itRemove.hasNext()) {
280                     Object JavaDoc o = itRemove.next();
281                     Iterator JavaDoc itObjs = m_objectList.iterator();
282                     while (itObjs.hasNext()) {
283                         CmsIdObjectElement object = (CmsIdObjectElement)itObjs.next();
284                         if (object.getObject() == o) {
285                             itObjs.remove();
286                             break;
287                         }
288                     }
289                 }
290                 m_objectsById.remove(id);
291             }
292         } else {
293             Object JavaDoc o = getObject(id);
294             m_objectList.remove(o);
295             m_objectsById.remove(id);
296         }
297     }
298 }
Popular Tags