com.devexperts.util
Class SystemProperties

java.lang.Object
  extended by com.devexperts.util.SystemProperties

public final class SystemProperties
extends Object

Utility methods for retrieving system properties without getting a SecurityException. Unless otherwise is specified all methods of this class conceal and log any exceptions and return default value in case of any failure.


Method Summary
static boolean getBooleanProperty(String key, boolean defValue)
          Gets the boolean system property indicated by the specified key.
static int getIntProperty(String key, int defValue)
          Gets the integer system property indicated by the specified key.
static int getIntProperty(String key, int defValue, int minValue, int maxValue)
          Gets the integer system property indicated by the specified key and shrinks it into specified range.
static long getLongProperty(String key, long defValue)
          Gets the long system property indicated by the specified key.
static long getLongProperty(String key, long defValue, long minValue, long maxValue)
          Gets the long system property indicated by the specified key and shrinks it into specified range.
static String getProperty(String key, String defValue)
          Gets the string system property indicated by the specified key.
static boolean parseBooleanValue(String value)
          Utility method that parses boolean value from a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getProperty

public static String getProperty(String key,
                                 String defValue)
Gets the string system property indicated by the specified key.

This method behaves as System.getProperty(key, defValue), except for the fact that it conceals SecurityException.

Parameters:
key - system property key
defValue - default value
Returns:
string system property indicated by the specified key or default value if failed to acquire the property
See Also:
System.getProperty(key, def)

getIntProperty

public static int getIntProperty(String key,
                                 int defValue)
Gets the integer system property indicated by the specified key.

This method can parse numbers in decimal, hexadecimal, and octal radix (the same way as Integer.getInteger(key, defValue) method does).

Parameters:
key - system property key
defValue - default value
Returns:
integer system property indicated by the specified key or default value if failed to acquire or parse the property

getIntProperty

public static int getIntProperty(String key,
                                 int defValue,
                                 int minValue,
                                 int maxValue)
Gets the integer system property indicated by the specified key and shrinks it into specified range.

This methods behaves likely to getIntProperty(key, defValue) but if the resulting value is less than minValue or greater than maxValue a warning is logged and the corresponding boundary value is returned.

Parameters:
key - system property key
defValue - default value
minValue - minimum allowable value
maxValue - maximum allowable value
Returns:
integer system property indicated by the specified key or default value if failed to acquire or parse the property
Throws:
IllegalArgumentException - if minValue≤defValue≤maxValue condition breaks

getLongProperty

public static long getLongProperty(String key,
                                   long defValue)
Gets the long system property indicated by the specified key.

This method can parse numbers in decimal, hexadecimal, and octal radix (the same way as Long.getLong(key, defValue) method does).

Parameters:
key - system property key
defValue - default value
Returns:
long system property indicated by the specified key or default value if failed to acquire or parse the property

getLongProperty

public static long getLongProperty(String key,
                                   long defValue,
                                   long minValue,
                                   long maxValue)
Gets the long system property indicated by the specified key and shrinks it into specified range.

This methods behaves likely to getLongProperty(key, defValue) but if the resulting value is less than minValue or greater than maxValue a warning is logged and the corresponding boundary value is returned.

Parameters:
key - system property key
defValue - default value
minValue - minimum allowable value
maxValue - maximum allowable value
Returns:
long system property indicated by the specified key or default value if failed to acquire or parse the property
Throws:
IllegalArgumentException - if minValue≤defValue≤maxValue condition breaks

getBooleanProperty

public static boolean getBooleanProperty(String key,
                                         boolean defValue)
Gets the boolean system property indicated by the specified key.

Unlike Boolean.getBoolean(key) this method recognizes defined properties with empty values as true and returns the default value (and logs an error) if the value of a specified property is neither of "true", "false" or "" (case-insensetively).

Parameters:
key - system property key
defValue - default value
Returns:
boolean system property indicated by the specified key or default value if failed to acquire or parse the property

parseBooleanValue

public static boolean parseBooleanValue(String value)
                                 throws IllegalArgumentException
Utility method that parses boolean value from a string.

This method recognizes "" (empty string) and "true" as true and "false" as false (case-insensitively); all other values are considered invalid.

Parameters:
value - string to parse
Returns:
boolean value, represented by given string
Throws:
IllegalArgumentException - if the string was neither of "", "true" or "false" (case-insensitively)
NullPointerException - if value is null


Copyright © 2013 Devexperts. All Rights Reserved.