us.asciiroth.client
Class Registry

java.lang.Object
  extended by us.asciiroth.client.Registry

public class Registry
extends java.lang.Object

A singleton that ensures our collection of piece instances, which are all immutable, are cached for reuse. Also manages the serialization and deserialization of pieces, since the serialized form of a Piece, also known as its key, is used to cache the pieces for lookup as well. Finally, the serializers themselves contain additional information that is used by the Map Editor to represent and manipulate piece types.

It is essential that pieces are created through the Registry; the game assumes two pieces, of the same type and with the same state, will be the same object instance. It is also required that all piece instances be immutable for this reason. With the exception of the player (who is an agent on the board), and effects (which are not saved and so never serialized), all changes in the game are modeled by swapping piece instances, not by changing piece state.


Field Summary
static java.lang.String FIELD_DELIMITER
          The delimiter used to mark fields in serialized keys.
 
Method Summary
<T extends Piece>
T
cache(T piece)
          Use the provided piece as an example to retrieve the cached version of this piece instance.
static Registry get()
          Get the Registry singleton.
 java.lang.String getEntitySpec()
           
 Piece getExample(java.lang.String type)
          Get an example instance for the given type.
 java.lang.String getKey(Piece piece)
          For the supplied piece, get its "key", its representation or serialization in String form.
 Piece getPiece(java.lang.Class<?> c)
          For the supplied class, create a piece instance.
 Piece getPiece(java.lang.String key)
          For the supplied key, create a piece instance.
 java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> getSerializersByTags()
          Get the templates for all available pieces; used by the editor to create a library of types from which individual instances can be created.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FIELD_DELIMITER

public static final java.lang.String FIELD_DELIMITER
The delimiter used to mark fields in serialized keys. The actual value can be used as-is in a regular expression to split the key.

See Also:
Constant Field Values
Method Detail

cache

public <T extends Piece> T cache(T piece)
Use the provided piece as an example to retrieve the cached version of this piece instance. Any time a piece is created, it should be passed to the registry in this manner and the returned version should be used, in order to ensure that equality holds between all piece instances (e.g. two Triffids with the color Red must be equals == to one another).

Parameters:
piece -
Returns:
the piece instance as it exists in the cache

get

public static Registry get()
Get the Registry singleton.

Returns:
the Registry singleton.

getEntitySpec

public java.lang.String getEntitySpec()

getExample

public Piece getExample(java.lang.String type)
Get an example instance for the given type. These are used by the editor to illustrate the types, since some types can't be rendered without additional information.

Parameters:
type - the type of a piece
Returns:
an instance of that piece for illustration of the type in the editor

getKey

public java.lang.String getKey(Piece piece)
For the supplied piece, get its "key", its representation or serialization in String form. This key can be used to re-create the piece instance, cache it, refer to it in a board, etc. For this method to work, an appropriate Serializer must be created for the piece type and registered with the registry.

Parameters:
piece - the piece to create a key for
Returns:
a String representation of the piece and its state

getPiece

public Piece getPiece(java.lang.Class<?> c)
For the supplied class, create a piece instance. Pieces should be created using the Registry exclusively. The game assumes that two pieces with the same type and the same state, being immutable, are the same object instance. For this method to work, an appropriate Serializer must be created for the piece type and registered with the registry.

Parameters:
c - the class of the piece to be created (must be a type-only serializable piece, otherwise use the string key for the piece).
Returns:
a piece instance

getPiece

public Piece getPiece(java.lang.String key)
For the supplied key, create a piece instance. Pieces should be created using the registry exclusively. The game assumes that two pieces with the same type and the same state, being immutable, are the same object instance. For this method to work, an appropriate Serializer must be created for the piece type and registered with the registry.

Parameters:
key - a String representation of the piece and its state
Returns:
a piece instance

getSerializersByTags

public java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> getSerializersByTags()
Get the templates for all available pieces; used by the editor to create a library of types from which individual instances can be created.

Returns:
a Map of keys and their associated templates

0.7

© 2009 Alx Dark