Homepage

org.tinymarbles.model
Class PObject

java.lang.Object
  extended by org.tinymarbles.model.Persistent<Long>
      extended by org.tinymarbles.model.PNode<PObject>
          extended by org.tinymarbles.model.PObject

public class PObject
extends PNode<PObject>

This class represents a persistent object in the repository.

Author:
duke

Field Summary
 
Fields inherited from class org.tinymarbles.model.PNode
id
 
Constructor Summary
protected PObject()
          Empty constructor for Hibernate
protected PObject(PType type, String systemId)
          Protected constructor for subclasses
 
Method Summary
 boolean add(String name, PObject el)
          Adds a new PObject element to the Set called name.
 boolean add(String name, PObject el, String inverseProperty)
          Adds a new PObject element to the Set called name.
 String describe()
          Builds a description based on the attributes defined for this object.
 boolean equals(Object obj)
           
 void fromMap(Map<String,Object> map)
          Popules this PObject from a Map.
<V> V
get(String name)
          Gets the value of the attribute called name
 PValue getPValue(String name)
          Returns the PValue associated with the attribute called name
 PSet getSet(String name)
          Returns a PSet associated with the attribute called name.
 String getSystemId()
          Get the systemId, a unique identifier for this instance
 PType getType()
          Gets the object's type
protected  Map<String,PValue> getValues()
          Gets the map of values.
 Integer getVersion()
          Gets the persistent version of this type
 boolean hasAttribute(String name)
          Returns true if this object has an attribute with name name.
 int hashCode()
           
 List<PValue> listDirtyValues()
          Gets a list of values which are dirty (i.e.
 boolean remove(String name, PObject el)
          Removes a PObject element from the Set called name.
 boolean remove(String name, PObject el, String inverseProperty)
          Removes a PObject element from the Set called name.
 PObject set(String name, Object value)
          Sets a value to an attribute.
protected  void setDirty()
          Set this instance with a dirty instance.
 void setSystemId(String systemId)
          Sets the systemId of this object.
protected  void setValues(Map<String,PValue> values)
           
 Map<String,Object> toMap()
          Creates a map with keys being the names of its attributes and values being the content of its persistent values.
 String toString()
          Returns a string representation of this instance.
 List<PValue> values()
          Gets an ordered list with all the PValues associated with this instance.
 
Methods inherited from class org.tinymarbles.model.PNode
addChild, containsChild, getChildren, getId, getParent, getPersistentIdentifier, hasAncestor, isPersistent, listChildren, removeChild, setChildren, setId, setParent
 
Methods inherited from class org.tinymarbles.model.Persistent
delete, refresh, save
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PObject

protected PObject()
Empty constructor for Hibernate


PObject

protected PObject(PType type,
                  String systemId)
Protected constructor for subclasses

Parameters:
type - The type that owns this instance
systemId - the object's system ID (can be null)
See Also:
PType.newInstance(String)
Method Detail

getVersion

public Integer getVersion()
Gets the persistent version of this type

Returns:
a version number, if persisted

getSystemId

public String getSystemId()
Get the systemId, a unique identifier for this instance

Returns:
the systemId

setSystemId

public void setSystemId(String systemId)
Sets the systemId of this object. NOTE:

Parameters:
systemId -

getType

public PType getType()
Gets the object's type

Returns:
the object's type

getValues

protected Map<String,PValue> getValues()
Gets the map of values. Used only for persistence.

Returns:
the persistent map of values.
See Also:
values(), get(String), hasAttribute(String)

setValues

protected final void setValues(Map<String,PValue> values)

set

public PObject set(String name,
                   Object value)
            throws UnknownAttributeException
Sets a value to an attribute. value must be compatible with the attribute type, or you will get a ClassCastException.

An exception is thrown if the attribute called name doesn't exist. Returns this instance to allow chaining

Parameters:
name - the name of the attribute.
value - the value to be set.
Returns:
this instance
Throws:
UnknownAttributeException

get

public <V> V get(String name)
Gets the value of the attribute called name

Type Parameters:
V - the type of the value, as determined by the attribute
Parameters:
name - the name of the attribute
Returns:
the value of the attribute or
null if the attribute does not exist

add

public boolean add(String name,
                   PObject el)
Adds a new PObject element to the Set called name. name must be obligatorily the name of an attribute of type Set.

Parameters:
name - the name of the attribute
el - the element to be added
Returns:
true if this set did not already contain the specified element.

add

public boolean add(String name,
                   PObject el,
                   String inverseProperty)
Adds a new PObject element to the Set called name. name must be obligatorily the name of an attribute of type Set.

Parameters:
name - the name of the attribute
el - the element to be added
inverseProperty - the inverse property to be added
Returns:
true if this set did not already contain the specified element.

remove

public boolean remove(String name,
                      PObject el)
Removes a PObject element from the Set called name. name must be obligatorily the name of an attribute of type Set.

Parameters:
name - the name of the attribute
el - the element to be removed
Returns:
true if the set contained the specified element.

remove

public boolean remove(String name,
                      PObject el,
                      String inverseProperty)
Removes a PObject element from the Set called name. name must be obligatorily the name of an attribute of type Set.

Parameters:
name - the name of the attribute
el - the element to be removed
inverseProperty - the inverse property to be removed
Returns:
true if the set contained the specified element.

hasAttribute

public boolean hasAttribute(String name)
Returns true if this object has an attribute with name name. This method gives no guarantee about the value of the attribute, which can be null.

Parameters:
name - the name of the attribute
Returns:
true if the attribute is present

getSet

public PSet getSet(String name)
            throws UnknownAttributeException
Returns a PSet associated with the attribute called name. name must be obligatorily the name of an attribute of type Set.

Parameters:
name -
Returns:
the PSet of the attribute
Throws:
UnknownAttributeException

getPValue

public PValue getPValue(String name)
                 throws UnknownAttributeException
Returns the PValue associated with the attribute called name

Parameters:
name - the name of the attribute
Returns:
the value of the attribute
Throws:
UnknownAttributeException - if attribute doesn't exist

toMap

public Map<String,Object> toMap()
Creates a map with keys being the names of its attributes and values being the content of its persistent values. Thus, the following code is valid:

 Map map = obj.toMap();
 assert map.get("key") == obj.get("key");
 
The map is not backed by the object. Changes to the map affect the map only, but collections are not cloned.

Returns:
map with attribute name and value

fromMap

public void fromMap(Map<String,Object> map)
Popules this PObject from a Map. The object looks for string keys in the map that correspond to its attribute names. The values must be of the type expected by the attribute. Entries in the map that don't correspond to attribute names are ignored.

Parameters:
map - a map of attribute names and values
See Also:
toMap()

describe

public String describe()
Builds a description based on the attributes defined for this object.

Returns:
a formated description string

values

public List<PValue> values()
Gets an ordered list with all the PValues associated with this instance. The list is not immutable and the order reflects the type's ordering of attributes.

Returns:
an unmodifiable list of PValues
See Also:
PType.attributes(), getValues()

toString

public String toString()
Returns a string representation of this instance.

Overrides:
toString in class Object
See Also:
describe()

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

listDirtyValues

public List<PValue> listDirtyValues()
Gets a list of values which are dirty (i.e. not persisted). Used to track values that are new since the object was created or loaded. Override with care.

Returns:
the list of dirty PValues ov this instance. Unlike values(), this method does not return an ordered list.
See Also:
values(), getValues()

setDirty

protected void setDirty()
Set this instance with a dirty instance. Useful when commit is called without call save


Homepage