Class SaveState

  • Direct Known Subclasses:
    PreferenceState

    public class SaveState
    extends java.lang.Object
    Class for saving values in "serializable safe" way. Classes that want to be able to save their state can do so using the SaveState object. The idea is that each state variable in the class is first saved into a SaveState object via a String key. Then the SaveState object is written out to an XML element. When the save state object is to be restored, the saveState object is constructed with an XML Element that contains all of the name/value pairs. Since the "get" methods require a default value, the object that is recovering its state variables will be successfully initialized even if the given key,value pair is not found in the SaveState object.

    Note: Names for options are assumed to be unique. When a putXXX() method is called, if a value already exists for a name, it will be overwritten.

    • Constructor Summary

      Constructors 
      Constructor Description
      SaveState()
      Default Constructor for SaveState; uses "SAVE_STATE" as the name of the state.
      SaveState​(java.io.File file)
      Construct a SaveState from a file containing XML from a previously saved SaveState.
      SaveState​(java.lang.String name)
      Creates a new saveState object.
      SaveState​(org.jdom.Element root)
      Construct a new SaveState object using the given XML element.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clear all objects from the save state.
      protected org.jdom.Element createElementFromElement​(java.lang.String internalKey, org.jdom.Element internalElement)  
      boolean getBoolean​(java.lang.String name, boolean defaultValue)
      Gets the boolean value for the given name.
      boolean[] getBooleans​(java.lang.String name, boolean[] defaultValue)
      Gets the boolean array for the given name.
      byte getByte​(java.lang.String name, byte defaultValue)
      Gets the byte value for the given name.
      byte[] getBytes​(java.lang.String name, byte[] defaultValue)
      Gets the byte array for the given name.
      double getDouble​(java.lang.String name, double defaultValue)
      Gets the double value for the given name.
      double[] getDoubles​(java.lang.String name, double[] defaultValue)
      Gets the double array for the given name.
      <T extends java.lang.Enum<T>>
      T
      getEnum​(java.lang.String name, T defaultValue)
      Gets the Enum value for the given name.
      float getFloat​(java.lang.String name, float defaultValue)
      Gets the float value for the given name.
      float[] getFloats​(java.lang.String name, float[] defaultValue)
      Gets the float array for the given name.
      int getInt​(java.lang.String name, int defaultValue)
      Gets the int value for the given name.
      int[] getInts​(java.lang.String name, int[] defaultValue)
      Gets the int array for the given name.
      long getLong​(java.lang.String name, long defaultValue)
      Gets the long value for the given name.
      long[] getLongs​(java.lang.String name, long[] defaultValue)
      Gets the long array for the given name.
      java.lang.String[] getNames()
      Return the names of the objects saved in the state.
      short getShort​(java.lang.String name, short defaultValue)
      Gets the short value for the given name.
      short[] getShorts​(java.lang.String name, short[] defaultValue)
      Gets the short array for the given name.
      java.lang.String getString​(java.lang.String name, java.lang.String defaultValue)
      Gets the String value for the given name.
      java.lang.String[] getStrings​(java.lang.String name, java.lang.String[] defaultValue)
      Gets the String array for the given name.
      org.jdom.Element getXmlElement​(java.lang.String name)
      Returns the root of an XML sub-tree associated with the given name.
      boolean hasValue​(java.lang.String name)
      Returns true if the SaveState object has a value for the given name.
      boolean isEmpty()
      Return whether anything was added to this save state.
      void putBoolean​(java.lang.String name, boolean value)
      Associates a boolean value with the given name.
      void putBooleans​(java.lang.String name, boolean[] value)
      Associates a boolean array with the given name.
      void putByte​(java.lang.String name, byte value)
      Associates a byte value with the given name.
      void putBytes​(java.lang.String name, byte[] value)
      Associates a byte array with the given name.
      void putDouble​(java.lang.String name, double value)
      Associates a double value with the given name.
      void putDoubles​(java.lang.String name, double[] value)
      Associates a double value with the given name.
      void putEnum​(java.lang.String name, java.lang.Enum<?> value)
      Associates an Enum with the given name.
      void putFloat​(java.lang.String name, float value)
      Associates a float value with the given name.
      void putFloats​(java.lang.String name, float[] value)
      Associates a float array with the given name.
      void putInt​(java.lang.String name, int value)
      Associates an integer value with the given name.
      void putInts​(java.lang.String name, int[] value)
      Associates an integer array with the given name.
      void putLong​(java.lang.String name, long value)
      Associates a long value with the given name.
      void putLongs​(java.lang.String name, long[] value)
      Associates a long array with the given name.
      void putShort​(java.lang.String name, short value)
      Associates a short value with the given name.
      void putShorts​(java.lang.String name, short[] value)
      Associates a short array with the given name.
      void putString​(java.lang.String name, java.lang.String value)
      Associates a String value with the given name.
      void putStrings​(java.lang.String name, java.lang.String[] value)
      Associates a String array with the given name.
      void putXmlElement​(java.lang.String name, org.jdom.Element element)
      Adds an XML element to the saved state object.
      void remove​(java.lang.String name)
      Remove the object identified by the given name.
      void saveToFile​(java.io.File file)
      Write the saveState to a file as XML
      org.jdom.Element saveToXml()
      Save this object to an XML element.
      org.jdom.Element saveToXml​(java.util.Set<java.lang.String> restrictedSet)
      Save this object to an XML element.
      int size()
      Return the number of objects in the save state.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SaveState

        public SaveState​(java.lang.String name)
        Creates a new saveState object.
        Parameters:
        name - of the state
      • SaveState

        public SaveState()
        Default Constructor for SaveState; uses "SAVE_STATE" as the name of the state.
        See Also:
        Object()
      • SaveState

        public SaveState​(java.io.File file)
                  throws java.io.IOException
        Construct a SaveState from a file containing XML from a previously saved SaveState.
        Parameters:
        file - the file containing the XML to read.
        Throws:
        java.io.IOException - if the file can't be read or is not formatted properly for a SaveState
      • SaveState

        public SaveState​(org.jdom.Element root)
        Construct a new SaveState object using the given XML element.
        Parameters:
        root - XML contents of the save state
    • Method Detail

      • saveToXml

        public org.jdom.Element saveToXml()
        Save this object to an XML element.
        Returns:
        Element XML element containing the state
      • saveToFile

        public void saveToFile​(java.io.File file)
                        throws java.io.FileNotFoundException,
                               java.io.IOException
        Write the saveState to a file as XML
        Parameters:
        file - the file to write to.
        Throws:
        java.io.FileNotFoundException - if the file does not represent a valid file path.
        java.io.IOException - if the file could not be written
      • saveToXml

        public org.jdom.Element saveToXml​(java.util.Set<java.lang.String> restrictedSet)
        Save this object to an XML element. Restrict child elements to the set specified.
        Parameters:
        restrictedSet - restricted set of element names or null for all names
        Returns:
        Element XML element containing the state
      • createElementFromElement

        protected org.jdom.Element createElementFromElement​(java.lang.String internalKey,
                                                            org.jdom.Element internalElement)
      • isEmpty

        public boolean isEmpty()
        Return whether anything was added to this save state.
      • remove

        public void remove​(java.lang.String name)
        Remove the object identified by the given name.
      • clear

        public void clear()
        Clear all objects from the save state.
      • size

        public int size()
        Return the number of objects in the save state.
      • getNames

        public java.lang.String[] getNames()
        Return the names of the objects saved in the state.
        Returns:
        String[] array will be zero length if the save state is empty
      • putInt

        public void putInt​(java.lang.String name,
                           int value)
        Associates an integer value with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putByte

        public void putByte​(java.lang.String name,
                            byte value)
        Associates a byte value with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putShort

        public void putShort​(java.lang.String name,
                             short value)
        Associates a short value with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putLong

        public void putLong​(java.lang.String name,
                            long value)
        Associates a long value with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putString

        public void putString​(java.lang.String name,
                              java.lang.String value)
        Associates a String value with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putBoolean

        public void putBoolean​(java.lang.String name,
                               boolean value)
        Associates a boolean value with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putFloat

        public void putFloat​(java.lang.String name,
                             float value)
        Associates a float value with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putDouble

        public void putDouble​(java.lang.String name,
                              double value)
        Associates a double value with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • getInt

        public int getInt​(java.lang.String name,
                          int defaultValue)
        Gets the int value for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the int value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getByte

        public byte getByte​(java.lang.String name,
                            byte defaultValue)
        Gets the byte value for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the byte value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getShort

        public short getShort​(java.lang.String name,
                              short defaultValue)
        Gets the short value for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the short value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getLong

        public long getLong​(java.lang.String name,
                            long defaultValue)
        Gets the long value for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the long value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getBoolean

        public boolean getBoolean​(java.lang.String name,
                                  boolean defaultValue)
        Gets the boolean value for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the boolean value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getString

        public java.lang.String getString​(java.lang.String name,
                                          java.lang.String defaultValue)
        Gets the String value for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the String value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getFloat

        public float getFloat​(java.lang.String name,
                              float defaultValue)
        Gets the float value for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the float value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getDouble

        public double getDouble​(java.lang.String name,
                                double defaultValue)
        Gets the double value for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the double value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • putInts

        public void putInts​(java.lang.String name,
                            int[] value)
        Associates an integer array with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putBytes

        public void putBytes​(java.lang.String name,
                             byte[] value)
        Associates a byte array with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putShorts

        public void putShorts​(java.lang.String name,
                              short[] value)
        Associates a short array with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putLongs

        public void putLongs​(java.lang.String name,
                             long[] value)
        Associates a long array with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putStrings

        public void putStrings​(java.lang.String name,
                               java.lang.String[] value)
        Associates a String array with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putEnum

        public void putEnum​(java.lang.String name,
                            java.lang.Enum<?> value)
        Associates an Enum with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The Enum value in the name,value pair.
      • putBooleans

        public void putBooleans​(java.lang.String name,
                                boolean[] value)
        Associates a boolean array with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putFloats

        public void putFloats​(java.lang.String name,
                              float[] value)
        Associates a float array with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • putDoubles

        public void putDoubles​(java.lang.String name,
                               double[] value)
        Associates a double value with the given name.
        Parameters:
        name - The name in the name,value pair.
        value - The value in the name,value pair.
      • getInts

        public int[] getInts​(java.lang.String name,
                             int[] defaultValue)
        Gets the int array for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the int array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getBytes

        public byte[] getBytes​(java.lang.String name,
                               byte[] defaultValue)
        Gets the byte array for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the byte array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getShorts

        public short[] getShorts​(java.lang.String name,
                                 short[] defaultValue)
        Gets the short array for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the short array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getLongs

        public long[] getLongs​(java.lang.String name,
                               long[] defaultValue)
        Gets the long array for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the long array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getBooleans

        public boolean[] getBooleans​(java.lang.String name,
                                     boolean[] defaultValue)
        Gets the boolean array for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the boolean array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getStrings

        public java.lang.String[] getStrings​(java.lang.String name,
                                             java.lang.String[] defaultValue)
        Gets the String array for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the String array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getEnum

        public <T extends java.lang.Enum<T>> T getEnum​(java.lang.String name,
                                                       T defaultValue)
        Gets the Enum value for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default Enum value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the Enum value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getFloats

        public float[] getFloats​(java.lang.String name,
                                 float[] defaultValue)
        Gets the float array for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the float array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • getDoubles

        public double[] getDoubles​(java.lang.String name,
                                   double[] defaultValue)
        Gets the double array for the given name.
        Parameters:
        name - the name of the pair.
        defaultValue - the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.
        Returns:
        the double array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
      • hasValue

        public boolean hasValue​(java.lang.String name)
        Returns true if the SaveState object has a value for the given name.
      • putXmlElement

        public void putXmlElement​(java.lang.String name,
                                  org.jdom.Element element)
        Adds an XML element to the saved state object. Used by plugins that have more complicated state information that needs to be saved.
        Parameters:
        name - the name to associate with the element
        element - XML element which is the root of an XML sub-tree.
      • getXmlElement

        public org.jdom.Element getXmlElement​(java.lang.String name)
        Returns the root of an XML sub-tree associated with the given name.
        Parameters:
        name - The name associated with the desired Element.
        Returns:
        The root of an XML sub-tree associated with the given name.