requiredPropertyKeys) {
for (String key : requiredPropertyKeys) {
getString(key, props);
}
}
/**
* Retrieve a required property from a Properties object. This method throws a
* MissingPropertyException if the required property is not in the provided properties file.
*
* The purpose of this method is to simplify property extraction by (1) providing better
* warning/exceptions, (2) automatically removing whitespace, and (3) make code intent clear.
*
* @param propertyKey A key for a property that MUST be in the properties object
* @param properties A Properties object
*
* @return The requested property that has been trimmed of any whitespace.
* @throws MissingPropertyException if the required property is missing
*/
public static String getString(String propertyKey, Properties properties) {
checkNotNull(propertyKey);
checkNotNull(properties);
String value = properties.getProperty(propertyKey);
if (value != null) {
return value.trim();
} else {
throw new MissingPropertyException(propertyKey);
}
}
/**
* Retrieve a value from a Properties object and parse it to the appropriate type. This method
* throws a MissingPropertyException if the required propertyKey is not found in the Properties
* object.
*
* This method simplify property extraction by (1) providing better warning/exceptions, (2)
* making code intent clear, (3) automatically casting String values to the desired types, and
* (4) automatically removing whitespace.
*
* @param propertyKey A key for a property that MUST be in the properties object
* @param properties A Properties object that stores String-String Key-Value pairs
*
* @return The requested property, trimmed, parsed, and cast appropriately.
* @throws MissingPropertyException if the required property is missing
*/
public static byte getByte(String propertyKey, Properties properties) {
return parseByte(getString(propertyKey, properties));
}
/**
* Retrieve a value from a Properties object and parse it to the appropriate type. This method
* throws a MissingPropertyException if the required propertyKey is not found in the Properties
* object.
*
* This method simplify property extraction by (1) providing better warning/exceptions, (2)
* making code intent clear, (3) automatically casting String values to the desired types, and
* (4) automatically removing whitespace.
*
* @param propertyKey A key for a property that MUST be in the properties object
* @param properties A Properties object that stores String-String Key-Value pairs
*
* @return The requested property, trimmed, parsed, and cast appropriately.
* @throws MissingPropertyException if the required property is missing
*/
public static short getShort(String propertyKey, Properties properties) {
return parseShort(getString(propertyKey, properties));
}
/**
* Retrieve a value from a Properties object and parse it to the appropriate type. This method
* throws a MissingPropertyException if the required propertyKey is not found in the Properties
* object.
*
* This method simplify property extraction by (1) providing better warning/exceptions, (2)
* making code intent clear, (3) automatically casting String values to the desired types, and
* (4) automatically removing whitespace.
*
* @param propertyKey A key for a property that MUST be in the properties object
* @param properties A Properties object that stores String-String Key-Value pairs
*
* @return The requested property, trimmed, parsed, and cast appropriately.
* @throws MissingPropertyException if the required property is missing
*/
public static int getInt(String propertyKey, Properties properties) {
return parseInt(getString(propertyKey, properties));
}
/**
* Retrieve a value from a Properties object and parse it to the appropriate type. This method
* throws a MissingPropertyException if the required propertyKey is not found in the Properties
* object.
*
* This method simplify property extraction by (1) providing better warning/exceptions, (2)
* making code intent clear, (3) automatically casting String values to the desired types, and
* (4) automatically removing whitespace.
*
* @param propertyKey A key for a property that MUST be in the properties object
* @param properties A Properties object that stores String-String Key-Value pairs
*
* @return The requested property, trimmed, parsed, and cast appropriately.
* @throws MissingPropertyException if the required property is missing
*/
public static long getLong(String propertyKey, Properties properties) {
return parseLong(getString(propertyKey, properties));
}
/**
* Retrieve a value from a Properties object and parse it to the appropriate type. This method
* throws a MissingPropertyException if the required propertyKey is not found in the Properties
* object.
*
* This method simplify property extraction by (1) providing better warning/exceptions, (2)
* making code intent clear, (3) automatically casting String values to the desired types, and
* (4) automatically removing whitespace.
*
* @param propertyKey A key for a property that MUST be in the properties object
* @param properties A Properties object that stores String-String Key-Value pairs
*
* @return The requested property, trimmed, parsed, and cast appropriately.
* @throws MissingPropertyException if the required property is missing
*/
public static float getFloat(String propertyKey, Properties properties) {
return parseFloat(getString(propertyKey, properties));
}
/**
* Retrieve a value from a Properties object and parse it to the appropriate type. This method
* throws a MissingPropertyException if the required propertyKey is not found in the Properties
* object.
*
* This method simplify property extraction by (1) providing better warning/exceptions, (2)
* making code intent clear, (3) automatically casting String values to the desired types, and
* (4) automatically removing whitespace.
*
* @param propertyKey A key for a property that MUST be in the properties object
* @param properties A Properties object that stores String-String Key-Value pairs
*
* @return The requested property, trimmed, parsed, and cast appropriately.
* @throws MissingPropertyException if the required property is missing
*/
public static double getDouble(String propertyKey, Properties properties) {
return parseDouble(getString(propertyKey, properties));
}
/**
* Retrieve a value from a Properties object and parse it to the appropriate type. This method
* throws a MissingPropertyException if the required propertyKey is not found in the Properties
* object.
*
* This method simplify property extraction by (1) providing better warning/exceptions, (2)
* making code intent clear, (3) automatically casting String values to the desired types, and
* (4) automatically removing whitespace.
*
* @param propertyKey A key for a property that MUST be in the properties object
* @param properties A Properties object that stores String-String Key-Value pairs
*
* @return The requested property, trimmed, parsed, and cast appropriately.
* @throws MissingPropertyException if the required property is missing
*/
public static boolean getBoolean(String propertyKey, Properties properties) {
checkNotNull(propertyKey);
checkNotNull(properties);
return parseBoolean(getString(propertyKey, properties));
}
/**
* Retrieve an optional property from a Properties object. Trim all whitespace from the returned
* result. Wrap the retrieved result in an Optional.
*
* The purpose of this method is to simplify property extraction by (1) providing better
* warning/exceptions, (2) automatically removing whitespace, and (3) make code intent clear.
*
* @param propertyKey A key for a property that may or may not be in the properties object
* @param properties A Properties object
*
* @return An Optional containing the corresponding property trimmed of any whitespace (if it
* exists)
*/
public static Optional getOptionalString(String propertyKey, Properties properties) {
String value = properties.getProperty(propertyKey);
if (value != null) {
return Optional.of(value.trim());
} else {
return Optional.empty();
}
}
/**
* Retrieve an optional property from a Properties object. Trim all whitespace from the returned
* result. If the property does not exist return the default value.
*
* The purpose of this method is to simplify property extraction by (1) providing better
* warning/exceptions, (2) automatically removing whitespace, and (3) make code intent clear.
*
* @param propertyKey A key for a property that may or may not be in the properties object
* @param properties A Properties object
* @param defaultValue The value returned if the propertyKey is missing.
*
* @return The request property trimmed of any whitespace or the default value if the requested
* property is missing.
*/
public static String getOptionalString(String propertyKey, Properties properties, String defaultValue) {
String value = properties.getProperty(propertyKey);
if (value != null) {
return value.trim();
} else {
return defaultValue;
}
}
public static byte getOptionalByte(String propertyKey, Properties properties, byte defaultValue) {
Optional prop = getOptionalString(propertyKey, properties);
return prop.isPresent()
? Byte.parseByte(prop.get())
: defaultValue;
}
public static short getOptionalShort(String propertyKey, Properties properties, short defaultValue) {
Optional prop = getOptionalString(propertyKey, properties);
return prop.isPresent()
? Short.parseShort(prop.get())
: defaultValue;
}
public static int getOptionalInt(String propertyKey, Properties properties, int defaultValue) {
Optional prop = getOptionalString(propertyKey, properties);
return prop.isPresent()
? Integer.parseInt(prop.get())
: defaultValue;
}
public static long getOptionalLong(String propertyKey, Properties properties, long defaultValue) {
Optional prop = getOptionalString(propertyKey, properties);
return prop.isPresent()
? Long.parseLong(prop.get())
: defaultValue;
}
public static float getOptionalFloat(String propertyKey, Properties properties, float defaultValue) {
Optional prop = getOptionalString(propertyKey, properties);
return prop.isPresent()
? Float.parseFloat(prop.get())
: defaultValue;
}
public static double getOptionalDouble(String propertyKey, Properties properties, double defaultValue) {
Optional prop = getOptionalString(propertyKey, properties);
return prop.isPresent()
? Double.parseDouble(prop.get())
: defaultValue;
}
public static boolean getOptionalBoolean(String propertyKey, Properties properties, boolean defaultValue) {
Optional prop = getOptionalString(propertyKey, properties);
return prop.isPresent()
? Boolean.parseBoolean(prop.get())
: defaultValue;
}
/**
* Tokenize a String and verify that every token in the input String appears in the collection
* of "valid tokens". Then returns a list of the tokens found.
*
* @param csvLine A line of comma separated tokens
* @param validTokens The set of tokens which are permitted to appear in this line
*
* @return An list of the tokens found while parsing (list elements are ordered according to
* appearance in input String)
*/
public static List tokenizeAndValidate(String csvLine, Collection validTokens) {
checkNotNull(validTokens);
csvLine = csvLine.trim();
if (csvLine.isEmpty()) {
return newArrayList();
}
//Tokenize, trim, and then verify the comma delimited tokens are valid
String[] splits = csvLine.split(",");
for (int i = 0; i < splits.length; i++) {
splits[i] = splits[i].trim();
checkArgument(validTokens.contains(splits[i]), "Invalid token: " + splits[i] + " found in: " + csvLine);
}
return newArrayList(splits);
}
/**
* This Exception class permits loading properties without try/catch blocks by converting
* checked IOException to RuntimeExceptions
*/
public static class PropertyLoadingException extends RuntimeException {
private static final long serialVersionUID = 1L;
PropertyLoadingException(String message, Exception cause) {
super(message, cause);
}
}
public static class MissingPropertyException extends RuntimeException {
private static final long serialVersionUID = 1L;
public MissingPropertyException(String nameOfMissingProperty) {
super("The property " + nameOfMissingProperty + " is missing");
}
}
}