Class Encoding

java.lang.Object
edu.uky.cs.nil.tt.world.Encoding
All Implemented Interfaces:
Named

public abstract class Encoding extends Object implements Named
An encoding is a method for translating data types to and from Java Strings so they can serialized, saved to file, and transmitted between the server and its agents. Encodings are primarily used to encode the values of variables in a state (see Variable.encoding).

An encoded object is one whose value can be encoded.

Author:
Stephen G. Ware
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Encoding
    An encoding for Boolean true and false values
    final String
    The unique name of this encoding method
    static final Encoding
    And encoding for session Role values
    static final Encoding
    And encoding for Turn.Type values
    static final String
    The prefixed used by all unsigned integer encodings
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract Object
    decode(Object value)
    Returns the object that produced the given encoding, or converts an easily recognizable object into the kind of object this encoding uses.
    abstract String
    encode(Object value)
    Returns a string of 1's and 0's that uniquely represents this object among other objects of the same type in the same story world.
    static Encoding
    get(String name)
    Returns the encoding method with a given name.
    Returns this object's unique name that distinguishes it from other objects of the same type in the same context.
     

    Methods inherited from class Object

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

    • UNSIGNED_INTEGER

      public static final String UNSIGNED_INTEGER
      The prefixed used by all unsigned integer encodings
      See Also:
    • BOOLEAN

      public static final Encoding BOOLEAN
      An encoding for Boolean true and false values
    • ROLE

      public static final Encoding ROLE
      And encoding for session Role values
    • TURN_TYPE

      public static final Encoding TURN_TYPE
      And encoding for Turn.Type values
    • name

      public final String name
      The unique name of this encoding method
  • Method Details

    • get

      public static Encoding get(String name)
      Returns the encoding method with a given name.
      Parameters:
      name - the name of the desired encoding
      Returns:
      the encoding objects with the given name
      Throws:
      IllegalArgumentException - if the given names does not match a known encoding
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getName

      public String getName()
      Description copied from interface: Named
      Returns this object's unique name that distinguishes it from other objects of the same type in the same context.
      Specified by:
      getName in interface Named
      Returns:
      the object's unique name
    • encode

      public abstract String encode(Object value)
      Returns a string of 1's and 0's that uniquely represents this object among other objects of the same type in the same story world.

      Encodings are returned as a string, rather than a number, to make it clear how many bits are required to represent other objects of the same type in the same story world. For example, Entity is an encoded object. A story world with 3 entities would require 2 bits to represent an etity (00 for null, 01 for the first entity, 10 for the second entity, and 11 for the third entity). All entities in that story world should return a string of length 2, even if they must be padded with 0's, to make it clear how many bits are used in the encoding.

      Parameters:
      value - the object to be encoded
      Returns:
      a string of 1's and 0's that uniquely represents this object
      Throws:
      IllegalArgumentException - if the given object cannot be encoded using this method
    • decode

      public abstract Object decode(Object value)
      Returns the object that produced the given encoding, or converts an easily recognizable object into the kind of object this encoding uses. This method can be used as the reverse of encode(Object), but it allows arbitrary objects as input in case other types of objects are already the right type or can be converted into the right type. This method can be used both as a decoder and also as a means of checking that an already decoded value is correctly formatted.

      For example, if this encoding represent an unsigned integer with a certain number of bits, this method can be used to decode a string of 1's and 0's into the original integer; however, if it is passed a number object, this method can check that the number is of the correct format (i.e. that it is an integer and that it is small enough that it could be encoded using this encoding's number of bits).

      Parameters:
      value - an encoding string or a decided object which needs to be checked for correctness
      Returns:
      the object that produced the encoding, or the original object (or something eqivalent) if this method is being used to check the correctness of a decoded object
      Throws:
      IllegalArgumentException - if the given object cannot be decoded or is not properly formatted