001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://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: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://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            m_map = map;
068        }
069
070        /**
071         *
072         * @see java.util.Map#clear()
073         */
074        @Override
075        public void clear() {
076
077            m_map.clear();
078        }
079
080        /**
081         * @see java.util.Map#containsKey(java.lang.Object)
082         */
083        @Override
084        public boolean containsKey(Object key) {
085
086            V value = m_map.get(key);
087            if (value instanceof CmsJspContentAccessValueWrapper) {
088                return ((CmsJspContentAccessValueWrapper)value).getExists();
089            } else {
090                return value != null;
091            }
092        }
093
094        /**
095         * @see java.util.Map#containsValue(java.lang.Object)
096         */
097        @Override
098        public boolean containsValue(Object value) {
099
100            return m_map.containsValue(value);
101        }
102
103        /**
104         * @see java.util.Map#entrySet()
105         */
106        @Override
107        public Set<java.util.Map.Entry<K, V>> entrySet() {
108
109            return m_map.entrySet();
110        }
111
112        /**
113         * @see java.util.Map#equals(java.lang.Object)
114         */
115        @Override
116        public boolean equals(Object o) {
117
118            return m_map.equals(o);
119        }
120
121        /**
122         * @see java.util.Map#get(java.lang.Object)
123         */
124        @Override
125        public V get(Object key) {
126
127            return m_map.get(key);
128        }
129
130        /**
131         * @see java.util.Map#hashCode()
132         */
133        @Override
134        public int hashCode() {
135
136            return m_map.hashCode();
137        }
138
139        /**
140         * @see java.util.Map#isEmpty()
141         */
142        @Override
143        public boolean isEmpty() {
144
145            return m_map.isEmpty();
146        }
147
148        /**
149         * @see java.util.Map#keySet()
150         */
151        @Override
152        public Set<K> keySet() {
153
154            return m_map.keySet();
155        }
156
157        /**
158         * @see java.util.Map#put(java.lang.Object, java.lang.Object)
159         */
160        @Override
161        public V put(K key, V value) {
162
163            return m_map.put(key, value);
164        }
165
166        /**
167         * @see java.util.Map#putAll(java.util.Map)
168         */
169        @Override
170        public void putAll(Map<? extends K, ? extends V> m) {
171
172            m_map.putAll(m);
173        }
174
175        /**
176         * @see java.util.Map#remove(java.lang.Object)
177         */
178        @Override
179        public V remove(Object key) {
180
181            return m_map.remove(key);
182        }
183
184        /**
185         * @see java.util.Map#size()
186         */
187        @Override
188        public int size() {
189
190            return m_map.size();
191        }
192
193        /**
194         * @see java.util.Map#values()
195         */
196        @Override
197        public Collection<V> values() {
198
199            return m_map.values();
200        }
201
202    }
203
204    /**
205     * Hides the public constructor.<p>
206     */
207    private CmsCollectionsGenericWrapper() {
208
209        // empty
210    }
211
212    /**
213     * Provides a wrapper to access the {@link LazyMap} functionality that avoids warnings with Java 1.5 generic code.<p>
214     *
215     * @param <K> the type of keys maintained by the returned map
216     * @param <V> the type of mapped values
217     * @param T the transformer to use for the Lazy Map
218     *
219     * @return a {@link LazyMap} of the required generic type
220     */
221    public static <K, V> Map<K, V> createLazyMap(Transformer T) {
222
223        return new MapWrapper<K, V>(LazyMap.decorate(new HashMap<K, V>(), T));
224    }
225
226    /**
227     * Provides a wrapper to create a {@link LRUMap} that avoids warnings with Java 1.5 generic code.<p>
228     *
229     * @param <K> the type of keys maintained by the returned map
230     * @param <V> the type of mapped values
231     *
232     * @return a {@link LRUMap} of the required generic type
233     */
234    @SuppressWarnings("unchecked")
235    public static <K, V> Map<K, V> createLRUMap() {
236
237        return new LRUMap();
238    }
239
240    /**
241     * Provides a wrapper to create a {@link LRUMap} with the given size that avoids warnings with Java 1.5 generic code.<p>
242     *
243     * @param <K> the type of keys maintained by the returned map
244     * @param <V> the type of mapped values
245     * @param size the initial size of the created Map
246     *
247     * @return a {@link LRUMap} with the given size of the required generic type
248     */
249    @SuppressWarnings("unchecked")
250    public static <K, V> Map<K, V> createLRUMap(int size) {
251
252        return new LRUMap(size);
253    }
254
255    /**
256     * Provides a wrapper to convert an enumeration that avoids warnings with Java 1.5 generic code.<p>
257     *
258     * @param <K> the type of the returned enumeration elements
259     * @param enumeration the enumeration to be converted
260     *
261     * @return a {@link Enumeration} with the required generic type
262     */
263    @SuppressWarnings("unchecked")
264    public static <K> Enumeration<K> enumeration(Enumeration<?> enumeration) {
265
266        return (Enumeration<K>)enumeration;
267    }
268
269    /**
270     * Provides a wrapper to convert an object into a list that avoids warnings with Java 1.5 generic code.<p>
271     *
272     * @param <K> the type of the returned list elements
273     * @param o the object to be converted
274     *
275     * @return a {@link List} with the required generic type
276     */
277    @SuppressWarnings("unchecked")
278    public static <K> List<K> list(Object o) {
279
280        return (List<K>)o;
281    }
282
283    /**
284     * Provides a wrapper to convert an object into a map that avoids warnings with Java 1.5 generic code.<p>
285     *
286     * @param <K> the type of keys maintained by the returned map
287     * @param <V> the type of mapped values
288     * @param o the object to be converted
289     *
290     * @return a {@link Map} of the required generic type
291     */
292    @SuppressWarnings("unchecked")
293    public static <K, V> Map<K, V> map(Object o) {
294
295        return (Map<K, V>)o;
296    }
297}