001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.util; 029 030import org.opencms.jsp.util.CmsJspContentAccessValueWrapper; 031 032import java.util.Collection; 033import java.util.Enumeration; 034import java.util.HashMap; 035import java.util.List; 036import java.util.Map; 037import java.util.Set; 038 039import org.apache.commons.collections.Transformer; 040import org.apache.commons.collections.map.LRUMap; 041import org.apache.commons.collections.map.LazyMap; 042 043/** 044 * Provides Map wrapping utility functions for Java generics.<p> 045 * 046 * @since 8.0.0 047 */ 048public final class CmsCollectionsGenericWrapper { 049 050 /** 051 * Wrapper for lazy maps providing a better containsKey implementation.<p> 052 * 053 * @param <K> the key type 054 * @param <V> the value type 055 */ 056 public static class MapWrapper<K, V> implements Map<K, V> { 057 058 /** The wrapped map. */ 059 private Map<K, V> m_map; 060 061 /** 062 * Constructor.<p> 063 * 064 * @param map the map to wrap 065 */ 066 MapWrapper(Map<K, V> map) { 067 068 m_map = map; 069 } 070 071 /** 072 * 073 * @see java.util.Map#clear() 074 */ 075 @Override 076 public void clear() { 077 078 m_map.clear(); 079 } 080 081 /** 082 * @see java.util.Map#containsKey(java.lang.Object) 083 */ 084 @Override 085 public boolean containsKey(Object key) { 086 087 V value = m_map.get(key); 088 if (value instanceof CmsJspContentAccessValueWrapper) { 089 return ((CmsJspContentAccessValueWrapper)value).getExists(); 090 } else { 091 return value != null; 092 } 093 } 094 095 /** 096 * @see java.util.Map#containsValue(java.lang.Object) 097 */ 098 @Override 099 public boolean containsValue(Object value) { 100 101 return m_map.containsValue(value); 102 } 103 104 /** 105 * @see java.util.Map#entrySet() 106 */ 107 @Override 108 public Set<java.util.Map.Entry<K, V>> entrySet() { 109 110 return m_map.entrySet(); 111 } 112 113 /** 114 * @see java.util.Map#equals(java.lang.Object) 115 */ 116 @Override 117 public boolean equals(Object o) { 118 119 return m_map.equals(o); 120 } 121 122 /** 123 * @see java.util.Map#get(java.lang.Object) 124 */ 125 @Override 126 public V get(Object key) { 127 128 return m_map.get(key); 129 } 130 131 /** 132 * @see java.util.Map#hashCode() 133 */ 134 @Override 135 public int hashCode() { 136 137 return m_map.hashCode(); 138 } 139 140 /** 141 * @see java.util.Map#isEmpty() 142 */ 143 @Override 144 public boolean isEmpty() { 145 146 return m_map.isEmpty(); 147 } 148 149 /** 150 * @see java.util.Map#keySet() 151 */ 152 @Override 153 public Set<K> keySet() { 154 155 return m_map.keySet(); 156 } 157 158 /** 159 * @see java.util.Map#put(java.lang.Object, java.lang.Object) 160 */ 161 @Override 162 public V put(K key, V value) { 163 164 return m_map.put(key, value); 165 } 166 167 /** 168 * @see java.util.Map#putAll(java.util.Map) 169 */ 170 @Override 171 public void putAll(Map<? extends K, ? extends V> m) { 172 173 m_map.putAll(m); 174 } 175 176 /** 177 * @see java.util.Map#remove(java.lang.Object) 178 */ 179 @Override 180 public V remove(Object key) { 181 182 return m_map.remove(key); 183 } 184 185 /** 186 * @see java.util.Map#size() 187 */ 188 @Override 189 public int size() { 190 191 return m_map.size(); 192 } 193 194 /** 195 * @see java.util.Map#values() 196 */ 197 @Override 198 public Collection<V> values() { 199 200 return m_map.values(); 201 } 202 203 } 204 205 /** 206 * Hides the public constructor.<p> 207 */ 208 private CmsCollectionsGenericWrapper() { 209 210 // empty 211 } 212 213 /** 214 * Provides a wrapper to access the {@link LazyMap} functionality that avoids warnings with Java 1.5 generic code.<p> 215 * 216 * @param <K> the type of keys maintained by the returned map 217 * @param <V> the type of mapped values 218 * @param T the transformer to use for the Lazy Map 219 * 220 * @return a {@link LazyMap} of the required generic type 221 */ 222 public static <K, V> Map<K, V> createLazyMap(Transformer T) { 223 224 return new MapWrapper<K, V>(LazyMap.decorate(new HashMap<K, V>(), T)); 225 } 226 227 /** 228 * Provides a wrapper to create a {@link LRUMap} that avoids warnings with Java 1.5 generic code.<p> 229 * 230 * @param <K> the type of keys maintained by the returned map 231 * @param <V> the type of mapped values 232 * 233 * @return a {@link LRUMap} of the required generic type 234 */ 235 @SuppressWarnings("unchecked") 236 public static <K, V> Map<K, V> createLRUMap() { 237 238 return new LRUMap(); 239 } 240 241 /** 242 * Provides a wrapper to create a {@link LRUMap} with the given size that avoids warnings with Java 1.5 generic code.<p> 243 * 244 * @param <K> the type of keys maintained by the returned map 245 * @param <V> the type of mapped values 246 * @param size the initial size of the created Map 247 * 248 * @return a {@link LRUMap} with the given size of the required generic type 249 */ 250 @SuppressWarnings("unchecked") 251 public static <K, V> Map<K, V> createLRUMap(int size) { 252 253 return new LRUMap(size); 254 } 255 256 /** 257 * Provides a wrapper to convert an enumeration that avoids warnings with Java 1.5 generic code.<p> 258 * 259 * @param <K> the type of the returned enumeration elements 260 * @param enumeration the enumeration to be converted 261 * 262 * @return a {@link Enumeration} with the required generic type 263 */ 264 @SuppressWarnings("unchecked") 265 public static <K> Enumeration<K> enumeration(Enumeration<?> enumeration) { 266 267 return (Enumeration<K>)enumeration; 268 } 269 270 /** 271 * Provides a wrapper to convert an object into a list that avoids warnings with Java 1.5 generic code.<p> 272 * 273 * @param <K> the type of the returned list elements 274 * @param o the object to be converted 275 * 276 * @return a {@link List} with the required generic type 277 */ 278 @SuppressWarnings("unchecked") 279 public static <K> List<K> list(Object o) { 280 281 return (List<K>)o; 282 } 283 284 /** 285 * Provides a wrapper to convert an object into a map that avoids warnings with Java 1.5 generic code.<p> 286 * 287 * @param <K> the type of keys maintained by the returned map 288 * @param <V> the type of mapped values 289 * @param o the object to be converted 290 * 291 * @return a {@link Map} of the required generic type 292 */ 293 @SuppressWarnings("unchecked") 294 public static <K, V> Map<K, V> map(Object o) { 295 296 return (Map<K, V>)o; 297 } 298}