Class KeyValue


  • public class KeyValue
    extends java.lang.Object
    Represents a recursive string key to arbitrary value container.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  KeyValue.Type  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static KeyValue INVALID
      Represents an invalid KeyValue given when a searched for child does not exist.
    • Constructor Summary

      Constructors 
      Constructor Description
      KeyValue()
      Initializes a new instance of the KeyValue class.
      KeyValue​(java.lang.String name)
      Initializes a new instance of the KeyValue class.
      KeyValue​(java.lang.String name, java.lang.String value)
      Initializes a new instance of the KeyValue class.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean asBoolean()
      Attempts to convert and return the value of this instance as a boolean.
      boolean asBoolean​(boolean defaultValue)
      Attempts to convert and return the value of this instance as a boolean.
      byte asByte()
      Attempts to convert and return the value of this instance as a byte.
      byte asByte​(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.
      float asFloat()
      Attempts to convert and return the value of this instance as a float.
      float asFloat​(float defaultValue)
      Attempts to convert and return the value of this instance as a float.
      int asInteger()
      Attempts to convert and return the value of this instance as an integer.
      int asInteger​(int defaultValue)
      Attempts to convert and return the value of this instance as an integer.
      long asLong()
      Attempts to convert and return the value of this instance as a long.
      long asLong​(long defaultValue)
      Attempts to convert and return the value of this instance as a long.
      short asShort()
      Attempts to convert and return the value of this instance as a short.
      short asShort​(short defaultValue)
      Attempts to convert and return the value of this instance as a short.
      java.lang.String asString()
      Returns the value of this instance as a string.
      KeyValue get​(java.lang.String key)
      Gets the child KeyValue with the specified key.
      java.util.List<KeyValue> getChildren()  
      java.lang.String getName()  
      java.lang.String getValue()  
      static KeyValue loadAsText​(java.lang.String path)
      Attempts to load the given filename as a text KeyValue.
      static KeyValue loadFromString​(java.lang.String input)
      Attempts to create an instance of KeyValue from the given input text.
      boolean readAsText​(java.io.InputStream is)  
      boolean readFileAsText​(java.lang.String filename)
      Opens and reads the given filename as text.
      void saveToFile​(java.io.File path, boolean binary)
      Saves this instance to file.
      void saveToStream​(java.io.OutputStream os, boolean binary)  
      void set​(java.lang.String key, KeyValue value)
      Sets the child KeyValue with the specified key.
      void setName​(java.lang.String name)  
      void setValue​(java.lang.String value)  
      java.lang.String toString()  
      static KeyValue tryLoadAsBinary​(java.lang.String path)
      Attempts to load the given filename as a binary KeyValue.
      boolean tryReadAsBinary​(java.io.InputStream is)
      Populate this instance from the given InputStream as a binary KeyValue.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • INVALID

        public static final KeyValue INVALID
        Represents an invalid KeyValue given when a searched for child does not exist.
    • Constructor Detail

      • KeyValue

        public KeyValue()
        Initializes a new instance of the KeyValue class.
      • KeyValue

        public KeyValue​(java.lang.String name)
        Initializes a new instance of the KeyValue class.
        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 the KeyValue class.
        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 child KeyValue with the specified key. If no child with the given key exists, INVALID is returned.
        Parameters:
        key - key
        Returns:
        the child KeyValue
      • set

        public void set​(java.lang.String key,
                        KeyValue value)
        Sets the child KeyValue with the specified key.
        Parameters:
        key - key
        value - the child KeyValue
      • 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 to
        defaultValue - 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 to
        defaultValue - 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.IOException
        Opens 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 text KeyValue.
        Parameters:
        path - The path to the file to load.
        Returns:
        a KeyValue instance 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 binary KeyValue.
        Parameters:
        path - The path to the file to load.
        Returns:
        a KeyValue instance if the load was successful, or null on failure.
      • loadFromString

        public static KeyValue loadFromString​(java.lang.String input)
        Attempts to create an instance of KeyValue from the given input text.
        Parameters:
        input - The input text to load.
        Returns:
        a KeyValue instance if the load was successful, or null on failure.
      • saveToFile

        public void saveToFile​(java.io.File path,
                               boolean binary)
                        throws java.io.IOException
        Saves 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.IOException
        Populate this instance from the given InputStream as a binary KeyValue.
        Parameters:
        is - The input InputStream to 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:
        toString in class java.lang.Object