org.jutil.relation
Class ReferenceSet

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

public class ReferenceSet
extends Relation

A class of Relation components for implementing a binding in which the object of the Reference has a relation with N other objects. This component behaves as a set.

In UML this class is used for implementing multiplicity n:

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 ReferenceSet object and A is made by passing this to the constructor of ReferenceSet.

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

This class is typically using in the following way.


public class A {
  public A() {
    _b= new ReferenceSet(this);
  }
  public List getBs() {
    return _b.getOtherEnds();
  }
  // public Set getBs() {
  //   return new TreeSet(_b.getOtherEnds());
  // }
  public void addB(B b) {
    _b.add(b.getALink());
  }
  public void removeB(B b) {
    _b.remove(b.getALink());
  }
  private ReferenceSet _b;
}
 

The other class must have a method getALink(), 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 getALink() 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 A the addB() may be removed. Similarly, getBLink() may be removed if the binding is not mutable from class B.

If getBs() must return a Set, you should add the result of _b.getOtherEnds() to a new Set (e.g. TreeSet). The general method getOtherEnds() must return a List because in some bindings the order may be important.


Class Specifications
public invariant contains(null) == false;
public invariant getObject() != null;
public invariant ( \forall Relation e; contains(e); e.contains(this));

Field Summary
static java.lang.String CVS_REVISION
           
 
Constructor Summary
ReferenceSet(java.lang.Object object)
          Initialize an empty ReferenceSet for the given object.
 
Method Summary
 void add(Relation element)
          Add the given Relation to this ReferenceSet.
 boolean contains(Relation element)
          Check whether or not the given element is connected to this ReferenceSet.
 java.util.List getOtherEnds()
          Return a set containing the objects at the n side of the 1-n binding.
 java.util.List getOtherRelations()
          Return a set containing the Relations at the other side of this binding.
protected  boolean isValidElement(Relation relation)
           
protected  void register(Relation element)
          Add the given Relation to this ReferenceSet
 boolean registered(java.util.List oldConnections, Relation registered)
           
 void remove(Relation other)
          Remove the given Relation from this ReferenceSet.
 int size()
          Return the size of the ReferenceSet
protected  void unregister(Relation element)
          Remove the given Relation from this ReferenceSet
 boolean unregistered(java.util.List oldConnections, Relation unregistered)
           
 
Methods inherited from class org.jutil.relation.Relation
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

ReferenceSet

public ReferenceSet(java.lang.Object object)
Initialize an empty ReferenceSet for the given object.

Parameters:
object - The object on this side of the binding

Specifications:
public behavior
requires object != null;
ensures getObject() == object;
ensures ( \forall Relation r; ; !contains(r));
Method Detail

remove

public void remove(Relation other)
Remove the given Relation from this ReferenceSet.

Parameters:
element - The Relation to be removed.

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

add

public void add(Relation element)
Add the given Relation to this ReferenceSet.

Parameters:
element - The Relation to be added.

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

getOtherEnds

public java.util.List getOtherEnds()
Return a set containing the objects at the n side of the 1-n binding.

Specifications:
     also
public behavior
ensures ( \forall Object o; ; \result .contains(o) <==> ( \exists Relation r; contains(r); r.getObject().equals(o)));
ensures \result != null;

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));

getOtherRelations

public java.util.List getOtherRelations()
Return a set containing the Relations at the other side of this binding.

Specifications:
     also
public behavior
ensures ( \forall Relation s; ; contains(s) <==> \result .contains(s));
ensures \result != null;

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);

unregister

protected void unregister(Relation element)
Remove the given Relation from this ReferenceSet

Parameters:
element - The element to be removed.

Specifications:
     also
protected behavior
requires element != null;
ensures !contains(element);

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

register

protected void register(Relation element)
Add the given Relation to this ReferenceSet

Parameters:
element - The element to be added.

Specifications:
     also
protected behavior
requires element != null;

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

registered

public boolean registered(java.util.List oldConnections,
                          Relation registered)
Specifications:
     also
public behavior
ensures \result == (contains(registered))&&( \forall Relation r; r != registered; oldConnections.contains(r) == contains(r));

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 == (oldConnections.contains(unregistered))&&( \forall Object o; oldConnections.contains(o); o instanceof Relation)&&(!contains(unregistered))&&( \forall Relation r; r != unregistered; oldConnections.contains(r) == contains(r));

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 == (relation != null);

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

size

public int size()
Return the size of the ReferenceSet

Specifications:
public behavior
ensures \result == getOtherRelations().size();

contains

public boolean contains(Relation element)
Check whether or not the given element is connected to this ReferenceSet.

Overrides:
contains in class Relation
Parameters:
element - The element of which one wants to know if it is in this ReferenceSet.
Specifications inherited from overridden method in class Relation:
public behavior
ensures \result == getOtherRelations().contains(relation);