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.tapestry5.ioc.internal.util; 016 017 import org.apache.tapestry5.ioc.util.CaseInsensitiveMap; 018 import org.apache.tapestry5.ioc.util.Stack; 019 020 import java.util.*; 021 import java.util.concurrent.ConcurrentHashMap; 022 import java.util.concurrent.ConcurrentMap; 023 import 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<Foo, Bar> map = newMap(); 031 * </pre> 032 * <p/> 033 * <p/> 034 * This is a replacement for: 035 * <p/> 036 * <pre> 037 * Map<Foo, Bar> map = new HashMap<Foo, Bar>(); 038 * </pre> 039 */ 040 public 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 * @since 5.3 090 */ 091 public static <K, V> Map<K, V> newWeakHashMap() 092 { 093 return new WeakHashMap<K, V>(); 094 } 095 096 /** 097 * Contructs and returns a new generic {@link java.util.ArrayList} instance. 098 */ 099 public static <T> List<T> newList() 100 { 101 return new ArrayList<T>(); 102 } 103 104 /** 105 * Creates a new, fully modifiable list from an initial set of elements. 106 */ 107 public static <T, V extends T> List<T> newList(V... elements) 108 { 109 // Was call to newList(), but Sun JDK can't handle that. 110 return new ArrayList<T>(Arrays.asList(elements)); 111 } 112 113 /** 114 * Useful for queues. 115 */ 116 public static <T> LinkedList<T> newLinkedList() 117 { 118 return new LinkedList<T>(); 119 } 120 121 /** 122 * Constructs and returns a new {@link java.util.ArrayList} as a copy of the provided collection. 123 */ 124 public static <T, V extends T> List<T> newList(Collection<V> list) 125 { 126 return new ArrayList<T>(list); 127 } 128 129 /** 130 * Constructs and returns a new {@link java.util.concurrent.CopyOnWriteArrayList}. 131 */ 132 public static <T> List<T> newThreadSafeList() 133 { 134 return new CopyOnWriteArrayList<T>(); 135 } 136 137 public static <T> Stack<T> newStack() 138 { 139 return new Stack<T>(); 140 } 141 142 public static <T> Stack<T> newStack(int initialSize) 143 { 144 return new Stack<T>(initialSize); 145 } 146 147 public static <V> Map<String, V> newCaseInsensitiveMap() 148 { 149 return new CaseInsensitiveMap<V>(); 150 } 151 152 public static <V> Map<String, V> newCaseInsensitiveMap(Map<String, ? extends V> map) 153 { 154 return new CaseInsensitiveMap<V>(map); 155 } 156 }