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 classConfiguration.IntegerRangesA 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 voidaddDefaultResource(String name)Add a default resource.voidaddResource(InputStream in)Add a configuration resource.voidaddResource(String name)Add a configuration resource.voidaddResource(URL url)Add a configuration resource.voidclear()Clears all keys from the configuration.static voiddumpConfiguration(Configuration conf, Writer out)Writes out all the parameters and their properties (final and resource) to the givenWriterThe format of the output would be { "properties" : [ {key1,value1,key1.isFinal,key1.resource}, {key2,value2, key2.isFinal,key2.resource}...Stringget(String name)Get the value of thenameproperty,nullif no such property exists.Stringget(String name, String defaultValue)Get the value of thenameproperty.booleangetBoolean(String name, boolean defaultValue)Get the value of thenameproperty as aboolean.chargetChar(String name)Get the char value of thenameproperty,nullif no such property exists.chargetChar(String name, char defaultValue)Get the char value of thenameproperty,nullif no such property exists.Class<?>getClass(String name, Class<?> defaultValue)Get the value of thenameproperty as aClass.<U> Class<? extends U>getClass(String name, Class<? extends U> defaultValue, Class<U> xface)Get the value of thenameproperty as aClassimplementing the interface specified byxface.Class<?>getClassByName(String name)Load a class by name.Class<?>[]getClasses(String name, Class<?>... defaultValue)Get the value of thenameproperty as an array ofClass.ClassLoadergetClassLoader()Get theClassLoaderfor this job.InputStreamgetConfResourceAsInputStream(String name)Get an input stream attached to the configuration resource with the givenname.ReadergetConfResourceAsReader(String name)Get aReaderattached to the configuration resource with the givenname.FilegetFile(String dirsProp, String path)Get a local file name under a directory named in dirsProp with the given path.floatgetFloat(String name, float defaultValue)Get the value of thenameproperty as afloat.<U> List<U>getInstances(String name, Class<U> xface)Get the value of thenameproperty as aListof objects implementing the interface specified byxface.intgetInt(String name, int defaultValue)Get the value of thenameproperty as anint.longgetLong(String name, long defaultValue)Get the value of thenameproperty as along.PatterngetPattern(String name, Pattern defaultValue)Get the value of thenameproperty as aPattern. Configuration.IntegerRangesgetRange(String name, String defaultValue)Parse the given attribute as a set of integer rangesStringgetRaw(String name)Get the value of thenameproperty, without doing variable expansion.URLgetResource(String name)Get theURLfor the named resource.Collection<String>getStringCollection(String name)Get the comma delimited values of thenameproperty as a collection ofStrings.String[]getStrings(String name)Get the comma delimited values of thenameproperty as an array ofStrings.String[]getStrings(String name, String... defaultValue)Get the comma delimited values of thenameproperty as an array ofStrings.Collection<String>getTrimmedStringCollection(String name)Get the comma delimited values of thenameproperty as a collection ofStrings, trimmed of the leading and trailing whitespace.String[]getTrimmedStrings(String name)Get the comma delimited values of thenameproperty as an array ofStrings, trimmed of the leading and trailing whitespace.String[]getTrimmedStrings(String name, String... defaultValue)Get the comma delimited values of thenameproperty as an array ofStrings, trimmed of the leading and trailing whitespace.WritableTypegetType()Get the type of the writable.Iterator<Map.Entry<String,String>>iterator()Get anIteratorto go through the list ofStringkey-value pairs in the configuration.static voidmain(String[] args)For debugging.voidreadFields(DataInput in)Deserialize the fields of this object fromin.voidreloadConfiguration()Reload configuration from previously added resources.voidset(String name, String value)Set thevalueof thenameproperty.voidsetBoolean(String name, boolean value)Set the value of thenameproperty to aboolean.voidsetBooleanIfUnset(String name, boolean value)Set the given property, if it is currently unset.voidsetClass(String name, Class<?> theClass, Class<?> xface)Set the value of thenameproperty to the name of atheClassimplementing the given interfacexface.voidsetClassLoader(ClassLoader classLoader)Set the class loader that will be used to load the various objects.voidsetFloat(String name, float value)Set the value of thenameproperty to afloat.voidsetIfUnset(String name, String value)Sets a property if it is currently unset.voidsetInt(String name, int value)Set the value of thenameproperty to anint.voidsetLong(String name, long value)Set the value of thenameproperty to along.voidsetPattern(String name, Pattern pattern)Set the given property toPattern.voidsetQuietMode(boolean quietmode)Set the quietness-mode.voidsetStrings(String name, String... values)Set the array of string values for thenameproperty as as comma delimited values.intsize()Return the number of keys in the configuration.doubletoDouble()Convert Writable to double.floattoFloat()Convert Writable to float.inttoInt()Convert Writable to int.longtoLong()Convert Writable to long.StringtoString()voidwrite(DataOutput out)Serialize the fields of this object toout.voidwriteType(DataOutput out)Write the type (a single short value) to the DataOutput.voidwriteXml(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 parameterloadDefaultsis 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 thenameproperty,nullif no such property exists. Values are processed for variable expansion before being returned.- Parameters:
name- the property name.- Returns:
- the value of the
nameproperty, or null if no such property exists.
-
getRaw
public String getRaw(String name)
Get the value of thenameproperty, without doing variable expansion.- Parameters:
name- the property name.- Returns:
- the value of the
nameproperty, or null if no such property exists.
-
getChar
public char getChar(String name)
Get the char value of thenameproperty,nullif no such property exists. Values are processed for variable expansion before being returned.- Parameters:
name- the property name.- Returns:
- the value of the
nameproperty, or null if no such property exists.
-
getChar
public char getChar(String name, char defaultValue)
Get the char value of thenameproperty,nullif no such property exists. Values are processed for variable expansion before being returned.- Parameters:
name- the property name.- Returns:
- the value of the
nameproperty, or null if no such property exists.
-
set
public void set(String name, String value)
Set thevalueof thenameproperty.- 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 thenameproperty. If no such property exists, thendefaultValueis returned.- Parameters:
name- property name.defaultValue- default value.- Returns:
- property value, or
defaultValueif the property doesn't exist.
-
getInt
public int getInt(String name, int defaultValue)
Get the value of thenameproperty as anint. If no such property exists, or if the specified value is not a validint, thendefaultValueis 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 thenameproperty to anint.- Parameters:
name- property name.value-intvalue of the property.
-
getLong
public long getLong(String name, long defaultValue)
Get the value of thenameproperty as along. If no such property is specified, or if the specified value is not a validlong, thendefaultValueis 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 thenameproperty to along.- Parameters:
name- property name.value-longvalue of the property.
-
getFloat
public float getFloat(String name, float defaultValue)
Get the value of thenameproperty as afloat. If no such property is specified, or if the specified value is not a validfloat, thendefaultValueis 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 thenameproperty to afloat.- Parameters:
name- property name.value- property value.
-
getBoolean
public boolean getBoolean(String name, boolean defaultValue)
Get the value of thenameproperty as aboolean. If no such property is specified, or if the specified value is not a validboolean, thendefaultValueis 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 thenameproperty to aboolean.- Parameters:
name- property name.value-booleanvalue 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 thenameproperty as aPattern. If no such property is specified, or if the specified value is not a valid Pattern, thenDefaultValueis 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:WritableSerialize the fields of this object toout.- Specified by:
writein interfaceWritable- Parameters:
out-DataOuputto serialize this object into.- Throws:
IOException
-
readFields
public void readFields(DataInput in) throws IOException
Description copied from interface:WritableDeserialize the fields of this object fromin.For efficiency, implementations should attempt to re-use storage in the existing object where possible.
- Specified by:
readFieldsin interfaceWritable- Parameters:
in-DataInputto 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 thenameproperty as a collection ofStrings. 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
Strings.
-
getStrings
public String[] getStrings(String name)
Get the comma delimited values of thenameproperty as an array ofStrings. If no such property is specified thennullis returned.- Parameters:
name- property name.- Returns:
- property value as an array of
Strings, ornull.
-
getStrings
public String[] getStrings(String name, String... defaultValue)
Get the comma delimited values of thenameproperty as an array ofStrings. 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
Strings, or default value.
-
getTrimmedStringCollection
public Collection<String> getTrimmedStringCollection(String name)
Get the comma delimited values of thenameproperty as a collection ofStrings, trimmed of the leading and trailing whitespace. If no such property is specified then emptyCollectionis returned.- Parameters:
name- property name.- Returns:
- property value as a collection of
Strings, or emptyCollection
-
getTrimmedStrings
public String[] getTrimmedStrings(String name)
Get the comma delimited values of thenameproperty as an array ofStrings, 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
Strings, or empty array.
-
getTrimmedStrings
public String[] getTrimmedStrings(String name, String... defaultValue)
Get the comma delimited values of thenameproperty as an array ofStrings, 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
Strings, or default value.
-
setStrings
public void setStrings(String name, String... values)
Set the array of string values for thenameproperty 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 thenameproperty as an array ofClass. The value of the property specifies a list of comma separated class names. If no such property is specified, thendefaultValueis 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 thenameproperty as aClass. If no such property is specified, thendefaultValueis 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 thenameproperty as aClassimplementing the interface specified byxface. If no such property is specified, thendefaultValueis 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 thenameproperty as aListof 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
Listof objects implementingxface.
-
setClass
public void setClass(String name, Class<?> theClass, Class<?> xface)
Set the value of thenameproperty to the name of atheClassimplementing the given interfacexface. An exception is thrown iftheClassdoes 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 theURLfor 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 aReaderattached 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 anIteratorto go through the list ofStringkey-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 givenWriterThe 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 theClassLoaderfor 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-trueto set quiet-mode on,falseto 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:WritableConvert Writable to double. Whether this is supported depends on the specific writable.
-
toFloat
public float toFloat()
Description copied from interface:WritableConvert Writable to float. Whether this is supported depends on the specific writable.
-
toInt
public int toInt()
Description copied from interface:WritableConvert Writable to int. Whether this is supported depends on the specific writable.
-
toLong
public long toLong()
Description copied from interface:WritableConvert Writable to long. Whether this is supported depends on the specific writable.
-
getType
public WritableType getType()
Description copied from interface:WritableGet the type of the writable.
-
writeType
public void writeType(DataOutput out) throws IOException
Description copied from interface:WritableWrite the type (a single short value) to the DataOutput. SeeWritableFactoryfor details.- Specified by:
writeTypein interfaceWritable- Parameters:
out- DataOutput to write to- Throws:
IOException- For errors during writing
-
-