1 4 package com.tc.object.logging; 5 6 import com.tc.logging.TCLogger; 7 8 import java.util.Arrays ; 9 import java.util.Collections ; 10 import java.util.HashMap ; 11 import java.util.HashSet ; 12 import java.util.Map ; 13 import java.util.Set ; 14 15 18 public class Options { 19 public static final String ALL = "ALL"; 20 public static final String NONE = "NONE"; 21 22 private final Map options = new HashMap (); 23 24 public Options(String input, String [] keys, TCLogger logger, Map defaults) { 25 if (keys == null) { throw new NullPointerException ("keys is null"); } 26 if (logger == null) { throw new NullPointerException ("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 valid = new HashSet (Arrays.asList(keys)); 37 String [] configValues = input.split(","); 38 39 for (int i = 0; i < configValues.length; i++) { 40 String 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 [] 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 [] keys) { 75 for (int i = 0; i < keys.length; i++) { 76 String key = keys[i]; 77 if (ALL.equals(key) || NONE.equals(key)) { throw new IllegalArgumentException ("Illegal key " + key 78 + " at position " + i); } 79 } 80 } 81 82 public boolean getOption(String opt) { 83 return ((Boolean ) options.get(opt)).booleanValue(); 84 } 85 86 private void enableAll(String [] 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 [] keys, Map defaults) { 93 for (int i = 0; i < keys.length; i++) { 94 String key = keys[i]; 95 if (defaults.containsKey(key) && !options.containsKey(key)) { 96 Object value = defaults.get(key); 97 if (value instanceof Boolean ) { 98 options.put(key, value); 99 } else { 100 throw new IllegalArgumentException ("Invalid default value in map for " + key + ": " + value); 101 } 102 } 103 104 if (!options.containsKey(key)) { 106 options.put(key, Boolean.FALSE); 107 } 108 } 109 } 110 111 } 112 | Popular Tags |