001: package com.quadcap.util;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.List;
042:
043: /**
044: * This class represents a configuration value which is a number
045: *
046: * @author Stan Bailes
047: */
048: public class ConfigNumber extends MutableInteger implements ConfigVar {
049: String name;
050: String stringVal;
051:
052: public ConfigNumber() {
053: }
054:
055: public final void init(String name, String val)
056: throws NumberFormatException {
057: this .name = name;
058: this .notify(val);
059: }
060:
061: public final String getName() {
062: return name;
063: }
064:
065: public final String getValue() {
066: return stringVal;
067: }
068:
069: public final void notify(String newValue) {
070: if (newValue.indexOf(',') >= 0 || newValue.indexOf('-') >= 0) {
071: setIntValue(parseBitList(newValue));
072: } else {
073: setIntValue(Integer.parseInt(newValue));
074: }
075: this .stringVal = newValue;
076: }
077:
078: public int parseBitList(String str) {
079: List list = Util.split(str, ',');
080: int ret = 0;
081:
082: for (int i = 0; i < list.size(); i++) {
083: int first, last;
084: final String t = list.get(i).toString();
085: final int idx = t.indexOf('-');
086: if (idx > 0) {
087: first = Integer.parseInt(t.substring(0, idx));
088: last = Integer.parseInt(t.substring(idx + 1));
089: if (last < first)
090: last = first;
091: } else {
092: try {
093: first = last = Integer.parseInt(t);
094: } catch (Throwable ex) {
095: first = 1;
096: last = 0;
097: }
098: }
099: for (int test = first; test <= last; test++) {
100: ret += (1 << test);
101: }
102: }
103: return ret;
104: }
105:
106: private static final Class configClass = new ConfigNumber()
107: .getClass();
108:
109: public static ConfigNumber find(String name, String dflt) {
110: ConfigNumber ret = (ConfigNumber) Config.find(configClass,
111: name, dflt);
112: return ret;
113: }
114: }
|