1 /***
2 *
3 */
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.javagen.agile.oo.naming;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 /***
25 * Holds a set of collection interfaces and implementation class names indexed
26 * by abstract key names.
27 * <p>
28 * The default abstract key names are: SET, SORTED_SET, BAG, LIST, MAP, SORTED_MAP.
29 *
30 * @author Richard Easterling
31 */
32 final public class Collections {
33
34
35
36
37 private String defaultCollectionKey;
38 private Map<String, String> collectionInterfaces;
39 private Map<String, String> collectionImplementations;
40
41 private static Collections singlton;
42
43 public static Collections getInstance() {
44 if (singlton==null)
45 singlton = new Collections();
46 return singlton;
47 }
48
49 public Collections() {
50 defaultCollectionKey = "SET";
51
52 collectionInterfaces = new HashMap<String, String>();
53 collectionInterfaces.put("SET", "java.util.Set");
54 collectionInterfaces.put("SORTED_SET", "java.util.SortedSet");
55 collectionInterfaces.put("BAG", "java.util.Collection");
56 collectionInterfaces.put("LIST", "java.util.List");
57 collectionInterfaces.put("MAP", "java.util.Map");
58 collectionInterfaces.put("SORTED_MAP", "java.util.SortedMap");
59
60 collectionImplementations = new HashMap<String, String>();
61 collectionImplementations.put("SET", "java.util.HashSet");
62 collectionImplementations.put("SORTED_SET", "java.util.TreeSet");
63 collectionImplementations.put("BAG", "java.util.ArrayList");
64 collectionImplementations.put("LIST", "java.util.ArrayList");
65 collectionImplementations.put("MAP", "java.util.HashMap");
66 collectionImplementations.put("SORTED_MAP", "java.util.TreeMap");
67 singlton = this;
68 }
69
70 public String getInterface(String collectionKey) {
71 validateKey(collectionKey, true);
72 return collectionInterfaces==null ? null : collectionInterfaces.get(collectionKey);
73 }
74
75 public String getImplementation(String collectionKey) {
76 return collectionImplementations==null ? null : collectionImplementations.get(collectionKey);
77 }
78
79 public boolean validateKey(String key, boolean throwException) {
80 boolean result = collectionInterfaces.containsKey(key);
81 if (!result && throwException) {
82 throw new IllegalArgumentException(key+" is not a valid collection key: "+collectionInterfaces.toString());
83 }
84 return result;
85 }
86
87 public String getCollectionKey(String _interface) {
88 if (_interface!=null)
89 for(Map.Entry<String, String> entry : collectionInterfaces.entrySet()) {
90 if (_interface.equals(entry.getValue()))
91 return entry.getKey();
92 }
93 return null;
94 }
95
96 public void setCollectionImplementations(Map<String, String> collectionImplementations) {
97 this.collectionImplementations = collectionImplementations;
98 }
99
100 public void setCollectionInterfaces(Map<String, String> collectionInterfaces) {
101 this.collectionInterfaces = collectionInterfaces;
102 }
103
104 public String getDefaultCollectionKey() {
105 return defaultCollectionKey;
106 }
107
108 public void setDefaultCollectionKey(String defaultCollectionKey) {
109 this.defaultCollectionKey = defaultCollectionKey;
110 }
111
112 }