001: // Copyright 2006, 2007 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:
015: package org.apache.tapestry.ioc.internal.util;
016:
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.HashSet;
022: import java.util.LinkedList;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026: import java.util.concurrent.ConcurrentHashMap;
027: import java.util.concurrent.ConcurrentMap;
028: import java.util.concurrent.CopyOnWriteArrayList;
029:
030: import org.apache.tapestry.ioc.util.CaseInsensitiveMap;
031: import org.apache.tapestry.ioc.util.Stack;
032:
033: /**
034: * Static factory methods to ease the creation of new collection types (when using generics). Most
035: * of these method leverage the compiler's ability to match generic types by return value. Typical
036: * usage (with a static import):
037: *
038: * <pre>
039: * Map<Foo, Bar> map = newMap();
040: * </pre>
041: *
042: * <p>
043: * This is a replacement for:
044: *
045: * <pre>
046: * Map<Foo, Bar> map = new HashMap<Foo, Bar>();
047: * </pre>
048: */
049: public final class CollectionFactory {
050: private CollectionFactory() {
051: }
052:
053: /** Constructs and returns a generic {@link HashMap} instance. */
054: public static <K, V> Map<K, V> newMap() {
055: return new HashMap<K, V>();
056: }
057:
058: /** Constructs and returns a generic {@link java.util.HashSet} instance. */
059: public static <T> Set<T> newSet() {
060: return new HashSet<T>();
061: }
062:
063: /** Contructs a new {@link HashSet} and initializes it using the provided collection. */
064: public static <T, V extends T> Set<T> newSet(Collection<V> values) {
065: return new HashSet<T>(values);
066: }
067:
068: public static <T, V extends T> Set<T> newSet(V... values) {
069: // Was a call to newSet(), but Sun JDK can't handle that. Fucking generics.
070: return new HashSet<T>(Arrays.asList(values));
071: }
072:
073: /**
074: * Constructs a new {@link java.util.HashMap} instance by copying an existing Map instance.
075: */
076: public static <K, V> Map<K, V> newMap(
077: Map<? extends K, ? extends V> map) {
078: return new HashMap<K, V>(map);
079: }
080:
081: /**
082: * Constructs a new concurrent map, which is safe to access via multiple threads.
083: */
084: public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {
085: return new ConcurrentHashMap<K, V>();
086: }
087:
088: /** Contructs and returns a new generic {@link java.util.ArrayList} instance. */
089: public static <T> List<T> newList() {
090: return new ArrayList<T>();
091: }
092:
093: /**
094: * Creates a new, fully modifiable list from an initial set of elements.
095: */
096: public static <T, V extends T> List<T> newList(V... elements) {
097: // Was call to newList(), but Sun JDK can't handle that.
098: return new ArrayList<T>(Arrays.asList(elements));
099: }
100:
101: /** Useful for queues. */
102: public static <T> LinkedList<T> newLinkedList() {
103: return new LinkedList<T>();
104: }
105:
106: /** Constructs and returns a new {@link ArrayList} as a copy of the provided collection. */
107: public static <T, V extends T> List<T> newList(Collection<V> list) {
108: return new ArrayList<T>(list);
109: }
110:
111: /** Constructs and returns a new {@link java.util.concurrent.CopyOnWriteArrayList}. */
112: public static <T> List<T> newThreadSafeList() {
113: return new CopyOnWriteArrayList<T>();
114: }
115:
116: public static <T> Stack<T> newStack() {
117: return new Stack<T>();
118: }
119:
120: public static <V> Map<String, V> newCaseInsensitiveMap() {
121: return new CaseInsensitiveMap<V>();
122: }
123:
124: public static <V> Map<String, V> newCaseInsensitiveMap(
125: Map<String, ? extends V> map) {
126: return new CaseInsensitiveMap<V>(map);
127: }
128: }
|