1 31 32 package org.opencms.util; 33 34 import java.util.ArrayList ; 35 import java.util.Collections ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.Map ; 40 41 52 public class CmsIdentifiableObjectContainer implements I_CmsIdentifiableObjectContainer { 53 54 int todo = 0; 56 57 66 private class CmsIdObjectElement { 67 68 69 private final Object m_object; 70 71 72 private final float m_position; 73 74 81 public CmsIdObjectElement(Object object, float position) { 82 83 m_object = object; 84 m_position = position; 85 } 86 87 92 public Object getObject() { 93 94 return m_object; 95 } 96 97 102 public float getPosition() { 103 104 return m_position; 105 } 106 107 } 108 109 110 private List m_cache; 111 112 113 private final List m_objectList = new ArrayList (); 114 115 116 private final Map m_objectsById = new HashMap (); 117 118 119 private final boolean m_relativeOrdered; 120 121 122 private final boolean m_uniqueIds; 123 124 130 public CmsIdentifiableObjectContainer(boolean uniqueIds, boolean relativeOrdered) { 131 132 m_uniqueIds = uniqueIds; 133 m_relativeOrdered = relativeOrdered; 134 } 135 136 139 public void addIdentifiableObject(String id, Object 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 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 172 public void addIdentifiableObject(String id, Object 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 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 prevObj = m_objectsById.get(id); 196 if (prevObj == null) { 197 List list = new ArrayList (); 198 list.add(idObject); 199 m_objectsById.put(id, list); 200 } else { 201 ((List )prevObj).add(idObject); 202 } 203 } 204 205 } 206 207 210 public void clear() { 211 212 m_cache = null; 213 m_objectList.clear(); 214 m_objectsById.clear(); 215 } 216 217 220 public List elementList() { 221 222 if (m_cache != null) { 223 return m_cache; 224 } 225 if (m_relativeOrdered) { 226 List objectList = new ArrayList (); 227 Iterator 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 249 public Object getObject(String id) { 250 251 return m_objectsById.get(id); 252 } 253 254 262 public synchronized void removeObject(String id) { 263 264 m_cache = null; 265 if (m_relativeOrdered) { 266 if (m_uniqueIds) { 267 Object o = getObject(id); 268 Iterator 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 itRemove = ((List )getObject(id)).iterator(); 279 while (itRemove.hasNext()) { 280 Object o = itRemove.next(); 281 Iterator 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 o = getObject(id); 294 m_objectList.remove(o); 295 m_objectsById.remove(id); 296 } 297 } 298 } | Popular Tags |