SystemProperty.java Source code

Java tutorial

Introduction

Here is the source code for SystemProperty.java

Source

/*
 * Copyright 2006 (C) TJDO.
 * All rights reserved.
 *
 * This software is distributed under the terms of the TJDO License version 1.0.
 * See the terms of the TJDO License in the documentation provided with this software.
 *
 * $Id: SystemProperty.java,v 1.1 2006/10/11 23:37:23 jackknifebarber Exp $
 */

/**
 * Static methods for retrieving and system properties and converting them to
 * various types.
 * <p>
 * These methods are very similar to others in <tt>java.lang</tt>; for example,
 * {@link #intValue} is nearly the same as <tt>Integer.getInteger(String,int)</tt>.
 * The main difference is that these methods log warnings for invalid property
 * values.
 *
 * @author <a href="mailto:jackknifebarber@users.sourceforge.net">Mike Martin</a>
 * @version $Revision: 1.1 $
 */

public final class SystemProperty {

    /** Private constructor prevents instantiation. */
    private SystemProperty() {
    }

    /**
     * Returns the value of a system property as a boolean.
     * If such a system property exists it is converted to a boolean with
     * <tt>Boolean.valueOf(String)</tt>.
     * Returns the specified default value if no such system property exists or
     * the property value is invalid.
     *
     * @return
     *      the boolean value of the specified property
     */
    public static boolean booleanValue(String propName, boolean defaultValue) {
        String prop = System.getProperty(propName);
        boolean val;

        if (prop == null)
            val = defaultValue;
        else
            val = Boolean.valueOf(prop.trim()).booleanValue();

        return val;
    }

    /**
     * Returns the value of a system property as an integer.
     * If such a system property exists it is converted to an integer with
     * <tt>Integer.decode(String)</tt>.
     * Returns the specified default value if no such system property exists or
     * the property value is invalid.
     *
     * @return
     *      the integer value of the specified property
     */
    public static int intValue(String propName, int defaultValue) {
        String prop = System.getProperty(propName);
        int val;

        if (prop == null)
            val = defaultValue;
        else {
            try {
                val = Integer.decode(prop.trim()).intValue();
            } catch (NumberFormatException e) {

                val = defaultValue;
            }
        }

        return val;
    }

    /**
     * Returns the value of a system property as a String.
     * Returns the specified default value if no such system property exists.
     *
     * @return
     *      the String value of the specified property
     */
    public static String stringValue(String propName, String defaultValue) {
        String val = System.getProperty(propName);

        if (val == null)
            val = defaultValue;

        return val;
    }
}