View Javadoc

1   package net.sourceforge.argval.collection;
2   
3   
4   import java.io.File;
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   
9   public class CollectionUtilConfigFactory {
10      public final static String CONFIG_FILE_SEPARATOR = "file-separator";
11      public final static String CONFIG_PROPERTY_KEY_SEPARATOR = "key-separator";
12      public final static String CONFIG_LIST = "list";
13      public final static String CONFIG_TUPLE = "tuple";
14      public final static String CONFIG_DICTIONARY = "dictionary";
15      public final static String CONFIG_TEXT_UNORDERED_LIST = "text-unordered-list";
16      
17      private Map<String, CollectionUtilConfig> configMap = new HashMap<String, CollectionUtilConfig>();
18      
19  
20      public CollectionUtilConfigFactory() {
21          String arrayPostfix = "]";
22          String arrayPrefix = "[";
23          String arrayValueSeparator = ",";
24          String arrayValuePostfix = "";
25          String arrayValuePrefix = "";
26          String arrayKeyPrefix = null;
27          String arrayKeyPostfix = null;
28          String keyValueSeparator = null;
29          String nullValue = "";
30          boolean isExtraSpacesActive = true;
31          setConfig(CONFIG_LIST, new CollectionUtilConfig(arrayPrefix, arrayPostfix, arrayValueSeparator, arrayValuePrefix, 
32                  arrayValuePostfix, arrayKeyPrefix, arrayKeyPostfix, keyValueSeparator, nullValue, 
33                  isExtraSpacesActive));
34          arrayPostfix = ")";
35          arrayPrefix = "(";
36          arrayValueSeparator = ",";
37          arrayValuePostfix = "";
38          arrayValuePrefix = "";
39          arrayKeyPrefix = null;
40          arrayKeyPostfix = null;
41          keyValueSeparator = null;
42          nullValue = "";
43          isExtraSpacesActive = true;
44          setConfig(CONFIG_TUPLE, new CollectionUtilConfig(arrayPrefix, arrayPostfix, arrayValueSeparator, arrayValuePrefix, 
45                  arrayValuePostfix, arrayKeyPrefix, arrayKeyPostfix, keyValueSeparator, nullValue, 
46                  isExtraSpacesActive));
47          
48          arrayPostfix = "}";
49          arrayPrefix = "{";
50          arrayValueSeparator = ",";
51          arrayValuePostfix = "'";
52          arrayValuePrefix = "'";
53          arrayKeyPrefix = "";
54          arrayKeyPostfix = "";
55          keyValueSeparator = ":";
56          nullValue = "";
57          isExtraSpacesActive = true;
58          setConfig(CONFIG_DICTIONARY, new CollectionUtilConfig(arrayPrefix, arrayPostfix, arrayValueSeparator, arrayValuePrefix, 
59                  arrayValuePostfix, arrayKeyPrefix, arrayKeyPostfix, keyValueSeparator, nullValue, 
60                  isExtraSpacesActive));
61          
62          arrayPrefix = "";
63          arrayPostfix = "";
64          arrayValueSeparator = System.getProperty("line.separator");//StringUtil.LINE_SEPARATOR;
65          arrayValuePrefix = "- ";
66          arrayValuePostfix = "";
67          arrayKeyPrefix = "";
68          arrayKeyPostfix = "";
69          keyValueSeparator = "";
70          nullValue = "";
71          isExtraSpacesActive = false;
72          setConfig(CONFIG_TEXT_UNORDERED_LIST, new CollectionUtilConfig(arrayPrefix, arrayPostfix, arrayValueSeparator, arrayValuePrefix, 
73                  arrayValuePostfix, arrayKeyPrefix, arrayKeyPostfix, keyValueSeparator, nullValue, 
74                  isExtraSpacesActive));
75          
76          arrayPostfix = "";
77          arrayPrefix = "";
78          arrayValueSeparator = ".";
79          arrayValuePostfix = "";
80          arrayValuePrefix = "";
81          arrayKeyPrefix = null;
82          arrayKeyPostfix = null;
83          keyValueSeparator = null;
84          nullValue = "";
85          isExtraSpacesActive = false;
86          setConfig(CONFIG_PROPERTY_KEY_SEPARATOR, new CollectionUtilConfig(arrayPrefix, arrayPostfix, arrayValueSeparator, arrayValuePrefix, 
87                  arrayValuePostfix, arrayKeyPrefix, arrayKeyPostfix, keyValueSeparator, nullValue, 
88                  isExtraSpacesActive));
89          
90          arrayPostfix = "";
91          arrayPrefix = "";
92          arrayValueSeparator = File.separator;
93          arrayValuePostfix = "";
94          arrayValuePrefix = "";
95          arrayKeyPrefix = null;
96          arrayKeyPostfix = null;
97          keyValueSeparator = null;
98          nullValue = "";
99          isExtraSpacesActive = false;
100         setConfig(CONFIG_FILE_SEPARATOR, new CollectionUtilConfig(arrayPrefix, arrayPostfix, arrayValueSeparator, arrayValuePrefix, 
101                 arrayValuePostfix, arrayKeyPrefix, arrayKeyPostfix, keyValueSeparator, nullValue, 
102                 isExtraSpacesActive));
103         
104         create(CONFIG_PROPERTY_KEY_SEPARATOR, ".");
105     }
106 
107 
108     /**
109      * Creates and registers a configuration, which only makes use of a value separator.
110      * This can be used to concatinate directory and file structures, or class names 
111      * (including their packages names).
112      * 
113      * When having a List of directory names like <code>[ "com", "company", "project"]</code>
114      * and a configuration with "/" as separator this can concatinated to "com/company/project".
115      * Or "com.company.project", when "." us used as separator.
116      * 
117      * @param  name  The name under which the configuration is registered.
118      * @param  valueSeparator  The value separator to use.
119      * @return  The configuration.
120      */
121     public CollectionUtilConfig create(String name, String valueSeparator) {
122         // TODO Argument Validation?
123         if (name == null || valueSeparator == null) {
124             throw new IllegalArgumentException("The parameters 'name' and 'valueSeparator' should not be null.");
125         }
126         if (configMap.keySet().contains(name)) {
127             CollectionUtilConfig config = getConfig(name);
128             if (! config.getArrayValueSeparator().equals(valueSeparator)) {
129                 throw new IllegalArgumentException();
130             }
131             else {
132                 return config;
133             }
134         }
135         setConfig(name, create(valueSeparator));
136         return getConfig(name);
137     }
138     
139     
140     public static CollectionUtilConfig create(String valueSeparator) {
141         String arrayPrefix = "";
142         String arrayPostfix = "";
143         String arrayValueSeparator = valueSeparator;
144         String arrayValuePrefix = ""; 
145         String arrayValuePostfix = "";
146         String arrayKeyPrefix = CollectionUtilConfig.DEFAULT_ARRAY_KEY_PREFIX;
147         String arrayKeyPostfix = CollectionUtilConfig.DEFAULT_ARRAY_KEY_POSTFIX;
148         String keyValueSeparator = CollectionUtilConfig.DEFAULT_KEY_VALUE_SEPARATOR;
149         String nullValue = "null"; 
150         boolean isExtraSpacesActive = false;
151         
152         CollectionUtilConfig config = new CollectionUtilConfig(
153                 arrayPrefix, arrayPostfix, arrayValueSeparator, arrayValuePrefix, 
154                 arrayValuePostfix, arrayKeyPrefix, arrayKeyPostfix, keyValueSeparator, nullValue, 
155                 isExtraSpacesActive);
156         return config;
157     }
158     
159     public void setConfig(String name, CollectionUtilConfig config) {
160         configMap.put(name, config);
161     }
162     
163     public CollectionUtilConfig getConfig(String name) {
164         return (CollectionUtilConfig)configMap.get(name);
165     }
166 }