001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005//     http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.ioc.internal.util;
014
015import org.apache.tapestry5.ioc.util.CaseInsensitiveMap;
016import org.apache.tapestry5.ioc.util.Stack;
017
018import java.util.*;
019import java.util.concurrent.ConcurrentHashMap;
020import java.util.concurrent.ConcurrentMap;
021import java.util.concurrent.CopyOnWriteArrayList;
022
023/**
024 * Static factory methods to ease the creation of new collection types (when using generics). Most of these method
025 * leverage the compiler's ability to match generic types by return value. Typical usage (with a static import):
026 *
027 * <pre>
028 * Map&lt;Foo, Bar&gt; map = newMap();
029 * </pre>
030 *
031 *
032 * This is a replacement for:
033 *
034 * <pre>
035 * Map&lt;Foo, Bar&gt; map = new HashMap&lt;Foo, Bar&gt;();
036 * </pre>
037 */
038public final class CollectionFactory
039{
040    /**
041     * Constructs and returns a generic {@link HashMap} instance.
042     */
043    public static <K, V> Map<K, V> newMap()
044    {
045        return new HashMap<K, V>();
046    }
047
048    /**
049     * Constructs and returns a generic {@link java.util.HashSet} instance.
050     */
051    public static <T> Set<T> newSet()
052    {
053        return new HashSet<T>();
054    }
055
056    /**
057     * Contructs a new {@link HashSet} and initializes it using the provided collection.
058     */
059    public static <T, V extends T> Set<T> newSet(Collection<V> values)
060    {
061        return new HashSet<T>(values);
062    }
063
064    public static <T, V extends T> Set<T> newSet(V... values)
065    {
066        // Was a call to newSet(), but Sun JDK can't handle that. Fucking generics.
067        return new HashSet<T>(Arrays.asList(values));
068    }
069
070    /**
071     * Constructs a new {@link java.util.HashMap} instance by copying an existing Map instance.
072     */
073    public static <K, V> Map<K, V> newMap(Map<? extends K, ? extends V> map)
074    {
075        return new HashMap<K, V>(map);
076    }
077
078    /**
079     * Constructs a new concurrent map, which is safe to access via multiple threads.
080     */
081    public static <K, V> ConcurrentMap<K, V> newConcurrentMap()
082    {
083        return new ConcurrentHashMap<K, V>();
084    }
085
086    /**
087     * Contructs and returns a new generic {@link java.util.ArrayList} instance.
088     */
089    public static <T> List<T> newList()
090    {
091        return new ArrayList<T>();
092    }
093
094    /**
095     * Creates a new, fully modifiable list from an initial set of elements.
096     */
097    public static <T, V extends T> List<T> newList(V... elements)
098    {
099        // Was call to newList(), but Sun JDK can't handle that.
100        return new ArrayList<T>(Arrays.asList(elements));
101    }
102
103    /**
104     * Useful for queues.
105     */
106    public static <T> LinkedList<T> newLinkedList()
107    {
108        return new LinkedList<T>();
109    }
110
111    /**
112     * Constructs and returns a new {@link java.util.ArrayList} as a copy of the provided collection.
113     */
114    public static <T, V extends T> List<T> newList(Collection<V> list)
115    {
116        return new ArrayList<T>(list);
117    }
118
119    /**
120     * Constructs and returns a new {@link java.util.concurrent.CopyOnWriteArrayList}.
121     */
122    public static <T> List<T> newThreadSafeList()
123    {
124        return new CopyOnWriteArrayList<T>();
125    }
126
127    public static <T> Stack<T> newStack()
128    {
129        return new Stack<T>();
130    }
131
132    public static <V> Map<String, V> newCaseInsensitiveMap()
133    {
134        return new CaseInsensitiveMap<V>();
135    }
136
137}