001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (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 java.util.Enumeration;
031import java.util.HashMap;
032import java.util.List;
033import java.util.Map;
034
035import org.apache.commons.collections.Transformer;
036import org.apache.commons.collections.map.LRUMap;
037import org.apache.commons.collections.map.LazyMap;
038
039/**
040 * Provides Map wrapping utility functions for Java generics.<p>
041 * 
042 * @since 8.0.0 
043 */
044public final class CmsCollectionsGenericWrapper {
045
046    /**
047     * Hides the public constructor.<p> 
048     */
049    private CmsCollectionsGenericWrapper() {
050
051        // empty
052    }
053
054    /**
055     * Provides a wrapper to access the {@link LazyMap} functionality that avoids warnings with Java 1.5 generic code.<p> 
056     * 
057     * @param <K> the type of keys maintained by the returned map
058     * @param <V> the type of mapped values
059     * @param T the transformer to use for the Lazy Map
060     * 
061     * @return a {@link LazyMap} of the required generic type
062     */
063    @SuppressWarnings("unchecked")
064    public static <K, V> Map<K, V> createLazyMap(Transformer T) {
065
066        return LazyMap.decorate(new HashMap<K, V>(), T);
067    }
068
069    /**
070     * Provides a wrapper to create a {@link LRUMap} that avoids warnings with Java 1.5 generic code.<p> 
071     * 
072     * @param <K> the type of keys maintained by the returned map
073     * @param <V> the type of mapped values
074     * 
075     * @return a {@link LRUMap} of the required generic type
076     */
077    @SuppressWarnings("unchecked")
078    public static <K, V> Map<K, V> createLRUMap() {
079
080        return new LRUMap();
081    }
082
083    /**
084     * Provides a wrapper to create a {@link LRUMap} with the given size that avoids warnings with Java 1.5 generic code.<p> 
085     * 
086     * @param <K> the type of keys maintained by the returned map
087     * @param <V> the type of mapped values
088     * @param size the initial size of the created Map
089     * 
090     * @return a {@link LRUMap} with the given size of the required generic type
091     */
092    @SuppressWarnings("unchecked")
093    public static <K, V> Map<K, V> createLRUMap(int size) {
094
095        return new LRUMap(size);
096    }
097
098    /**
099     * Provides a wrapper to convert an enumeration that avoids warnings with Java 1.5 generic code.<p> 
100     * 
101     * @param <K> the type of the returned enumeration elements
102     * @param enumeration the enumeration to be converted
103     * 
104     * @return a {@link Enumeration} with the required generic type
105     */
106    @SuppressWarnings("unchecked")
107    public static <K> Enumeration<K> enumeration(Enumeration<?> enumeration) {
108
109        return (Enumeration<K>)enumeration;
110    }
111
112    /**
113     * Provides a wrapper to convert an object into a list that avoids warnings with Java 1.5 generic code.<p> 
114     * 
115     * @param <K> the type of the returned list elements
116     * @param o the object to be converted
117     * 
118     * @return a {@link List} with the required generic type
119     */
120    @SuppressWarnings("unchecked")
121    public static <K> List<K> list(Object o) {
122
123        return (List<K>)o;
124    }
125
126    /**
127     * Provides a wrapper to convert an object into a map that avoids warnings with Java 1.5 generic code.<p> 
128     * 
129     * @param <K> the type of keys maintained by the returned map
130     * @param <V> the type of mapped values
131     * @param o the object to be converted
132     * 
133     * @return a {@link Map} of the required generic type
134     */
135    @SuppressWarnings("unchecked")
136    public static <K, V> Map<K, V> map(Object o) {
137
138        return (Map<K, V>)o;
139    }
140}