org.unitils.util
Class PropertyUtils

java.lang.Object
  extended by org.unitils.util.PropertyUtils

public class PropertyUtils
extends Object

Utilities for working with property files.

Author:
Tim Ducheyne, Filip Neven

Constructor Summary
PropertyUtils()
           
 
Method Summary
static boolean containsProperty(String propertyName, Properties properties)
          Checks whether the property with the given name exists in the System or in the given properties.
static boolean getBoolean(String propertyName, boolean defaultValue, Properties properties)
          Gets the boolean value for the property with the given name.
static boolean getBoolean(String propertyName, Properties properties)
          Gets the boolean value for the property with the given name.
static
<T> T
getInstance(String propertyName, Properties properties)
          Gets an instance of the type specified by the property with the given name.
static
<T> T
getInstance(String propertyName, T defaultValue, Properties properties)
          Gets an instance of the type specified by the property with the given name.
static int getInt(String propertyName, int defaultValue, Properties properties)
          Gets the int value for the property with the given name.
static int getInt(String propertyName, Properties properties)
          Gets the int value for the property with the given name.
static long getLong(String propertyName, long defaultValue, Properties properties)
          Gets the long value for the property with the given name.
static long getLong(String propertyName, Properties properties)
          Gets the long value for the property with the given name.
static String getString(String propertyName, Properties properties)
          Gets the string value for the property with the given name.
static String getString(String propertyName, String defaultValue, Properties properties)
          Gets the string value for the property with the given name.
static List<String> getStringList(String propertyName, Properties properties)
          Gets the list of comma separated string values for the property with the given name.
static List<String> getStringList(String propertyName, Properties properties, boolean required)
          Gets the list of comma separated string values for the property with the given name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertyUtils

public PropertyUtils()
Method Detail

getString

public static String getString(String propertyName,
                               Properties properties)
Gets the string value for the property with the given name. If no such property is found or the value is empty, an exception will be raised.

Parameters:
propertyName - The name, not null
properties - The properties, not null
Returns:
The trimmed string value, not null

getString

public static String getString(String propertyName,
                               String defaultValue,
                               Properties properties)
Gets the string value for the property with the given name. If no such property is found or the value is empty, the given default value is returned.

Parameters:
propertyName - The name, not null
defaultValue - The default value
properties - The properties, not null
Returns:
The trimmed string value, not null

getStringList

public static List<String> getStringList(String propertyName,
                                         Properties properties)
Gets the list of comma separated string values for the property with the given name. If no such property is found or the value is empty, an empty list is returned. Empty elements (",,") will not be added. A space (", ,") is not empty, a "" will be added.

Parameters:
propertyName - The name, not null
properties - The properties, not null
Returns:
The trimmed string list, empty if none found

getStringList

public static List<String> getStringList(String propertyName,
                                         Properties properties,
                                         boolean required)
Gets the list of comma separated string values for the property with the given name. If no such property is found or the value is empty, an empty list is returned if not required, else an exception is raised. Empty elements (",,") will not be added. A space (", ,") is not empty, a "" will be added.

Parameters:
propertyName - The name, not null
properties - The properties, not null
required - If true an exception will be raised when the property is not found or empty
Returns:
The trimmed string list, empty or exception if none found

getBoolean

public static boolean getBoolean(String propertyName,
                                 Properties properties)
Gets the boolean value for the property with the given name. If no such property is found or the value is empty, an exception will be raised.

Parameters:
propertyName - The name, not null
properties - The properties, not null
Returns:
The boolean value, not null

getBoolean

public static boolean getBoolean(String propertyName,
                                 boolean defaultValue,
                                 Properties properties)
Gets the boolean value for the property with the given name. If no such property is found or the value is empty, the given default value is returned.

Parameters:
propertyName - The name, not null
defaultValue - The default value
properties - The properties, not null
Returns:
The boolean value, not null

getLong

public static long getLong(String propertyName,
                           Properties properties)
Gets the long value for the property with the given name. If no such property is found, the value is empty or cannot be converted to a long, an exception will be raised.

Parameters:
propertyName - The name, not null
properties - The properties, not null
Returns:
The long value, not null

getLong

public static long getLong(String propertyName,
                           long defaultValue,
                           Properties properties)
Gets the long value for the property with the given name. If no such property is found or the value is empty, the given default value is returned. If the value cannot be converted to a long, an exception will be raised.

Parameters:
propertyName - The name, not null
defaultValue - The default value
properties - The properties, not null
Returns:
The long value, not null

getInt

public static int getInt(String propertyName,
                         Properties properties)
Gets the int value for the property with the given name. If no such property is found, the value is empty or cannot be converted to a long, an exception will be raised.

Parameters:
propertyName - The name, not null
properties - The properties, not null
Returns:
The int value, not null

getInt

public static int getInt(String propertyName,
                         int defaultValue,
                         Properties properties)
Gets the int value for the property with the given name. If no such property is found or the value is empty, the given default value is returned. If the value cannot be converted to a long, an exception will be raised.

Parameters:
propertyName - The name, not null
defaultValue - The default value
properties - The properties, not null
Returns:
The int value, not null

containsProperty

public static boolean containsProperty(String propertyName,
                                       Properties properties)
Checks whether the property with the given name exists in the System or in the given properties.

Parameters:
propertyName - The property name, not null
properties - The properties if not found in System, not null
Returns:
True if the property exitsts

getInstance

public static <T> T getInstance(String propertyName,
                                Properties properties)
Gets an instance of the type specified by the property with the given name. If no such property is found, the value is empty or the instance cannot be created, an exception will be raised.

Parameters:
propertyName - The name, not null
properties - The properties, not null
Returns:
The instance value, not null

getInstance

public static <T> T getInstance(String propertyName,
                                T defaultValue,
                                Properties properties)
Gets an instance of the type specified by the property with the given name. If no such property is found, the value is empty, the given default value is returned. If the instance cannot be created an exception will be raised.

Parameters:
propertyName - The name, not null
defaultValue - The default value
properties - The properties, not null
Returns:
The instance value, not null


Copyright © 2011. All Rights Reserved.