Class AbstractConnection

  • All Implemented Interfaces:
    Connection
    Direct Known Subclasses:
    AbstractStrongReferencingConnection

    public abstract class AbstractConnection
    extends java.lang.Object
    implements Connection
    Note that for all implementations of Connection, it is essential to consider memory consumption. Connections are kept in the framework for as long as the isRemoved() method returns false, hence this method should return true at the earliest occasion. The AbstractConnection keeps weak references to the objects added through the put methods. As soon as one of these objects is collected by the garbage collector, then this connection becomes removed. Therefore, it is essential that subclasses of the AbstractConnection do not keep pointers to these objects, or at least only keep weak references. This class is annotated with @ConnectionAnnotation. Therefore, none of the implementing classes have to do so explicitly.
    Author:
    bfvdonge
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AbstractConnection​(java.lang.String label)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean containsObjects​(java.lang.Object... objects)
      Return true if all objects given as parameter are contained in the connection.
      boolean containsObjects​(java.util.Collection<?> objects)
      Return true if all objects given as parameter are contained in the connection.
      boolean equals​(java.lang.Object o)  
      protected java.lang.Object get​(java.lang.String role)  
      ConnectionID getID()
      Return the ID of the connection.
      java.lang.String getLabel()
      Return the label of the connection
      protected java.util.Map<java.lang.String,​java.lang.ref.WeakReference<?>> getMapping()  
      MultiSet<java.lang.Object> getObjects()
      Return all objects contained in this connection, without their labels.
      <T> T getObjectWithRole​(java.lang.String role)
      Return the object with the given role in this connection The type of the returned object is T.
      java.util.Set<java.lang.String> getRoles()
      Return the roles of all objects in this connection
      int hashCode()  
      boolean isRemoved()
      Return true if one of the objects connected by this connection no longer exists, i.e.
      protected <T> java.lang.ref.WeakReference<T> put​(java.lang.String role, T o)  
      void remove()
      Removes the connection.
      protected void remove​(java.lang.String role)  
      void setLabel​(java.lang.String name)
      sets the label of the connection to the new name
      void setManager​(ConnectionManager manager)
      Sets the manager for the connection.
      java.lang.String toString()  
      void updated()
      This method should be called as soon as the connection is changed, for example if the label changed, or if the contents of one of the connected objects changes.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • AbstractConnection

        protected AbstractConnection​(java.lang.String label)
    • Method Detail

      • setManager

        public void setManager​(ConnectionManager manager)
        Description copied from interface: Connection
        Sets the manager for the connection. This method is called by the connection manager as soon as this connection is added to that manager. A connection should keep a reference to the manager only in a transient field.
        Specified by:
        setManager in interface Connection
      • getLabel

        public java.lang.String getLabel()
        Description copied from interface: Connection
        Return the label of the connection
        Specified by:
        getLabel in interface Connection
        Returns:
      • containsObjects

        public boolean containsObjects​(java.lang.Object... objects)
        Description copied from interface: Connection
        Return true if all objects given as parameter are contained in the connection. Multiplicities are taken into account, however order is abstracted from
        Specified by:
        containsObjects in interface Connection
        Parameters:
        objects - the objects to check for
        Returns:
      • containsObjects

        public boolean containsObjects​(java.util.Collection<?> objects)
        Description copied from interface: Connection
        Return true if all objects given as parameter are contained in the connection. Multiplicities are taken into account, i.e. each object should occur as often as it is returned by the iterator of the given collection however order is abstracted from
        Specified by:
        containsObjects in interface Connection
        Parameters:
        objects - the objects as a collection
        Returns:
      • isRemoved

        public boolean isRemoved()
        Description copied from interface: Connection
        Return true if one of the objects connected by this connection no longer exists, i.e. it is collected by the garbage collector.
        Specified by:
        isRemoved in interface Connection
        Returns:
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • getObjects

        public MultiSet<java.lang.Object> getObjects()
        Description copied from interface: Connection
        Return all objects contained in this connection, without their labels. By contract, this method should always return the same set of objects after the connections was created, i.e. connections may only be changed by changing the contents of the objects, but not by changing the pointers.
        Specified by:
        getObjects in interface Connection
        Returns:
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • getObjectWithRole

        public <T> T getObjectWithRole​(java.lang.String role)
        Description copied from interface: Connection
        Return the object with the given role in this connection The type of the returned object is T. However, no checks have to be performed to see if the cast can be made. It is up to the calling method to ensure this cast is safe.
        Specified by:
        getObjectWithRole in interface Connection
        Type Parameters:
        T - The type of object that should be returned.
        Parameters:
        role - the role the returned object has to have
        Returns:
        the object attached to this role (not null).
      • getRoles

        public java.util.Set<java.lang.String> getRoles()
        Description copied from interface: Connection
        Return the roles of all objects in this connection
        Specified by:
        getRoles in interface Connection
        Returns:
      • put

        protected <T> java.lang.ref.WeakReference<T> put​(java.lang.String role,
                                                         T o)
      • remove

        protected void remove​(java.lang.String role)
      • get

        protected java.lang.Object get​(java.lang.String role)
      • getMapping

        protected java.util.Map<java.lang.String,​java.lang.ref.WeakReference<?>> getMapping()
      • remove

        public void remove()
        Removes the connection. After calling this method, isRemoved()returns true;
        Specified by:
        remove in interface Connection
      • setLabel

        public void setLabel​(java.lang.String name)
        sets the label of the connection to the new name
        Specified by:
        setLabel in interface Connection
        Parameters:
        name -
      • updated

        public void updated()
        Description copied from interface: Connection
        This method should be called as soon as the connection is changed, for example if the label changed, or if the contents of one of the connected objects changes. By calling this method, the connection manager should be notified.
        Specified by:
        updated in interface Connection