001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/tools/CmsIdentifiableObjectContainer.java,v $
003: * Date : $Date: 2008-02-27 12:05:31 $
004: * Version: $Revision: 1.4 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.tools;
033:
034: import java.util.ArrayList;
035: import java.util.Collections;
036: import java.util.HashMap;
037: import java.util.Iterator;
038: import java.util.List;
039: import java.util.Map;
040:
041: /**
042: * Default implementation of a named object container. <p>
043: *
044: * It can handle relative or absolute orderings and unique names.<p>
045: *
046: * @author Michael Moossen
047: *
048: * @version $Revision: 1.4 $
049: *
050: * @since 6.0.0
051: */
052: public class CmsIdentifiableObjectContainer implements
053: I_CmsIdentifiableObjectContainer {
054:
055: /**
056: * Internal class just for taking care of the positions in the container.<p>
057: *
058: * @author Michael Moossen
059: *
060: * @version $Revision: 1.4 $
061: *
062: * @since 6.0.0
063: */
064: private static class CmsIdObjectElement {
065:
066: /** Identifiable object. */
067: private final Object m_object;
068:
069: /** Relative position. */
070: private final float m_position;
071:
072: /**
073: * Default Constructor.<p>
074: *
075: * @param object the object
076: * @param position the relative position
077: *
078: */
079: public CmsIdObjectElement(Object object, float position) {
080:
081: m_object = object;
082: m_position = position;
083: }
084:
085: /**
086: * Returns the object.<p>
087: *
088: * @return the object
089: */
090: public Object getObject() {
091:
092: return m_object;
093: }
094:
095: /**
096: * Returns the position.<p>
097: *
098: * @return the position
099: */
100: public float getPosition() {
101:
102: return m_position;
103: }
104:
105: }
106:
107: /** Cache for element list. */
108: private List m_cache;
109:
110: /** List of objects. */
111: private final List m_objectList = new ArrayList();
112:
113: /** Map of objects only used if uniqueIds flag set. */
114: private final Map m_objectsById = new HashMap();
115:
116: /** Flag for managing absolute and relative ordering. */
117: private final boolean m_relativeOrdered;
118:
119: /** Flag for managing uniqueness check. */
120: private final boolean m_uniqueIds;
121:
122: /**
123: * Default Constructor.<p>
124: *
125: * @param uniqueIds if the list show check for unique ids
126: * @param relativeOrdered if the list show use relative ordering, instead of absolute ordering
127: */
128: public CmsIdentifiableObjectContainer(boolean uniqueIds,
129: boolean relativeOrdered) {
130:
131: m_uniqueIds = uniqueIds;
132: m_relativeOrdered = relativeOrdered;
133: }
134:
135: /**
136: * @see org.opencms.workplace.tools.I_CmsIdentifiableObjectContainer#addIdentifiableObject(java.lang.String, java.lang.Object)
137: */
138: public void addIdentifiableObject(String id, Object idObject) {
139:
140: m_cache = null;
141: if (m_uniqueIds && (m_objectsById.get(id) != null)) {
142: removeObject(id);
143: }
144: if (m_relativeOrdered) {
145: float pos = 1;
146: if (!m_objectList.isEmpty()) {
147: pos = ((CmsIdObjectElement) m_objectList
148: .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 prevObj = m_objectsById.get(id);
158: if (prevObj == null) {
159: List list = new ArrayList();
160: list.add(idObject);
161: m_objectsById.put(id, list);
162: } else {
163: ((List) prevObj).add(idObject);
164: }
165: }
166:
167: }
168:
169: /**
170: * @see org.opencms.workplace.tools.I_CmsIdentifiableObjectContainer#addIdentifiableObject(java.lang.String, java.lang.Object, float)
171: */
172: public void addIdentifiableObject(String id, Object idObject,
173: float position) {
174:
175: m_cache = null;
176: if (m_uniqueIds && (m_objectsById.get(id) != null)) {
177: removeObject(id);
178: }
179: if (m_relativeOrdered) {
180: int pos = 0;
181: Iterator itElems = m_objectList.iterator();
182: while (itElems.hasNext()) {
183: CmsIdObjectElement element = (CmsIdObjectElement) itElems
184: .next();
185: if (element.getPosition() > position) {
186: break;
187: }
188: pos++;
189: }
190: m_objectList.add(pos, new CmsIdObjectElement(idObject,
191: position));
192: } else {
193: m_objectList.add((int) position, idObject);
194: }
195: if (m_uniqueIds) {
196: m_objectsById.put(id, idObject);
197: } else {
198: Object prevObj = m_objectsById.get(id);
199: if (prevObj == null) {
200: List list = new ArrayList();
201: list.add(idObject);
202: m_objectsById.put(id, list);
203: } else {
204: ((List) prevObj).add(idObject);
205: }
206: }
207:
208: }
209:
210: /**
211: * @see org.opencms.workplace.tools.I_CmsIdentifiableObjectContainer#clear()
212: */
213: public void clear() {
214:
215: m_cache = null;
216: m_objectList.clear();
217: m_objectsById.clear();
218: }
219:
220: /**
221: * @see org.opencms.workplace.tools.I_CmsIdentifiableObjectContainer#elementList()
222: */
223: public List elementList() {
224:
225: if (m_cache != null) {
226: return m_cache;
227: }
228: if (m_relativeOrdered) {
229: List objectList = new ArrayList();
230: Iterator itObjs = m_objectList.iterator();
231: while (itObjs.hasNext()) {
232: CmsIdObjectElement object = (CmsIdObjectElement) itObjs
233: .next();
234: objectList.add(object.getObject());
235: }
236: m_cache = Collections.unmodifiableList(objectList);
237: } else {
238: m_cache = Collections.unmodifiableList(m_objectList);
239: }
240: return m_cache;
241: }
242:
243: /**
244: * Returns the object with the given id.<p>
245: *
246: * If <code>uniqueIds</code> is set to <code>false</code> an <code>{@link Object}</code>
247: * containing a <code>{@link List}</code> with all the objects with the given id is returned.<p>
248: *
249: * If the container no contains any object with the given id, <code>null</code> is returned.<p>
250: *
251: * @see org.opencms.workplace.tools.I_CmsIdentifiableObjectContainer#getObject(String)
252: */
253: public Object getObject(String id) {
254:
255: return m_objectsById.get(id);
256: }
257:
258: /**
259: * Removes an object with the given id.<p>
260: *
261: * if <code>m_uniqueIds</code> is set, it will remove at most one object.
262: * otherwise it will remove all elements with the given id.<p>
263: *
264: * @param id the id of the object to remove
265: */
266: public synchronized void removeObject(String id) {
267:
268: m_cache = null;
269: if (m_relativeOrdered) {
270: if (m_uniqueIds) {
271: Object o = getObject(id);
272: Iterator itObjs = m_objectList.iterator();
273: while (itObjs.hasNext()) {
274: CmsIdObjectElement object = (CmsIdObjectElement) itObjs
275: .next();
276: if (object.getObject() == o) {
277: itObjs.remove();
278: break;
279: }
280: }
281: m_objectsById.remove(id);
282: } else {
283: Iterator itRemove = ((List) getObject(id)).iterator();
284: while (itRemove.hasNext()) {
285: Object o = itRemove.next();
286: Iterator itObjs = m_objectList.iterator();
287: while (itObjs.hasNext()) {
288: CmsIdObjectElement object = (CmsIdObjectElement) itObjs
289: .next();
290: if (object.getObject() == o) {
291: itObjs.remove();
292: break;
293: }
294: }
295: }
296: m_objectsById.remove(id);
297: }
298: } else {
299: Object o = getObject(id);
300: m_objectList.remove(o);
301: m_objectsById.remove(id);
302: }
303: }
304: }
|