Package org.datavec.api.conf
Class Configuration
- java.lang.Object
-
- org.datavec.api.conf.Configuration
-
public class Configuration extends Object implements Iterable<Map.Entry<String,String>>, Writable, Serializable
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Configuration.IntegerRanges
A class that represents a set of positive integer ranges.
-
Constructor Summary
Constructors Constructor Description Configuration()
A new configuration.Configuration(boolean loadDefaults)
A new configuration where the behavior of reading from the default resources can be turned off.Configuration(Configuration other)
A new configuration with the same settings cloned from another.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
addDefaultResource(String name)
Add a default resource.void
addResource(InputStream in)
Add a configuration resource.void
addResource(String name)
Add a configuration resource.void
addResource(URL url)
Add a configuration resource.void
clear()
Clears all keys from the configuration.static void
dumpConfiguration(Configuration conf, Writer out)
Writes out all the parameters and their properties (final and resource) to the givenWriter
The format of the output would be { "properties" : [ {key1,value1,key1.isFinal,key1.resource}, {key2,value2, key2.isFinal,key2.resource}...String
get(String name)
Get the value of thename
property,null
if no such property exists.String
get(String name, String defaultValue)
Get the value of thename
property.boolean
getBoolean(String name, boolean defaultValue)
Get the value of thename
property as aboolean
.char
getChar(String name)
Get the char value of thename
property,null
if no such property exists.char
getChar(String name, char defaultValue)
Get the char value of thename
property,null
if no such property exists.Class<?>
getClass(String name, Class<?> defaultValue)
Get the value of thename
property as aClass
.<U> Class<? extends U>
getClass(String name, Class<? extends U> defaultValue, Class<U> xface)
Get the value of thename
property as aClass
implementing the interface specified byxface
.Class<?>
getClassByName(String name)
Load a class by name.Class<?>[]
getClasses(String name, Class<?>... defaultValue)
Get the value of thename
property as an array ofClass
.ClassLoader
getClassLoader()
Get theClassLoader
for this job.InputStream
getConfResourceAsInputStream(String name)
Get an input stream attached to the configuration resource with the givenname
.Reader
getConfResourceAsReader(String name)
Get aReader
attached to the configuration resource with the givenname
.File
getFile(String dirsProp, String path)
Get a local file name under a directory named in dirsProp with the given path.float
getFloat(String name, float defaultValue)
Get the value of thename
property as afloat
.<U> List<U>
getInstances(String name, Class<U> xface)
Get the value of thename
property as aList
of objects implementing the interface specified byxface
.int
getInt(String name, int defaultValue)
Get the value of thename
property as anint
.long
getLong(String name, long defaultValue)
Get the value of thename
property as along
.Pattern
getPattern(String name, Pattern defaultValue)
Get the value of thename
property as aPattern. Configuration.IntegerRanges
getRange(String name, String defaultValue)
Parse the given attribute as a set of integer rangesString
getRaw(String name)
Get the value of thename
property, without doing variable expansion.URL
getResource(String name)
Get theURL
for the named resource.Collection<String>
getStringCollection(String name)
Get the comma delimited values of thename
property as a collection ofString
s.String[]
getStrings(String name)
Get the comma delimited values of thename
property as an array ofString
s.String[]
getStrings(String name, String... defaultValue)
Get the comma delimited values of thename
property as an array ofString
s.Collection<String>
getTrimmedStringCollection(String name)
Get the comma delimited values of thename
property as a collection ofString
s, trimmed of the leading and trailing whitespace.String[]
getTrimmedStrings(String name)
Get the comma delimited values of thename
property as an array ofString
s, trimmed of the leading and trailing whitespace.String[]
getTrimmedStrings(String name, String... defaultValue)
Get the comma delimited values of thename
property as an array ofString
s, trimmed of the leading and trailing whitespace.WritableType
getType()
Get the type of the writable.Iterator<Map.Entry<String,String>>
iterator()
Get anIterator
to go through the list ofString
key-value pairs in the configuration.static void
main(String[] args)
For debugging.void
readFields(DataInput in)
Deserialize the fields of this object fromin
.void
reloadConfiguration()
Reload configuration from previously added resources.void
set(String name, String value)
Set thevalue
of thename
property.void
setBoolean(String name, boolean value)
Set the value of thename
property to aboolean
.void
setBooleanIfUnset(String name, boolean value)
Set the given property, if it is currently unset.void
setClass(String name, Class<?> theClass, Class<?> xface)
Set the value of thename
property to the name of atheClass
implementing the given interfacexface
.void
setClassLoader(ClassLoader classLoader)
Set the class loader that will be used to load the various objects.void
setFloat(String name, float value)
Set the value of thename
property to afloat
.void
setIfUnset(String name, String value)
Sets a property if it is currently unset.void
setInt(String name, int value)
Set the value of thename
property to anint
.void
setLong(String name, long value)
Set the value of thename
property to along
.void
setPattern(String name, Pattern pattern)
Set the given property toPattern
.void
setQuietMode(boolean quietmode)
Set the quietness-mode.void
setStrings(String name, String... values)
Set the array of string values for thename
property as as comma delimited values.int
size()
Return the number of keys in the configuration.double
toDouble()
Convert Writable to double.float
toFloat()
Convert Writable to float.int
toInt()
Convert Writable to int.long
toLong()
Convert Writable to long.String
toString()
void
write(DataOutput out)
Serialize the fields of this object toout
.void
writeType(DataOutput out)
Write the type (a single short value) to the DataOutput.void
writeXml(OutputStream out)
Write out the non-default properties in this configuration to the giveOutputStream
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
Configuration
public Configuration()
A new configuration.
-
Configuration
public Configuration(boolean loadDefaults)
A new configuration where the behavior of reading from the default resources can be turned off. If the parameterloadDefaults
is false, the new instance will not load resources from the default files.- Parameters:
loadDefaults
- specifies whether to load from the default files
-
Configuration
public Configuration(Configuration other)
A new configuration with the same settings cloned from another.- Parameters:
other
- the configuration from which to clone settings.
-
-
Method Detail
-
addDefaultResource
public static void addDefaultResource(String name)
Add a default resource. Resources are loaded in the order of the resources added.- Parameters:
name
- file name. File should be present in the classpath.
-
addResource
public void addResource(String name)
Add a configuration resource. The properties of this resource will override properties of previously added resources, unless they were marked final.- Parameters:
name
- resource to be added, the classpath is examined for a file with that name.
-
addResource
public void addResource(URL url)
Add a configuration resource. The properties of this resource will override properties of previously added resources, unless they were marked final.- Parameters:
url
- url of the resource to be added, the local filesystem is examined directly to find the resource, without referring to the classpath.
-
addResource
public void addResource(InputStream in)
Add a configuration resource. The properties of this resource will override properties of previously added resources, unless they were marked final.- Parameters:
in
- InputStream to deserialize the object from.
-
reloadConfiguration
public void reloadConfiguration()
Reload configuration from previously added resources. This method will clear all the configuration read from the added resources, and final parameters. This will make the resources to be read again before accessing the values. Values that are added via set methods will overlay values read from the resources.
-
get
public String get(String name)
Get the value of thename
property,null
if no such property exists. Values are processed for variable expansion before being returned.- Parameters:
name
- the property name.- Returns:
- the value of the
name
property, or null if no such property exists.
-
getRaw
public String getRaw(String name)
Get the value of thename
property, without doing variable expansion.- Parameters:
name
- the property name.- Returns:
- the value of the
name
property, or null if no such property exists.
-
getChar
public char getChar(String name)
Get the char value of thename
property,null
if no such property exists. Values are processed for variable expansion before being returned.- Parameters:
name
- the property name.- Returns:
- the value of the
name
property, or null if no such property exists.
-
getChar
public char getChar(String name, char defaultValue)
Get the char value of thename
property,null
if no such property exists. Values are processed for variable expansion before being returned.- Parameters:
name
- the property name.- Returns:
- the value of the
name
property, or null if no such property exists.
-
set
public void set(String name, String value)
Set thevalue
of thename
property.- Parameters:
name
- property name.value
- property value.
-
setIfUnset
public void setIfUnset(String name, String value)
Sets a property if it is currently unset.- Parameters:
name
- the property namevalue
- the new value
-
get
public String get(String name, String defaultValue)
Get the value of thename
property. If no such property exists, thendefaultValue
is returned.- Parameters:
name
- property name.defaultValue
- default value.- Returns:
- property value, or
defaultValue
if the property doesn't exist.
-
getInt
public int getInt(String name, int defaultValue)
Get the value of thename
property as anint
. If no such property exists, or if the specified value is not a validint
, thendefaultValue
is returned.- Parameters:
name
- property name.defaultValue
- default value.- Returns:
- property value as an
int
, ordefaultValue
.
-
setInt
public void setInt(String name, int value)
Set the value of thename
property to anint
.- Parameters:
name
- property name.value
-int
value of the property.
-
getLong
public long getLong(String name, long defaultValue)
Get the value of thename
property as along
. If no such property is specified, or if the specified value is not a validlong
, thendefaultValue
is returned.- Parameters:
name
- property name.defaultValue
- default value.- Returns:
- property value as a
long
, ordefaultValue
.
-
setLong
public void setLong(String name, long value)
Set the value of thename
property to along
.- Parameters:
name
- property name.value
-long
value of the property.
-
getFloat
public float getFloat(String name, float defaultValue)
Get the value of thename
property as afloat
. If no such property is specified, or if the specified value is not a validfloat
, thendefaultValue
is returned.- Parameters:
name
- property name.defaultValue
- default value.- Returns:
- property value as a
float
, ordefaultValue
.
-
setFloat
public void setFloat(String name, float value)
Set the value of thename
property to afloat
.- Parameters:
name
- property name.value
- property value.
-
getBoolean
public boolean getBoolean(String name, boolean defaultValue)
Get the value of thename
property as aboolean
. If no such property is specified, or if the specified value is not a validboolean
, thendefaultValue
is returned.- Parameters:
name
- property name.defaultValue
- default value.- Returns:
- property value as a
boolean
, ordefaultValue
.
-
setBoolean
public void setBoolean(String name, boolean value)
Set the value of thename
property to aboolean
.- Parameters:
name
- property name.value
-boolean
value of the property.
-
setBooleanIfUnset
public void setBooleanIfUnset(String name, boolean value)
Set the given property, if it is currently unset.- Parameters:
name
- property namevalue
- new value
-
getPattern
public Pattern getPattern(String name, Pattern defaultValue)
Get the value of thename
property as aPattern. If no such property is specified, or if the specified value is not a valid Pattern
, thenDefaultValue
is returned.- Parameters:
name
- property namedefaultValue
- default value- Returns:
- property value as a compiled Pattern, or defaultValue
-
setPattern
public void setPattern(String name, Pattern pattern)
Set the given property toPattern
. If the pattern is passed as null, sets the empty pattern which results in further calls to getPattern(...) returning the default value.- Parameters:
name
- property namepattern
- new value
-
write
public void write(DataOutput out) throws IOException
Description copied from interface:Writable
Serialize the fields of this object toout
.- Specified by:
write
in interfaceWritable
- Parameters:
out
-DataOuput
to serialize this object into.- Throws:
IOException
-
readFields
public void readFields(DataInput in) throws IOException
Description copied from interface:Writable
Deserialize the fields of this object fromin
.For efficiency, implementations should attempt to re-use storage in the existing object where possible.
- Specified by:
readFields
in interfaceWritable
- Parameters:
in
-DataInput
to deseriablize this object from.- Throws:
IOException
-
getRange
public Configuration.IntegerRanges getRange(String name, String defaultValue)
Parse the given attribute as a set of integer ranges- Parameters:
name
- the attribute namedefaultValue
- the default value if it is not set- Returns:
- a new set of ranges from the configured value
-
getStringCollection
public Collection<String> getStringCollection(String name)
Get the comma delimited values of thename
property as a collection ofString
s. If no such property is specified then empty collection is returned.This is an optimized version of
getStrings(String)
- Parameters:
name
- property name.- Returns:
- property value as a collection of
String
s.
-
getStrings
public String[] getStrings(String name)
Get the comma delimited values of thename
property as an array ofString
s. If no such property is specified thennull
is returned.- Parameters:
name
- property name.- Returns:
- property value as an array of
String
s, ornull
.
-
getStrings
public String[] getStrings(String name, String... defaultValue)
Get the comma delimited values of thename
property as an array ofString
s. If no such property is specified then default value is returned.- Parameters:
name
- property name.defaultValue
- The default value- Returns:
- property value as an array of
String
s, or default value.
-
getTrimmedStringCollection
public Collection<String> getTrimmedStringCollection(String name)
Get the comma delimited values of thename
property as a collection ofString
s, trimmed of the leading and trailing whitespace. If no such property is specified then emptyCollection
is returned.- Parameters:
name
- property name.- Returns:
- property value as a collection of
String
s, or emptyCollection
-
getTrimmedStrings
public String[] getTrimmedStrings(String name)
Get the comma delimited values of thename
property as an array ofString
s, trimmed of the leading and trailing whitespace. If no such property is specified then an empty array is returned.- Parameters:
name
- property name.- Returns:
- property value as an array of trimmed
String
s, or empty array.
-
getTrimmedStrings
public String[] getTrimmedStrings(String name, String... defaultValue)
Get the comma delimited values of thename
property as an array ofString
s, trimmed of the leading and trailing whitespace. If no such property is specified then default value is returned.- Parameters:
name
- property name.defaultValue
- The default value- Returns:
- property value as an array of trimmed
String
s, or default value.
-
setStrings
public void setStrings(String name, String... values)
Set the array of string values for thename
property as as comma delimited values.- Parameters:
name
- property name.values
- The values
-
getClassByName
public Class<?> getClassByName(String name) throws ClassNotFoundException
Load a class by name.- Parameters:
name
- the class name.- Returns:
- the class object.
- Throws:
ClassNotFoundException
- if the class is not found.
-
getClasses
public Class<?>[] getClasses(String name, Class<?>... defaultValue)
Get the value of thename
property as an array ofClass
. The value of the property specifies a list of comma separated class names. If no such property is specified, thendefaultValue
is returned.- Parameters:
name
- the property name.defaultValue
- default value.- Returns:
- property value as a
Class[]
, ordefaultValue
.
-
getClass
public Class<?> getClass(String name, Class<?> defaultValue)
Get the value of thename
property as aClass
. If no such property is specified, thendefaultValue
is returned.- Parameters:
name
- the class name.defaultValue
- default value.- Returns:
- property value as a
Class
, ordefaultValue
.
-
getClass
public <U> Class<? extends U> getClass(String name, Class<? extends U> defaultValue, Class<U> xface)
Get the value of thename
property as aClass
implementing the interface specified byxface
. If no such property is specified, thendefaultValue
is returned. An exception is thrown if the returned class does not implement the named interface.- Parameters:
name
- the class name.defaultValue
- default value.xface
- the interface implemented by the named class.- Returns:
- property value as a
Class
, ordefaultValue
.
-
getInstances
public <U> List<U> getInstances(String name, Class<U> xface)
Get the value of thename
property as aList
of objects implementing the interface specified byxface
. An exception is thrown if any of the classes does not exist, or if it does not implement the named interface.- Parameters:
name
- the property name.xface
- the interface implemented by the classes named byname
.- Returns:
- a
List
of objects implementingxface
.
-
setClass
public void setClass(String name, Class<?> theClass, Class<?> xface)
Set the value of thename
property to the name of atheClass
implementing the given interfacexface
. An exception is thrown iftheClass
does not implement the interfacexface
.- Parameters:
name
- property name.theClass
- property value.xface
- the interface implemented by the named class.
-
getFile
public File getFile(String dirsProp, String path) throws IOException
Get a local file name under a directory named in dirsProp with the given path. If dirsProp contains multiple directories, then one is chosen based on path's hash code. If the selected directory does not exist, an attempt is made to create it.- Parameters:
dirsProp
- directory in which to locate the file.path
- file-path.- Returns:
- local file under the directory with the given path.
- Throws:
IOException
-
getResource
public URL getResource(String name)
Get theURL
for the named resource.- Parameters:
name
- resource name.- Returns:
- the url for the named resource.
-
getConfResourceAsInputStream
public InputStream getConfResourceAsInputStream(String name)
Get an input stream attached to the configuration resource with the givenname
.- Parameters:
name
- configuration resource name.- Returns:
- an input stream attached to the resource.
-
getConfResourceAsReader
public Reader getConfResourceAsReader(String name)
Get aReader
attached to the configuration resource with the givenname
.- Parameters:
name
- configuration resource name.- Returns:
- a reader attached to the resource.
-
size
public int size()
Return the number of keys in the configuration.- Returns:
- number of keys in the configuration.
-
clear
public void clear()
Clears all keys from the configuration.
-
iterator
public Iterator<Map.Entry<String,String>> iterator()
Get anIterator
to go through the list ofString
key-value pairs in the configuration.
-
writeXml
public void writeXml(OutputStream out) throws IOException
Write out the non-default properties in this configuration to the giveOutputStream
.- Parameters:
out
- the output stream to write to.- Throws:
IOException
-
dumpConfiguration
public static void dumpConfiguration(Configuration conf, Writer out) throws IOException
Writes out all the parameters and their properties (final and resource) to the givenWriter
The format of the output would be { "properties" : [ {key1,value1,key1.isFinal,key1.resource}, {key2,value2, key2.isFinal,key2.resource}... ] } It does not output the parameters of the configuration object which is loaded from an input stream.- Parameters:
out
- the Writer to write to- Throws:
IOException
-
getClassLoader
public ClassLoader getClassLoader()
Get theClassLoader
for this job.- Returns:
- the correct class loader.
-
setClassLoader
public void setClassLoader(ClassLoader classLoader)
Set the class loader that will be used to load the various objects.- Parameters:
classLoader
- the new class loader.
-
setQuietMode
public void setQuietMode(boolean quietmode)
Set the quietness-mode. In the quiet-mode, error and informational messages might not be logged.- Parameters:
quietmode
-true
to set quiet-mode on,false
to turn it off.
-
main
public static void main(String[] args) throws Exception
For debugging. List non-default properties to the terminal and exit.- Throws:
Exception
-
toDouble
public double toDouble()
Description copied from interface:Writable
Convert Writable to double. Whether this is supported depends on the specific writable.
-
toFloat
public float toFloat()
Description copied from interface:Writable
Convert Writable to float. Whether this is supported depends on the specific writable.
-
toInt
public int toInt()
Description copied from interface:Writable
Convert Writable to int. Whether this is supported depends on the specific writable.
-
toLong
public long toLong()
Description copied from interface:Writable
Convert Writable to long. Whether this is supported depends on the specific writable.
-
getType
public WritableType getType()
Description copied from interface:Writable
Get the type of the writable.
-
writeType
public void writeType(DataOutput out) throws IOException
Description copied from interface:Writable
Write the type (a single short value) to the DataOutput. SeeWritableFactory
for details.- Specified by:
writeType
in interfaceWritable
- Parameters:
out
- DataOutput to write to- Throws:
IOException
- For errors during writing
-
-