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