Package in.dragonbra.javasteam.types
Class KeyValue
- java.lang.Object
-
- in.dragonbra.javasteam.types.KeyValue
-
public class KeyValue extends java.lang.ObjectRepresents a recursive string key to arbitrary value container.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKeyValue.Type
-
Constructor Summary
Constructors Constructor Description KeyValue()Initializes a new instance of theKeyValueclass.KeyValue(java.lang.String name)Initializes a new instance of theKeyValueclass.KeyValue(java.lang.String name, java.lang.String value)Initializes a new instance of theKeyValueclass.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanasBoolean()Attempts to convert and return the value of this instance as a boolean.booleanasBoolean(boolean defaultValue)Attempts to convert and return the value of this instance as a boolean.byteasByte()Attempts to convert and return the value of this instance as a byte.byteasByte(byte defaultValue)Attempts to convert and return the value of this instance as a byte.<T extends java.lang.Enum<T>>
java.util.EnumSet<T>asEnum(java.lang.Class<T> enumClass, java.util.EnumSet<T> defaultValue)Attempts to convert and return the value of this instance as an enum.<T extends java.lang.Enum<T>>
java.util.EnumSet<T>asEnum(java.lang.Class<T> enumClass, T defaultValue)Attempts to convert and return the value of this instance as an enum.floatasFloat()Attempts to convert and return the value of this instance as a float.floatasFloat(float defaultValue)Attempts to convert and return the value of this instance as a float.intasInteger()Attempts to convert and return the value of this instance as an integer.intasInteger(int defaultValue)Attempts to convert and return the value of this instance as an integer.longasLong()Attempts to convert and return the value of this instance as a long.longasLong(long defaultValue)Attempts to convert and return the value of this instance as a long.shortasShort()Attempts to convert and return the value of this instance as a short.shortasShort(short defaultValue)Attempts to convert and return the value of this instance as a short.java.lang.StringasString()Returns the value of this instance as a string.KeyValueget(java.lang.String key)Gets the childKeyValuewith the specified key.java.util.List<KeyValue>getChildren()java.lang.StringgetName()java.lang.StringgetValue()static KeyValueloadAsText(java.lang.String path)Attempts to load the given filename as a textKeyValue.static KeyValueloadFromString(java.lang.String input)Attempts to create an instance ofKeyValuefrom the given input text.booleanreadAsText(java.io.InputStream is)booleanreadFileAsText(java.lang.String filename)Opens and reads the given filename as text.voidsaveToFile(java.io.File path, boolean binary)Saves this instance to file.voidsaveToStream(java.io.OutputStream os, boolean binary)voidset(java.lang.String key, KeyValue value)Sets the childKeyValuewith the specified key.voidsetName(java.lang.String name)voidsetValue(java.lang.String value)java.lang.StringtoString()static KeyValuetryLoadAsBinary(java.lang.String path)Attempts to load the given filename as a binaryKeyValue.booleantryReadAsBinary(java.io.InputStream is)Populate this instance from the givenInputStreamas a binaryKeyValue.
-
-
-
Constructor Detail
-
KeyValue
public KeyValue()
Initializes a new instance of theKeyValueclass.
-
KeyValue
public KeyValue(java.lang.String name)
Initializes a new instance of theKeyValueclass.- Parameters:
name- The optional name of the root key.
-
KeyValue
public KeyValue(java.lang.String name, java.lang.String value)Initializes a new instance of theKeyValueclass.- Parameters:
name- The optional name of the root key.value- The optional value assigned to the root key.
-
-
Method Detail
-
get
public KeyValue get(java.lang.String key)
Gets the childKeyValuewith the specified key. If no child with the given key exists,INVALIDis returned.- Parameters:
key- key- Returns:
- the child
KeyValue
-
set
public void set(java.lang.String key, KeyValue value)Sets the childKeyValuewith the specified key.- Parameters:
key- keyvalue- the childKeyValue
-
asString
public java.lang.String asString()
Returns the value of this instance as a string.- Returns:
- The value of this instance as a string.
-
asByte
public byte asByte(byte defaultValue)
Attempts to convert and return the value of this instance as a byte. If the conversion is invalid, the default value is returned.- Parameters:
defaultValue- The default value to return if the conversion is invalid.- Returns:
- The value of this instance as an unsigned byte.
-
asByte
public byte asByte()
Attempts to convert and return the value of this instance as a byte. If the conversion is invalid, the default value is returned.- Returns:
- The value of this instance as an unsigned byte.
-
asShort
public short asShort(short defaultValue)
Attempts to convert and return the value of this instance as a short. If the conversion is invalid, the default value is returned.- Parameters:
defaultValue- The default value to return if the conversion is invalid.- Returns:
- The value of this instance as an unsigned byte.
-
asShort
public short asShort()
Attempts to convert and return the value of this instance as a short. If the conversion is invalid, the default value is returned.- Returns:
- The value of this instance as an unsigned byte.
-
asInteger
public int asInteger(int defaultValue)
Attempts to convert and return the value of this instance as an integer. If the conversion is invalid, the default value is returned.- Parameters:
defaultValue- The default value to return if the conversion is invalid.- Returns:
- The value of this instance as an unsigned byte.
-
asInteger
public int asInteger()
Attempts to convert and return the value of this instance as an integer. If the conversion is invalid, the default value is returned.- Returns:
- The value of this instance as an unsigned byte.
-
asLong
public long asLong(long defaultValue)
Attempts to convert and return the value of this instance as a long. If the conversion is invalid, the default value is returned.- Parameters:
defaultValue- The default value to return if the conversion is invalid.- Returns:
- The value of this instance as an unsigned byte.
-
asLong
public long asLong()
Attempts to convert and return the value of this instance as a long. If the conversion is invalid, the default value is returned.- Returns:
- The value of this instance as an unsigned byte.
-
asFloat
public float asFloat(float defaultValue)
Attempts to convert and return the value of this instance as a float. If the conversion is invalid, the default value is returned.- Parameters:
defaultValue- The default value to return if the conversion is invalid.- Returns:
- The value of this instance as an unsigned byte.
-
asFloat
public float asFloat()
Attempts to convert and return the value of this instance as a float. If the conversion is invalid, the default value is returned.- Returns:
- The value of this instance as an unsigned byte.
-
asBoolean
public boolean asBoolean(boolean defaultValue)
Attempts to convert and return the value of this instance as a boolean. If the conversion is invalid, the default value is returned.- Parameters:
defaultValue- The default value to return if the conversion is invalid.- Returns:
- The value of this instance as an unsigned byte.
-
asBoolean
public boolean asBoolean()
Attempts to convert and return the value of this instance as a boolean. If the conversion is invalid, the default value is returned.- Returns:
- The value of this instance as an unsigned byte.
-
asEnum
public <T extends java.lang.Enum<T>> java.util.EnumSet<T> asEnum(java.lang.Class<T> enumClass, T defaultValue)Attempts to convert and return the value of this instance as an enum. If the conversion is invalid, the default value is returned.- Type Parameters:
T- the type of the enum to convert to- Parameters:
enumClass- the type of the enum to convert todefaultValue- The default value to return if the conversion is invalid.- Returns:
- The value of this instance as an unsigned byte.
-
asEnum
public <T extends java.lang.Enum<T>> java.util.EnumSet<T> asEnum(java.lang.Class<T> enumClass, java.util.EnumSet<T> defaultValue)Attempts to convert and return the value of this instance as an enum. If the conversion is invalid, the default value is returned.- Type Parameters:
T- the type of the enum to convert to- Parameters:
enumClass- the type of the enum to convert todefaultValue- The default value to return if the conversion is invalid.- Returns:
- The value of this instance as an unsigned byte.
-
getName
public java.lang.String getName()
-
setName
public void setName(java.lang.String name)
-
getValue
public java.lang.String getValue()
-
setValue
public void setValue(java.lang.String value)
-
getChildren
public java.util.List<KeyValue> getChildren()
-
readAsText
public boolean readAsText(java.io.InputStream is) throws java.io.IOException- Throws:
java.io.IOException
-
readFileAsText
public boolean readFileAsText(java.lang.String filename) throws java.io.IOExceptionOpens and reads the given filename as text.- Parameters:
filename- The file to open and read.- Returns:
- true if the read was successful; otherwise, false.
- Throws:
java.io.IOException- exception while reading from the file
-
loadAsText
public static KeyValue loadAsText(java.lang.String path)
Attempts to load the given filename as a textKeyValue.- Parameters:
path- The path to the file to load.- Returns:
- a
KeyValueinstance if the load was successful, or null on failure.
-
tryLoadAsBinary
public static KeyValue tryLoadAsBinary(java.lang.String path)
Attempts to load the given filename as a binaryKeyValue.- Parameters:
path- The path to the file to load.- Returns:
- a
KeyValueinstance if the load was successful, or null on failure.
-
loadFromString
public static KeyValue loadFromString(java.lang.String input)
Attempts to create an instance ofKeyValuefrom the given input text.- Parameters:
input- The input text to load.- Returns:
- a
KeyValueinstance if the load was successful, or null on failure.
-
saveToFile
public void saveToFile(java.io.File path, boolean binary) throws java.io.IOExceptionSaves this instance to file.- Parameters:
path- The file path to save to.binary- If set to true, saves this instance as binary.- Throws:
java.io.IOException- exception while writing to the file
-
saveToStream
public void saveToStream(java.io.OutputStream os, boolean binary) throws java.io.IOException- Throws:
java.io.IOException
-
tryReadAsBinary
public boolean tryReadAsBinary(java.io.InputStream is) throws java.io.IOExceptionPopulate this instance from the givenInputStreamas a binaryKeyValue.- Parameters:
is- The inputInputStreamto read from.- Returns:
- true if the read was successful; otherwise, false.
- Throws:
java.io.IOException- exception while reading from the stream
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-