KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > logging > Options


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.logging;
5
6 import com.tc.logging.TCLogger;
7
8 import java.util.Arrays JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14
15 /**
16  * Generic comma separated options parser
17  */

18 public class Options {
19   public static final String JavaDoc ALL = "ALL";
20   public static final String JavaDoc NONE = "NONE";
21
22   private final Map JavaDoc options = new HashMap JavaDoc();
23
24   public Options(String JavaDoc input, String JavaDoc[] keys, TCLogger logger, Map JavaDoc defaults) {
25     if (keys == null) { throw new NullPointerException JavaDoc("keys is null"); }
26     if (logger == null) { throw new NullPointerException JavaDoc("logger is null"); }
27     checkKeys(keys);
28
29     if (defaults == null) {
30       defaults = Collections.EMPTY_MAP;
31     }
32
33     if (input != null) {
34       input = input.replaceAll("\\s", "");
35       if (input.length() > 0) {
36         Set JavaDoc valid = new HashSet JavaDoc(Arrays.asList(keys));
37         String JavaDoc[] configValues = input.split(",");
38
39         for (int i = 0; i < configValues.length; i++) {
40           String JavaDoc value = configValues[i];
41           if (value.length() > 0) {
42             boolean negate = value.startsWith("-");
43             if (negate) {
44               value = value.substring(1);
45             }
46
47             if (value.length() == 0) {
48               continue;
49             }
50
51             if (valid.contains(value)) {
52               options.put(value, negate ? Boolean.FALSE : Boolean.TRUE);
53             } else if (ALL.equals(value)) {
54               enableAll(keys);
55             } else if (NONE.equals(value)) {
56               disableAll(keys);
57             } else {
58               logger.warn("[" + value + "] is not a valid option, ignoring it");
59             }
60           }
61         }
62       }
63     }
64
65     defaultMissingOptions(keys, defaults);
66   }
67
68   private void disableAll(String JavaDoc[] keys) {
69     for (int i = 0; i < keys.length; i++) {
70       options.put(keys[i], Boolean.FALSE);
71     }
72   }
73
74   private void checkKeys(String JavaDoc[] keys) {
75     for (int i = 0; i < keys.length; i++) {
76       String JavaDoc key = keys[i];
77       if (ALL.equals(key) || NONE.equals(key)) { throw new IllegalArgumentException JavaDoc("Illegal key " + key
78                                                                                     + " at position " + i); }
79     }
80   }
81
82   public boolean getOption(String JavaDoc opt) {
83     return ((Boolean JavaDoc) options.get(opt)).booleanValue();
84   }
85
86   private void enableAll(String JavaDoc[] keys) {
87     for (int i = 0; i < keys.length; i++) {
88       options.put(keys[i], Boolean.TRUE);
89     }
90   }
91
92   private void defaultMissingOptions(String JavaDoc[] keys, Map JavaDoc defaults) {
93     for (int i = 0; i < keys.length; i++) {
94       String JavaDoc key = keys[i];
95       if (defaults.containsKey(key) && !options.containsKey(key)) {
96         Object JavaDoc value = defaults.get(key);
97         if (value instanceof Boolean JavaDoc) {
98           options.put(key, value);
99         } else {
100           throw new IllegalArgumentException JavaDoc("Invalid default value in map for " + key + ": " + value);
101         }
102       }
103
104       // finally default option to false if no default present, and not explicitly set
105
if (!options.containsKey(key)) {
106         options.put(key, Boolean.FALSE);
107       }
108     }
109   }
110
111 }
112
Popular Tags