View Javadoc

1   /***
2    * 
3    */
4   /*
5    * Copyright 2006 Outsource Cafe, Inc.
6    *
7    * Licensed under the Apache License, Version 2.0 (the 'License')
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *    http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an 'AS IS' BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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       * Notes: had to implemtent a singlton pattern that would work with IoC. Would have prefered
35       * just using injection but this class is needed by the Reference class which is too fine-grained for injection.
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 }