001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.util;
027:
028: import java.util.Properties;
029: import org.cougaar.bootstrap.SystemProperties;
030:
031: /**
032: * Utility for parsing Properties values of various types with defaults
033: * @see SystemProperties
034: */
035: public abstract class PropertyParser {
036: public static final boolean getBoolean(Properties props,
037: String prop, boolean def) {
038: return (Boolean.valueOf(props.getProperty(prop, String
039: .valueOf(def)))).booleanValue();
040: }
041:
042: public static final boolean getBoolean(String prop, boolean def) {
043: return SystemProperties.getBoolean(prop, def);
044: }
045:
046: public static final int getInt(Properties props, String prop,
047: int def) {
048: try {
049: return Integer.parseInt(props.getProperty(prop, String
050: .valueOf(def)));
051: } catch (NumberFormatException e) {
052: return def;
053: }
054: }
055:
056: public static final int getInt(String prop, int def) {
057: return SystemProperties.getInt(prop, def, true);
058: }
059:
060: public static final long getLong(Properties props, String prop,
061: long def) {
062: try {
063: return Long.parseLong(props.getProperty(prop, String
064: .valueOf(def)));
065: } catch (NumberFormatException e) {
066: return def;
067: }
068:
069: }
070:
071: public static final long getLong(String prop, long def) {
072: return SystemProperties.getLong(prop, def, true);
073: }
074:
075: public static final float getFloat(Properties props, String prop,
076: float def) {
077: try {
078: return Float.parseFloat(props.getProperty(prop, String
079: .valueOf(def)));
080: } catch (NumberFormatException e) {
081: return def;
082: }
083: }
084:
085: public static final float getFloat(String prop, float def) {
086: return SystemProperties.getFloat(prop, def, true);
087: }
088:
089: public static final double getDouble(Properties props, String prop,
090: double def) {
091: try {
092: return Double.parseDouble(props.getProperty(prop, String
093: .valueOf(def)));
094: } catch (NumberFormatException e) {
095: return def;
096: }
097: }
098:
099: public static final double getDouble(String prop, double def) {
100: return SystemProperties.getDouble(prop, def, true);
101: }
102: }
|