Class Dictionary

Object
Dictionary

public class Dictionary extends Object
Collection of parameters represented by key-value pairs.

Values are always strings.

Author:
Owen Feehan
  • Constructor Details

    • Dictionary

      public Dictionary()
      Creates empty, with no parameters.
  • Method Details

    • readFromFile

      public static Dictionary readFromFile(Path path) throws IOException
      Reads parameters from a Java properties file.
      Parameters:
      path - the path where the properties file is located.
      Returns:
      a newly created dictionary
      Throws:
      IOException - if the path doesn't exist, is in the correct format, or otherwise cannot be read.
    • duplicate

      public Dictionary duplicate()
      Deep-copy of existing dictionary.
      Returns:
      a newly created dictionary, containing identical key-values.
    • getAsString

      public Optional<String> getAsString(String key)
      Retrieves a value from the dictionary as a String.
      Parameters:
      key - the key to retrieve
      Returns:
      the value if key exists, otherwise Optional.empty().
    • getAsDouble

      public double getAsDouble(String key)
      Retrieves a value from the dictionary, and converts to a double.
      Parameters:
      key - the key to retrieve
      Returns:
      the value if key exists, otherwise Double.NaN.
    • put

      public void put(String key, String value)
      Inserts a key-value pair.

      If the key already exists, its value is replaced.

      Parameters:
      key - the key to insert
      value - the value corresponding to key
    • put

      public void put(String key, double value)
      Inserts a key-value pair, after converting value to a String.
      Parameters:
      key - the key to insert
      value - a floating-point value corresponding to key
    • putCheck

      public void putCheck(String key, String value) throws OperationFailedException
      Inserts a key/value pair, checking that the key doesn't already exist.
      Parameters:
      key - the key to insert
      value - a value corresponding to key
      Throws:
      OperationFailedException - if key already exists in the dictionary.
    • putCheck

      public void putCheck(String key, double value) throws OperationFailedException
      Inserts a key/value pair, checking that the key doesn't already exist.
      Parameters:
      key - the key to insert
      value - a floating-point value corresponding to key
      Throws:
      OperationFailedException - if key already exists in the dictionary.
    • putCheck

      public void putCheck(Dictionary dictionary) throws OperationFailedException
      Inserts all key/value pairs from another dictionary, checking that no key already exists.
      Parameters:
      dictionary - the dictionary to insert all key/value pairs from
      Throws:
      OperationFailedException - if key already exists in the dictionary.
    • keys

      public Set<String> keys()
      All keys existing in the dictionary.

      The set is not backed by the dictionary. Changes to the set have no impact on the contents of the dictionary.

      Returns:
      a newly created set of all keys in the dictionary.
    • containsKey

      public boolean containsKey(String key)
      Does a parameter exist with a particular key?
      Parameters:
      key - the key
      Returns:
      true iff the parameter exists.
    • writeToFile

      public void writeToFile(Path path) throws IOException
      Serializes the key-value parameters to a file.
      Parameters:
      path - path to write to.
      Throws:
      IOException - if something goes wrong.