01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.internal;
13:
14: import net.sf.oval.collection.CollectionFactory;
15: import net.sf.oval.collection.CollectionFactoryJDKImpl;
16: import net.sf.oval.collection.CollectionFactoryJavalutionImpl;
17: import net.sf.oval.collection.CollectionFactoryTroveImpl;
18: import net.sf.oval.internal.util.ReflectionUtils;
19:
20: /**
21: * The held factory is used by OVal to instantiate new collections.
22: *
23: * @author Sebastian Thomschke
24: */
25: public final class CollectionFactoryHolder {
26: private final static Log LOG = Log
27: .getLog(CollectionFactoryHolder.class);
28:
29: private static CollectionFactory factory = createDefaultCollectionFactory();
30:
31: private static CollectionFactory createDefaultCollectionFactory() {
32: // if javalution collection classes are found use them by default
33: if (ReflectionUtils.isClassPresent("javolution.util.FastMap")
34: && ReflectionUtils
35: .isClassPresent("javolution.util.FastSet")
36: && ReflectionUtils
37: .isClassPresent("javolution.util.FastTable")) {
38: LOG
39: .info("javolution.util collection classes are available.");
40:
41: return new CollectionFactoryJavalutionImpl();
42: }
43: // else if trove collection classes are found use them by default
44: else if (ReflectionUtils.isClassPresent("gnu.trove.THashMap")
45: && ReflectionUtils.isClassPresent("gnu.trove.THashSet")) {
46: LOG.info("gnu.trove collection classes are available.");
47:
48: return new CollectionFactoryTroveImpl();
49: }
50: // else use JDK collection classes by default
51: else
52: return new CollectionFactoryJDKImpl();
53: }
54:
55: /**
56: * Returns a shared instance of the CollectionFactory
57: */
58: public static CollectionFactory getFactory() {
59: return factory;
60: }
61:
62: /**
63: *
64: * @param factory the new collection factory to use
65: */
66: public static void setFactory(final CollectionFactory factory)
67: throws IllegalArgumentException {
68: if (factory == null)
69: throw new IllegalArgumentException("factory cannot be null");
70:
71: CollectionFactoryHolder.factory = factory;
72: }
73: }
|