Class PropertyArray

java.lang.Object
com.sun.javatest.util.PropertyArray

public class PropertyArray extends Object
A space-efficient string to string map. This class is similar to java.util.Properties. For this class, space is more important than speed. Use this class when you care much more about wasted space than wasting time doing reference juggling in memory. Arrays in this class must correspond to this format:
 {"key1", "value1", "key2", "value2", ...}
 
  • Constructor Details

    • PropertyArray

      public PropertyArray()
      Create a mutable object.
    • PropertyArray

      public PropertyArray(int initSize)
      Create a mutable object.
      Parameters:
      initSize - the initial capacity of the array
    • PropertyArray

      public PropertyArray(Reader in) throws IOException
      Create a immutable object, from data read from on a stream in the format of a standard Java properties file.
      Parameters:
      in - the stream from which to read the properties
      Throws:
      IOException - if a problem occurred while reading the data
    • PropertyArray

      public PropertyArray(Map<String,String> props)
      Create a immutable PropertyArray object from a standard Properties object.
      Parameters:
      props - the object from which to initialize the array
    • PropertyArray

      public PropertyArray(String... data)
      Create a immutable PropertyArray object from data in a compact array of names and values.
      Parameters:
      data - an array containing pairs of entries: even-numbered entries identify the names of properties, odd-numbered entries give the value for the preceding property name.
  • Method Details

    • getArray

      public static String[] getArray(Map<String,String> props)
      Get a compact array containing the names and values of entries from a standard Properties object.
      Parameters:
      props - the Properties object from which to get the data
      Returns:
      an array containing the names of the properties in even-numbered entries, and the corresponding values in the adjacent odd-numbered entries
    • put

      public static String[] put(String[] data, String key, String value)
      Add a mapping to an array, returning a new array.
      Parameters:
      data - The array to which to the new array is to be added. May be null.
      key - the name of the new value to be added
      value - the new value to be added
      Returns:
      an array with the new element added
      Throws:
      PropertyArray.PropertyArrayError - May be thrown if a null key or value is supplied.
    • get

      public static String get(String[] data, String key)
      Get a named value from the array of properties. If the given data array is null or zero length, null is returned. If the key paramter is null, null will be returned, no error will occur.
      Parameters:
      data - an array containing sequential name value pairs
      key - the name of the property to be returned
      Returns:
      the value of the named entry, or null if not found
    • remove

      public static String[] remove(String[] data, String key)
      Remove an entry from an array of properties.
      Parameters:
      data - an array of sequential name value properties
      key - the name of the entry to be removed
      Returns:
      an array that does not contain the named property
    • getProperties

      public static Map<String,String> getProperties(String... data)
      Get a standard Map object from an array of properties.
      Parameters:
      data - an array of sequential name value properties
      Returns:
      a Map object containing data from the array
    • save

      public static void save(String[] data, Writer out) throws IOException
      Write an array of properties to a stream. The data is written using the format for standard Java property files.
      Parameters:
      data - an array of sequential name value properties
      out - a stream to which to write the data
      Throws:
      IOException - if a problem occurred while writing to the stream
      See Also:
    • load

      public static String[] load(Reader in) throws IOException
      Read an array of properties from an input stream. The data will be read according to the standard format for Java property files.
      Parameters:
      in - the stream from which to read the data
      Returns:
      an array of sequential name value properties
      Throws:
      IOException - if an error occurred while reading the data
      See Also:
    • enumerate

      public static Enumeration<String> enumerate(String... props)
      Enumerate the properties in an array.
      Parameters:
      props - an array of sequential name value properties
      Returns:
      an enumeration of the properties in the array
    • getProperties

      public Map<String,String> getProperties()
      Get the data in this PropertyArray as a standard Properties object.
      Returns:
      a Properties object containing the same data as this PropertyArray
    • isMutable

      public boolean isMutable()
      Check if the property array is mutable.
      Returns:
      true if data can be stored in this array, and false otherwise
    • size

      public int size()
      Get the number of properties stored in the property array.
      Returns:
      the number of properties stored in the property array
    • get

      public String get(String key)
      Get the value of a named property.
      Parameters:
      key - the name of the desired property
      Returns:
      the value of the property, or null if it was not found
    • getArray

      public String[] getArray()
      Get a copy of the data in this PropertyArray.
      Returns:
      a copy of the data, or null if there is no data.
    • put

      public String put(String key, String value)
      Put a property into the PropertyArray.
      Parameters:
      key - the name of the property to be added
      value - the value of the property to be added
      Returns:
      the previous value (if any) of this property
      Throws:
      PropertyArray.PropertyArrayError - if a null key or value is supplied.
    • remove

      public void remove(String key)
      Remove a property.
      Parameters:
      key - the name of the property to be removed
    • save

      public void save(Writer out) throws IOException
      Save the properties to a stream. The data is written using the format for a standard Java properties file.
      Parameters:
      out - the stream to which to write the data
      Throws:
      IOException - if an error occurred while writing the data