1 /* 2 Copyright 2009 Ramon Servadei 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package fulmine.util.collection; 17 18 import java.util.Collection; 19 import java.util.List; 20 import java.util.Map; 21 import java.util.Queue; 22 import java.util.Set; 23 24 /** 25 * A factory for constructing {@link java.util.Collection} instances. A 26 * {@link ICollectionBuilder} instance is used to construct the collection 27 * instances. The implementation of the builder is specified by setting the 28 * class name of the builder to use against the system property name 29 * {@link ICollectionBuilder}. 30 * 31 * @author Ramon Servadei 32 */ 33 public final class CollectionFactory 34 { 35 /** the builder instance to use */ 36 private static final ICollectionBuilder builder; 37 static 38 { 39 final String className = 40 System.getProperty(ICollectionBuilder.class.getSimpleName(), 41 CollectionBuilder.class.getCanonicalName()); 42 ICollectionBuilder instance = null; 43 try 44 { 45 instance = 46 (ICollectionBuilder) Class.forName(className).newInstance(); 47 } 48 catch (Exception e) 49 { 50 e.printStackTrace(); 51 // this is terminal 52 System.exit(5); 53 } 54 builder = instance; 55 } 56 57 /** 58 * Construct a {@link Set} implementation with the given size. 59 * 60 * @param <T> 61 * the type for the set elements 62 * @param size 63 * the size 64 * @return a {@link Set} implementation 65 */ 66 public static <T> Set<T> newSet(int size) 67 { 68 return builder.newSet(size); 69 } 70 71 /** 72 * Construct a {@link Set} implementation from another {@link Collection} 73 * instance. This copies the contents of the other collection into the newly 74 * constructed implementation. 75 * 76 * @param <T> 77 * the type for the set elements 78 * @param other 79 * the other {@link Collection} instance to construct the 80 * {@link Set} from 81 * @return a {@link Set} implementation constructed from a 82 * {@link Collection} instance. 83 */ 84 public static <T> Set<T> newSet(Collection<T> other) 85 { 86 return builder.newSet(other); 87 } 88 89 /** 90 * Construct a {@link Set} implementation with the implementation default 91 * size. 92 * 93 * @param <T> 94 * the type for the set elements 95 * @return a {@link Set} implementation 96 */ 97 public static <T> Set<T> newSet() 98 { 99 return builder.newSet(); 100 } 101 102 /** 103 * Construct a {@link Queue} implementation with the implementation default 104 * size. 105 * 106 * @param <T> 107 * the type for the queue elements 108 * @return a {@link Queue} implementation 109 */ 110 public static <T> Queue<T> newQueue() 111 { 112 return builder.newQueue(); 113 } 114 115 /** 116 * Construct a {@link Queue} implementation with the given size. 117 * 118 * @param <T> 119 * the type for the queue elements 120 * @param size 121 * the size 122 * @return a {@link Queue} implementation 123 */ 124 public static <T> Queue<T> newQueue(int size) 125 { 126 return builder.newQueue(size); 127 } 128 129 /** 130 * Construct a {@link List} implementation with its implementation default 131 * size. 132 * 133 * @param <T> 134 * the type for the list elements 135 * @return a {@link List} implementation 136 */ 137 public static <T> List<T> newList() 138 { 139 return builder.newList(); 140 } 141 142 /** 143 * Construct a {@link List} implementation from another {@link Collection} 144 * instance size. 145 * 146 * @param <T> 147 * the type for the list elements 148 * @param other 149 * the other {@link Collection} to construct this list from 150 * @return a {@link List} implementation constructed with the elements of 151 * the other {@link Collection} 152 */ 153 public static <T> List<T> newList(Collection<T> other) 154 { 155 return builder.newList(other); 156 } 157 158 /** 159 * Construct a {@link List} implementation with the given size. 160 * 161 * @param <T> 162 * the type for the list elements 163 * @param size 164 * the size 165 * @return a {@link List} implementation 166 */ 167 public static <T> List<T> newList(int size) 168 { 169 return builder.newList(size); 170 } 171 172 /** 173 * Construct a {@link Map} implementation with the given size. 174 * 175 * @param <K> 176 * the type for the map keys 177 * @param <V> 178 * the type for the map values 179 * @param size 180 * the size 181 * @return a {@link Map} implementation 182 */ 183 public static <K, V> Map<K, V> newMap(int size) 184 { 185 return builder.newMap(size); 186 } 187 188 /** 189 * Construct a {@link Map} implementation with the given size. The 190 * implementation maintains insertion order and does not have any generic 191 * type information. 192 * 193 * @param size 194 * the size 195 * @return a {@link Map} implementation 196 */ 197 @SuppressWarnings("unchecked") 198 public static Map newUntypedOrderedMap(int size) 199 { 200 return builder.newUntypedOrderedMap(size); 201 } 202 203 /** 204 * Construct a {@link Map} implementation with the given size. This returns 205 * a map with no generic type information. 206 * 207 * @param size 208 * the size 209 * @return a {@link Map} implementation 210 */ 211 @SuppressWarnings("unchecked") 212 public static Map newUntypedMap(int size) 213 { 214 return builder.newUntypedMap(size); 215 } 216 217 /** 218 * Construct a {@link Map} implementation with the given size and 219 * loadFactor. 220 * 221 * @param <K> 222 * the type for the map keys 223 * @param <V> 224 * the type for the map values 225 * @param size 226 * the size 227 * @param loadFactor 228 * the loadFactor 229 * @return a {@link Map} implementation 230 */ 231 public static <K, V> Map<K, V> newMap(int size, float loadFactor) 232 { 233 return builder.newMap(size, loadFactor); 234 } 235 236 /** 237 * Construct a {@link Map} implementation from another instance. This copies 238 * the contents of the other map into the newly constructed implementation. 239 * 240 * @param <K> 241 * the type for the map keys 242 * @param <V> 243 * the type for the map values 244 * @param other 245 * the other instance to construct this map from 246 * @return a {@link Map} implementation constructed from the other instance. 247 */ 248 public static <K, V> Map<K, V> newMap(Map<K, V> other) 249 { 250 return builder.newMap(other); 251 } 252 253 /** 254 * Construct a {@link Map} implementation with the implementation default 255 * size. 256 * 257 * @param <K> 258 * the type for the map keys 259 * @param <V> 260 * the type for the map values 261 * @return a {@link Map} implementation 262 */ 263 public static <K, V> Map<K, V> newMap() 264 { 265 return builder.newMap(); 266 } 267 268 }