org.jutil.relation
Class Reference

java.lang.Object
  |
  +--org.jutil.relation.Relation
        |
        +--org.jutil.relation.Reference

public class Reference
extends Relation

A class of Relation components for implementing a binding in which the object of the Reference has a relation with only 1 other object.

In UML this class is used for implementing multiplicity 1:

In Java, you get the following situation.

Note that the question mark is represented by a Relation object since we don't know its multiplicity. The double binding between the Reference object and A is made by passing this to the constructor of Reference.

This is actually a lightweight version of the APElement class. of the Beedra framework of Jan Dockx.

This class is typically using in the following way.


public class B {
  public B() {
    _a = new Reference(this);
  }
  public A getA() {
    return (A)_a.getOtherEnd();
  }
  public void setA(A a) {
    _a.connectTo(a == null ? null : a.getBLink());
  }
  Reference getALink() {
    return _a;
  }
  private Reference _a;
}
 

The other class must have a method getBLink(), which returns a Relation object that represents the other side of the bi-directional binding. In case the two classes are not in the same package that means that getBLink() must be public there, but that is also the case when not working with these components. Note that if the binding should not be mutable from class B the setA() may be removed. Similarly, getALink() may be removed if the binding is not mutable from class A.


Class Specifications
public invariant getOtherRelation() != null ==> getOtherRelation().contains(this);

Field Summary
static java.lang.String CVS_REVISION
           
 
Constructor Summary
Reference(java.lang.Object object)
          Initialize a new Reference for the given object.
Reference(java.lang.Object object, Relation other)
          Initialize a new Reference for the given object, connected to the given Relation.
 
Method Summary
 void connectTo(Relation other)
          Set the other side of this binding.
 java.lang.Object getOtherEnd()
          Return the Object at the other end of this double binding.
 java.util.List getOtherEnds()
           
 Relation getOtherRelation()
          return the Relation this Reference belongs to
 java.util.List getOtherRelations()
          See superclass.
protected  boolean isValidElement(Relation relation)
           
protected  void register(Relation other)
          See superclass
 boolean registered(java.util.List oldConnections, Relation registered)
           
protected  void unregister(Relation other)
          See superclass
 boolean unregistered(java.util.List oldConnections, Relation unregistered)
           
 
Methods inherited from class org.jutil.relation.Relation
contains, equals, getObject
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CVS_REVISION

public static final java.lang.String CVS_REVISION
Constructor Detail

Reference

public Reference(java.lang.Object object)
Initialize a new Reference for the given object. The new Reference will be unconnected.

Parameters:
object - The object at this side of the binding.

Specifications:
public behavior
requires object != null;
ensures getObject() == object;
ensures getOtherRelation() == null;

Reference

public Reference(java.lang.Object object,
                 Relation other)
Initialize a new Reference for the given object, connected to the given Relation.

Parameters:
object - The object at this side of the binding.
other - The Relation component at the other side of the binding.

Specifications:
public behavior
requires object != null;
ensures getObject() == object;
ensures getOtherRelation() == other;
ensures other != null ==> other.contains(this);
ensures other.registered(new ArrayList(),this);
Method Detail

getOtherEnd

public java.lang.Object getOtherEnd()
Return the Object at the other end of this double binding.

Specifications:
public behavior
ensures getOtherRelation() == null ==> \result == null;
ensures getOtherRelation() != null ==> \result == getOtherRelation().getObject();

getOtherEnds

public java.util.List getOtherEnds()
Specifications:
     also
public behavior
ensures \result != null;
ensures getOtherEnd() != null ==> (\result .size() == 1&&\result .contains(getOtherEnd()));
ensures getOtherEnd() == null ==> \result .isEmpty();

Specifications inherited from overridden method in class Relation:
public behavior
ensures \result != null;
ensures \result .size() == getOtherRelations().size();
ensures ( \forall Object o; \result .contains(o); ( \exists Relation r; getOtherRelations().contains(r); r.getObject() == o));

connectTo

public void connectTo(Relation other)
Set the other side of this binding.

Parameters:
other - The new Relation this Reference will be connected to.

Specifications:
public behavior
ensures registered(\old(getOtherRelations()),other);
ensures other != null ==> other.registered(\old(other.getOtherRelations()),this);

registered

public boolean registered(java.util.List oldConnections,
                          Relation registered)
Specifications:
     also
public behavior
ensures \result == (registered != null ==> contains(registered))&&(((oldConnections.size() == 0))||((oldConnections.size() == 1)&&((oldConnections.get(1) == registered)||((!contains((Relation)oldConnections.get(1)))))));

Specifications inherited from overridden method in class Relation:
public behavior
requires oldConnections != null;
requires !oldConnections.contains(null);
requires ( \forall Object o; oldConnections.contains(o); o instanceof Relation);
ensures !contains(registered) ==> \result == false;
ensures !( \forall Relation r; r != registered; oldConnections.contains(r) == contains(r)) ==> \result == false;

unregistered

public boolean unregistered(java.util.List oldConnections,
                            Relation unregistered)
Specifications:
     also
public behavior
ensures \result == (getOtherRelations().isEmpty())&&(((oldConnections.size() == 1)&&(oldConnections.get(1) == unregistered))||(oldConnections.size() == 0));

Specifications inherited from overridden method in class Relation:
public behavior
requires oldConnections != null;
requires !oldConnections.contains(null);
requires ( \forall Object o; oldConnections.contains(o); o instanceof Relation);
ensures contains(unregistered) ==> \result == false;
ensures !oldConnections.contains(unregistered) ==> \result == false;
ensures !( \forall Relation r; r != unregistered; oldConnections.contains(r) == contains(r)) ==> \result == false;

isValidElement

protected boolean isValidElement(Relation relation)
Specifications:
     also
public behavior
ensures \result == true;

Specifications inherited from overridden method in class Relation:
public behavior
ensures \result == true|\result == false;

unregister

protected void unregister(Relation other)
See superclass
Specifications inherited from overridden method in class Relation:
protected behavior
requires contains(other);
ensures unregistered(\old(getOtherRelations()),other);

register

protected void register(Relation other)
See superclass
Specifications inherited from overridden method in class Relation:
protected behavior
requires isValidElement(other);
ensures registered(\old(getOtherRelations()),other);

getOtherRelation

public Relation getOtherRelation()
return the Relation this Reference belongs to

getOtherRelations

public java.util.List getOtherRelations()
See superclass.
Specifications inherited from overridden method in class Relation:
public behavior
ensures \result != null;
ensures ( \forall Object o; \result .contains(o); o instanceof Relation);
ensures !\result .contains(null);