Class SpatialElement

  • All Implemented Interfaces:
    PositionIfc, SpatialElementIfc, GetNameIfc, IdentityIfc

    public class SpatialElement
    extends java.lang.Object
    implements SpatialElementIfc
    A SpatialElement represents an object that is within a spatial model. A spatial element can be in one and only one spatial model at a time. A spatial model may contain many spatial elements. Spatial elements can be added to and removed from a spatial model. A spatial element has a position within a spatial model. The position should allow the distance between spatial elements to be computed within a spatial model. If the position of the spatial element is changed then the spatial element should notify any position observers and should indicate to its spatial model that the position has changed. A spatial element should start in one spatial model. The starting spatial model is the element's initial model. A spatial element may move to other spatial models during a simulation and thus should always have a current spatial model. A spatial element may be related to a ModelElement.
    • Field Detail

      • CHANGED_POSITION

        public static final int CHANGED_POSITION
        An enum to indicate that an element changed its position within a spatial model
    • Constructor Detail

      • SpatialElement

        public SpatialElement​(SpatialModel spatialModel)
        Creates a spatial element with the given spatial model. The CoordinateIfc will be the default within the supplied spatial model.
        Parameters:
        spatialModel -
      • SpatialElement

        public SpatialElement​(SpatialModel spatialModel,
                              CoordinateIfc coordinate)
        Creates a spatial element with the given parent and spatial model. TThe CoordinateIfc must be valid within the supplied spatial model.
        Parameters:
        spatialModel -
        coordinate -
      • SpatialElement

        public SpatialElement​(SpatialModel spatialModel,
                              java.lang.String name)
        Creates a spatial element with the given parent and spatial model. TThe coordinate will be the default within the supplied spatial model.
        Parameters:
        spatialModel -
        name -
      • SpatialElement

        public SpatialElement​(SpatialModel spatialModel,
                              double x,
                              double y)
        Creates a spatial element at the given (x, y, 0.0) coordinates
        Parameters:
        spatialModel -
        x -
        y -
      • SpatialElement

        public SpatialElement​(SpatialModel spatialModel,
                              double x,
                              double y,
                              java.lang.String name)
        Creates a spatial element at the given (x, y, 0.0) coordinates
        Parameters:
        spatialModel -
        x -
        y -
        name -
      • SpatialElement

        public SpatialElement​(SpatialModel spatialModel,
                              double x,
                              double y,
                              double z)
        Creates a spatial element at the given (x,y,z) coordinates
        Parameters:
        spatialModel -
        x -
        y -
        z -
      • SpatialElement

        public SpatialElement​(SpatialModel spatialModel,
                              double x,
                              double y,
                              double z,
                              java.lang.String name)
        Creates a spatial element at the given (x,y,z) coordinates
        Parameters:
        spatialModel -
        x -
        y -
        z -
        name -
      • SpatialElement

        public SpatialElement​(SpatialModel spatialModel,
                              CoordinateIfc coordinate,
                              java.lang.String name)
        Creates a spatial element with the given parent and spatial model. The coordinate must be valid within the supplied spatial model.
        Parameters:
        spatialModel -
        coordinate -
        name -
    • Method Detail

      • toString

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

        public final java.lang.String getName()
        Specified by:
        getName in interface GetNameIfc
        Returns:
        a string representing the name of the object
      • getId

        public final int getId()
        Specified by:
        getId in interface IdentityIfc
        Returns:
        an int representing the id of the object
      • getObserverState

        public final int getObserverState()
        Description copied from interface: SpatialElementIfc
        Observers can call this to get an integer representing the state of the element after the observers have been notified
        Specified by:
        getObserverState in interface SpatialElementIfc
        Returns:
        the observer state
      • distanceTo

        public final double distanceTo​(CoordinateIfc coordinate)
        Description copied from interface: SpatialElementIfc
        This is a "convenience" method for getting the distance from this element to the supplied coordinate within the underlying spatial model
        Specified by:
        distanceTo in interface SpatialElementIfc
        Parameters:
        coordinate - the coordinate to check
        Returns:
        the distance
      • isPositionEqualTo

        public final boolean isPositionEqualTo​(CoordinateIfc coordinate)
        Description copied from interface: SpatialElementIfc
        Returns true if the position of this element is the same as supplied coordinate within the underlying spatial model. This is not necessarily object reference equality, but rather whether or not the positions within the underlying spatial model can be considered the same (equivalent).
        Specified by:
        isPositionEqualTo in interface SpatialElementIfc
        Parameters:
        coordinate - the coordinate to check
        Returns:
        true if position is equal
      • distanceTo

        public final double distanceTo​(SpatialElementIfc element)
        Description copied from interface: SpatialElementIfc
        This is a "convenience" method for getting the distance from this element to the supplied element within the underlying spatial model Requirement: The elements must be in the same spatial model. The distance should be calculated by the spatial model. If they are not in the same spatial model this method will throw and IllegalArgumentException
        Specified by:
        distanceTo in interface SpatialElementIfc
        Parameters:
        element - the element to check
        Returns:
        the distance
      • isPositionEqualTo

        public final boolean isPositionEqualTo​(SpatialElementIfc element)
        Description copied from interface: SpatialElementIfc
        Returns true if the position of this element is the same as the position of the supplied element within the underlying spatial model. This is not necessarily object reference equality, but rather whether or not the positions within the underlying spatial model can be considered the same (equivalent). Requirement: The elements must be in the same spatial model. If they are not in the same spatial model, then this method should return false.
        Specified by:
        isPositionEqualTo in interface SpatialElementIfc
        Parameters:
        element - the element to check
        Returns:
        the distance
      • changeSpatialModel

        public final void changeSpatialModel​(SpatialModel spatialModel,
                                             CoordinateIfc coordinate)
        Description copied from interface: SpatialElementIfc
        Changes the spatial model for this element and places the element at the supplied coordinate within the new spatial model. Throws IllegalArgumentException if the coordinate is not valid for the supplied spatial model. This spatial element becomes a child element of the new spatial model.
        Specified by:
        changeSpatialModel in interface SpatialElementIfc
        Parameters:
        spatialModel - the spatial model
        coordinate - the coordinate
      • setInitialPosition

        public final void setInitialPosition​(CoordinateIfc initialPosition)
        Description copied from interface: SpatialElementIfc
        Sets the initial position of the element. This position should be used when initializeSpatialElement() is called, typically at the beginning of a replication.
        Specified by:
        setInitialPosition in interface SpatialElementIfc
        Parameters:
        initialPosition - the coordinate
      • setSpatialModel

        protected final void setSpatialModel​(SpatialModel spatialModel)
        Sets the spatial model for the spatial element. This method is used by addSpatialElement() within SpatialModel
        Parameters:
        spatialModel -
      • setModelElement

        protected void setModelElement​(ModelElement modelElement)
        Sets the model element associated with this spatial element, may be null
        Parameters:
        modelElement -
      • setCurrentPosition

        protected final void setCurrentPosition​(CoordinateIfc currentPosition)
        Parameters:
        currentPosition - The currentPosition to set.
      • setPreviousPosition

        protected final void setPreviousPosition​(CoordinateIfc previousPosition)
        Parameters:
        previousPosition - The previousPosition to set.
      • setName

        protected final void setName​(java.lang.String str)
        Sets the name of this element
        Parameters:
        str - The name as a string.
      • setInitialSpatialModel

        protected final void setInitialSpatialModel​(SpatialModel spatialModel)
        Sets the initial spatial model for the position. Since a element can move between spatial models during a simulation, there must be a way to remember which spatial model the element started with prior to re-running a simulation This method sets the initial spatial model. This only has an effect when the element is initialized. Calling this method during a simulation has no effect until the element is initialized. The current spatial model is not changed by this method. When the element is initialized, its current spatial model is set to the supplied spatial model.
        Parameters:
        spatialModel -
      • setObserverState

        protected final void setObserverState​(int observerState)
        Can be called by sub-classes to set an indicator of the state of the position prior to notifying observers
        Parameters:
        observerState - The observerState to set.
      • notifyObservers

        protected final void notifyObservers​(int observerState)
        Parameters:
        observerState - The observerState to set.
      • attachPositionObserver

        public void attachPositionObserver​(ObserverIfc observer)
        Description copied from interface: SpatialElementIfc
        Implementor of this interface should allow Observers to be attached. For example, the observers should be notified when the position changes. It is the responsibility of implementers to properly notify the observers.
        Specified by:
        attachPositionObserver in interface SpatialElementIfc
        Parameters:
        observer - the observer
      • notifyObservers

        protected final void notifyObservers​(java.lang.Object theObserved,
                                             java.lang.Object arg)