KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > MapFactory


1 package org.picocontainer.defaults;
2
3 import java.util.Map JavaDoc;
4
5 /**
6  * A simple factory for ordered maps: use JDK1.4's java.util.LinkedHashMap if available,
7  * or commons-collection's LinkedMap, or defaults to unordered java.util.HashMap
8  *
9  * @author Gregory Joseph
10  * @version $Revision: $
11  */

12 public class MapFactory {
13     private static final String JavaDoc JDK14 = "java.util.LinkedHashMap";
14     private static final String JavaDoc COMMONS = "org.apache.commons.collections.map.LinkedMap";
15     private static final String JavaDoc NON_ORDERED = "java.util.HashMap";
16
17     private Class JavaDoc clazz;
18
19     public MapFactory() {
20         try {
21             clazz = Class.forName(JDK14);
22         } catch (ClassNotFoundException JavaDoc e) {
23             try {
24                 clazz = Class.forName(COMMONS);
25             } catch (ClassNotFoundException JavaDoc e1) {
26                 try {
27                     clazz = Class.forName(NON_ORDERED);
28                 } catch (ClassNotFoundException JavaDoc e2) {
29                     throw new IllegalStateException JavaDoc("What kind of JRE is this ? No " + NON_ORDERED + " class was found.");
30                 }
31             }
32         }
33     }
34
35     public Map JavaDoc newInstance() {
36         try {
37             return (Map JavaDoc) clazz.newInstance();
38         } catch (InstantiationException JavaDoc e) {
39             throw new RuntimeException JavaDoc("Could not instantiate " + clazz + " : " + e.getMessage());
40         } catch (IllegalAccessException JavaDoc e) {
41             throw new RuntimeException JavaDoc("Could not instantiate " + clazz + " : " + e.getMessage());
42         }
43     }
44 }
45
Popular Tags