Package ratpack.func
Class TypeCoercingProperties
- java.lang.Object
-
- ratpack.func.TypeCoercingProperties
-
public class TypeCoercingProperties extends Object
A delegate forProperties
that can coerce values to various types.
-
-
Constructor Summary
Constructors Constructor Description TypeCoercingProperties(Properties delegate)
TypeCoercingProperties(Properties delegate, ClassLoader classLoader)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
asBoolean(String key, boolean defaultValue)
Gets a property value as a boolean, substituting a default if the property does not exist.com.google.common.io.ByteSource
asByteSource(String key)
Gets a property value as a ByteSource.<T> Class<T>
asClass(String key, Class<T> type)
Gets a property value as a Class.InetAddress
asInetAddress(String key)
Gets a property value as an InetAddress.int
asInt(String key, int defaultValue)
Gets a property value as an int, substituting a default if the property does not exist.List<String>
asList(String key)
Gets a property value as a List of Strings.long
asLong(String key, long defaultValue)
Gets a property value as an long, substituting a default if the property does not exist.InputStream
asStream(String key)
Gets a property value as an InputStream.String
asString(String key, String defaultValue)
Gets a property value as a String, substituting a default if the property does not exist.URI
asURI(String key)
Gets a property value as a URI.
-
-
-
Constructor Detail
-
TypeCoercingProperties
public TypeCoercingProperties(Properties delegate)
-
TypeCoercingProperties
public TypeCoercingProperties(Properties delegate, ClassLoader classLoader)
-
-
Method Detail
-
asString
public String asString(String key, String defaultValue)
Gets a property value as a String, substituting a default if the property does not exist.- Parameters:
key
- the property key.defaultValue
- the value to use if the property does not exist.- Returns:
- the property value as a String or
defaultValue
if it does not exist. - See Also:
Properties.getProperty(String, String)
-
asBoolean
public boolean asBoolean(String key, boolean defaultValue)
Gets a property value as a boolean, substituting a default if the property does not exist.- Parameters:
key
- the property key.defaultValue
- the value to use if the property does not exist.- Returns:
- the property value coerced to a boolean as specified by
Boolean.parseBoolean(String)
ordefaultValue
if it does not exist.
-
asInt
public int asInt(String key, int defaultValue)
Gets a property value as an int, substituting a default if the property does not exist.- Parameters:
key
- the property key.defaultValue
- the value to use if the property does not exist.- Returns:
- the property value coerced to an int as specified by
Integer.parseInt(String)
ordefaultValue
if it does not exist.
-
asLong
public long asLong(String key, long defaultValue)
Gets a property value as an long, substituting a default if the property does not exist.- Parameters:
key
- the property key.defaultValue
- the value to use if the property does not exist.- Returns:
- the property value coerced to an long as specified by
Long.parseLong(String)
ordefaultValue
if it does not exist.
-
asURI
public URI asURI(String key) throws URISyntaxException
Gets a property value as a URI.- Parameters:
key
- the property key.- Returns:
- the URI represented by the property value or
null
if the property does not exist. - Throws:
URISyntaxException
- if the property value is not a valid URI.
-
asInetAddress
public InetAddress asInetAddress(String key) throws UnknownHostException
Gets a property value as an InetAddress.- Parameters:
key
- the property key.- Returns:
- the InetAddress represented by the property value or
null
if the property does not exist. - Throws:
UnknownHostException
- if the property value is not a valid address.
-
asList
public List<String> asList(String key)
Gets a property value as a List of Strings. Each element of the returned List is trimmed of leading and trailing whitespace. Empty elements are omitted from the List.- Parameters:
key
- the property key.- Returns:
- a List of Strings created by treating the property value as a comma-separated list or an empty List if the property does not exist.
-
asByteSource
public com.google.common.io.ByteSource asByteSource(String key)
Gets a property value as a ByteSource. The property value can be any of:- An absolute file path to a file that exists.
- A valid URI.
- A classpath resource path loaded via the ClassLoader passed to the constructor.
- Parameters:
key
- the property key.- Returns:
- a ByteSource or
null
if the property does not exist. - Throws:
IllegalArgumentException
- if the property value cannot be resolved to a byte source.
-
asStream
public InputStream asStream(String key) throws IOException
Gets a property value as an InputStream. The property value can be any of:- An absolute file path to a file that exists.
- A valid URI.
- A classpath resource path loaded via the ClassLoader passed to the constructor.
- Parameters:
key
- the property key.- Returns:
- an InputStream or
null
if the property does not exist. - Throws:
FileNotFoundException
- if the property value cannot be resolved to a stream source.IOException
-
asClass
public <T> Class<T> asClass(String key, Class<T> type) throws ClassNotFoundException
Gets a property value as a Class. The property value should be a fully-qualified class name that can be loaded via the ClassLoader passed to the constructor. The class should be an instance of thetype
parameter – typically an interface.- Parameters:
key
- the property key.type
- the expected type of the Class, typically the interface it should implement.- Returns:
- a Class or
null
if the property does not exist. - Throws:
ClassNotFoundException
- if no Class is found matching the name from the property value.ClassCastException
- if a Class is loaded but it does not implement thetype
interface.
-
-