1 package com.quadcap.util; 2 3 40 41 import java.util.List ; 42 43 48 public class ConfigNumber extends MutableInteger implements ConfigVar { 49 String name; 50 String stringVal; 51 52 public ConfigNumber() {} 53 54 public final void init(String name, String val) 55 throws NumberFormatException 56 { 57 this.name = name; 58 this.notify(val); 59 } 60 61 public final String getName() { 62 return name; 63 } 64 65 public final String getValue() { 66 return stringVal; 67 } 68 69 public final void notify(String newValue) { 70 if (newValue.indexOf(',') >= 0 || newValue.indexOf('-') >= 0) { 71 setIntValue(parseBitList(newValue)); 72 } else { 73 setIntValue(Integer.parseInt(newValue)); 74 } 75 this.stringVal = newValue; 76 } 77 78 public int parseBitList(String str) { 79 List list = Util.split(str, ','); 80 int ret = 0; 81 82 for (int i = 0; i < list.size(); i++) { 83 int first, last; 84 final String t = list.get(i).toString(); 85 final int idx = t.indexOf('-'); 86 if (idx > 0) { 87 first = Integer.parseInt(t.substring(0, idx)); 88 last = Integer.parseInt(t.substring(idx+1)); 89 if (last < first) last = first; 90 } else { 91 try { 92 first = last = Integer.parseInt(t); 93 } catch (Throwable ex) { 94 first = 1; 95 last = 0; 96 } 97 } 98 for (int test = first; test <= last; test++) { 99 ret += (1 << test); 100 } 101 } 102 return ret; 103 } 104 105 private static final Class configClass = new ConfigNumber().getClass(); 106 107 public static ConfigNumber find(String name, String dflt) { 108 ConfigNumber ret = (ConfigNumber)Config.find(configClass, name, dflt); 109 return ret; 110 } 111 } 112 | Popular Tags |