org.argouml.cognitive
Class ItemUID

java.lang.Object
  extended byorg.argouml.cognitive.ItemUID

public class ItemUID
extends Object

An instances of this class is supposed to be attached to an instance of another class to uniquely identify it. It is intended that such a tagging should be persistent over saving and loading, if applicable.

The class also harbors the getIDOfObject(Object, boolean) which provides a way to get the ItemUID of any object with a method ItemUID getItemUID() and creating new ItemUIDs for any object with a method setItemUID(ItemUID) using reflection in java.

A class intended to be tagged must at least provide a ItemUID getItemUID() method. It may also provide a void setItemUID(ItemUID id) such that getItemUID() will return id if a call returns successfully, and which is stored persistently should the tagged object be stored. This allows this class to automatically tag an object when necessary, but it is allowed to tag classes by other means and only provide the getItemUID() call.

A critical requirement for this class is that the cognitive component is supposed to work with general objects. This class is a wrapper around places where the component needs persistent identities of objects, since I have said that some features cannot be implemented without that, such as ResolvedCritic, and so far noone has shown me wrong (though I wouldn't mind). It is for this reason that some perhaps ugly looking exceptions in this code must be considered perfectly normal conditions. Failure of some object to work with tagging must be handled by the cognitive component programmer and it is (see eg ResolvedCritic).

A possible future change would be to allow tag handlers to be registered with this class to handle other preexisting tagging mechanisms, which could be used to remove the dependancy to the model component here, which I find a bit unaesthetic. So far, not enough to write it (though it is not much work).


Constructor Summary
ItemUID()
          Constructs a new ItemUID and creates a new ID for it.
ItemUID(String param)
          Constructs a new ItemUID and uses the String param as the ID.
 
Method Summary
protected static String createObjectID(Object obj)
          Tries to create a new ID for the object.
static String generateID()
          Generates a new unique ID and returns it as a String.
static String getIDOfObject(Object obj, boolean canCreate)
          Obtains the ID of an object and returns it as a String.
protected static String readObjectID(Object obj)
          Tries to read the ID of the object.
 String toString()
          Returns the ID of this ItemUID as a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ItemUID

public ItemUID()
Constructs a new ItemUID and creates a new ID for it.


ItemUID

public ItemUID(String param)
Constructs a new ItemUID and uses the String param as the ID. Mainly intended to be used during loading of saved objects.

Parameters:
param - The ID to used for the new instance.
See Also:
toString()
Method Detail

toString

public String toString()
Returns the ID of this ItemUID as a String. If everything works all such Strings will be unique. It is possible to created a new identical ItemUID using this String.

Returns:
The ID as a String.
See Also:
ItemUID(String)

generateID

public static String generateID()
Generates a new unique ID and returns it as a String. The contents of the String is supposed to be unique with respect to all Strings generated by other instances of this class.

Returns:
A String with unique content.

getIDOfObject

public static String getIDOfObject(Object obj,
                                   boolean canCreate)
Obtains the ID of an object and returns it as a String. If canCreate is true it will try to create a new ID for the object if it has none.

Parameters:
obj - the Object to get the ID of.
canCreate - If an ID can be created, should object not have one.
Returns:
The ID of the object, or null.

readObjectID

protected static String readObjectID(Object obj)
Tries to read the ID of the object. It uses the reflective properties of java to access a method named getItemUID of the object which is expected to return an ItemUID.

Parameters:
obj - The object whose ID to read.
Returns:
The ID of the object, or null.

createObjectID

protected static String createObjectID(Object obj)
Tries to create a new ID for the object. It uses the reflective properties of java to access a method named setItemUID(ItemUID). If that method exist and doesn't throw when called, then the call is assumed to have been successful and the object is responsible for remembering the ID.

Parameters:
obj - The object to assign a new ID.
Returns:
The new ID of the object, or null.


ArgoUML © 1996-2006ArgoUML HomepageArgoUML Cookbook