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    public static <K, V> Map<K, V> createLazyMap(Transformer T) {
064
065        return LazyMap.decorate(new HashMap<K, V>(), T);
066    }
067
068    /**
069     * Provides a wrapper to create a {@link LRUMap} that avoids warnings with Java 1.5 generic code.<p>
070     *
071     * @param <K> the type of keys maintained by the returned map
072     * @param <V> the type of mapped values
073     *
074     * @return a {@link LRUMap} of the required generic type
075     */
076    @SuppressWarnings("unchecked")
077    public static <K, V> Map<K, V> createLRUMap() {
078
079        return new LRUMap();
080    }
081
082    /**
083     * Provides a wrapper to create a {@link LRUMap} with the given size that avoids warnings with Java 1.5 generic code.<p>
084     *
085     * @param <K> the type of keys maintained by the returned map
086     * @param <V> the type of mapped values
087     * @param size the initial size of the created Map
088     *
089     * @return a {@link LRUMap} with the given size of the required generic type
090     */
091    @SuppressWarnings("unchecked")
092    public static <K, V> Map<K, V> createLRUMap(int size) {
093
094        return new LRUMap(size);
095    }
096
097    /**
098     * Provides a wrapper to convert an enumeration that avoids warnings with Java 1.5 generic code.<p>
099     *
100     * @param <K> the type of the returned enumeration elements
101     * @param enumeration the enumeration to be converted
102     *
103     * @return a {@link Enumeration} with the required generic type
104     */
105    @SuppressWarnings("unchecked")
106    public static <K> Enumeration<K> enumeration(Enumeration<?> enumeration) {
107
108        return (Enumeration<K>)enumeration;
109    }
110
111    /**
112     * Provides a wrapper to convert an object into a list that avoids warnings with Java 1.5 generic code.<p>
113     *
114     * @param <K> the type of the returned list elements
115     * @param o the object to be converted
116     *
117     * @return a {@link List} with the required generic type
118     */
119    @SuppressWarnings("unchecked")
120    public static <K> List<K> list(Object o) {
121
122        return (List<K>)o;
123    }
124
125    /**
126     * Provides a wrapper to convert an object into a map that avoids warnings with Java 1.5 generic code.<p>
127     *
128     * @param <K> the type of keys maintained by the returned map
129     * @param <V> the type of mapped values
130     * @param o the object to be converted
131     *
132     * @return a {@link Map} of the required generic type
133     */
134    @SuppressWarnings("unchecked")
135    public static <K, V> Map<K, V> map(Object o) {
136
137        return (Map<K, V>)o;
138    }
139}