Class GameObjectContainer<G extends GameObject<G,​A,​R>,​A extends MapArchObject<A>,​R extends Archetype<G,​A,​R>>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<G>
    Direct Known Subclasses:
    AbstractBaseObject, MapSquare

    public abstract class GameObjectContainer<G extends GameObject<G,​A,​R>,​A extends MapArchObject<A>,​R extends Archetype<G,​A,​R>>
    extends java.lang.Object
    implements java.lang.Cloneable, java.lang.Iterable<G>, java.io.Serializable
    Base class for classes that contain GameObjects as children in the sense of containment. The interface serves 2 main purposes:
    • GameObject extends this class for containing other GameObjects, like inventory contents e.g. of a bag or attached events.
    • MapSquare extends this class to list the squares on a MapSquare.
    See Also:
    Serialized Form
    Todo:
    In case of MapSquares, this class is most likely bogus regarding multi-part objects. This needs to be fixed.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected GameObjectContainer()
      Creates a new instance.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void addFirst​(G gameObject)
      Adds the given game object at the end of this container.
      void addLast​(G gameObject)
      Adds the given game object at the end of this container.
      abstract G asGameObject()
      Returns this instance as a GameObject or null if this instance is not a game object.
      protected @NotNull java.lang.Object clone()  
      G getFirst()
      Returns the first GameObject contained in this container.
      G getLast()
      Returns the last GameObject contained in this container.
      abstract @NotNull MapSquare<G,​A,​R> getMapSquare()
      Returns the MapSquare this game object is part of.
      abstract @Nullable MapSquare<G,​A,​R> getMapSquareOptional()
      Returns the MapSquare this game object is part of.
      G getNext​(G gameObject)
      Returns the GameObject succeeding a given game object.
      G getPrev​(G gameObject)
      Returns the GameObject preceding a given game object.
      protected boolean hasSameContents​(@NotNull GameObjectContainer<?,​?,​?> gameObjectContainer)
      Compares this object to another game object container.
      void insertAfter​(G previousGameObject, G gameObject)
      Adds a game object after another.
      void insertBefore​(G gameObject, G nextGameObject)
      Adds a game object before another.
      boolean isBottom​(G gameObject)
      Returns whether this game object is the bottom-most one.
      boolean isEmpty()
      Check whether this square is empty.
      boolean isTop​(G gameObject)
      Returns whether this game object is the top-most one.
      @NotNull java.util.Iterator<G> iterator()
      The Iterator returned does not recurse, it only contains objects on the first level.
      void moveBottom​(G gameObject)
      Moves a game object to bottom.
      void moveDown​(G gameObject)
      Moves a game object down.
      void moveTop​(G gameObject)
      Moves a game object to top.
      void moveUp​(G gameObject)
      Moves a game object up.
      protected abstract void notifyBeginChange()
      Notifies the map model that this container is about to change.
      protected abstract void notifyEndChange()
      Notifies the map model that this container has changed.
      @NotNull java.lang.Iterable<G> recursive()
      Returns an object that is a recursive representation.
      void remove​(G gameObject)
      Removes a game object from this container.
      void removeAll()
      Removes all game objects from this container.
      void replace​(G oldGameObject, G newGameObject)
      Replaces a game object with another one.
      @NotNull java.lang.Iterable<G> reverse()
      Returns an object that is the reverse representation.
      protected abstract void setThisContainer​(G gameObject)
      Sets a GameObject's container to this container.
      @NotNull java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • GameObjectContainer

        protected GameObjectContainer()
        Creates a new instance.
    • Method Detail

      • iterator

        @NotNull
        public @NotNull java.util.Iterator<G> iterator()
        The Iterator returned does not recurse, it only contains objects on the first level. The Iterator returned is transparent, that means modifying the iterator's collection actually modifies the underlying GameObjectContainer.
        Specified by:
        iterator in interface java.lang.Iterable<G extends GameObject<G,​A,​R>>
      • reverse

        @NotNull
        public @NotNull java.lang.Iterable<G> reverse()
        Returns an object that is the reverse representation. Invoke this method if you want to iterate over the contained GameObjects in reverse order.
        Returns:
        reverse representation
      • recursive

        @NotNull
        public @NotNull java.lang.Iterable<G> recursive()
        Returns an object that is a recursive representation. Invoke this method if you want to iterate over the contained GameObjects recursively.
        Returns:
        recursive representation
      • isEmpty

        public boolean isEmpty()
        Check whether this square is empty.
        Returns:
        true if this square is empty, otherwise false
      • getFirst

        @Nullable
        public G getFirst()
        Returns the first GameObject contained in this container.
        Returns:
        first GameObject contained or null if isEmpty() returns true
      • getPrev

        @Nullable
        public G getPrev​(@NotNull
                         G gameObject)
        Returns the GameObject preceding a given game object.
        Parameters:
        gameObject - the given game object
        Returns:
        the preceding game object or null if no preceding game object exists.
      • getNext

        @Nullable
        public G getNext​(@NotNull
                         G gameObject)
        Returns the GameObject succeeding a given game object.
        Parameters:
        gameObject - the given game object
        Returns:
        the successor game object or null if no successor game object exists.
      • getLast

        @Nullable
        public G getLast()
        Returns the last GameObject contained in this container. You should not invoke this method to iterate over GameObjects, such invocation is regarded deprecated.
        Returns:
        last GameObject contained or null if isEmpty() returns true
      • remove

        public void remove​(@NotNull
                           G gameObject)
        Removes a game object from this container.
        Parameters:
        gameObject - GameObject to remove
        Throws:
        java.lang.IllegalArgumentException - if gameObject isn't in this container
        Fixme:
        this implementation does not take care of multi square objects.
      • removeAll

        public void removeAll()
        Removes all game objects from this container.
        Fixme:
        this implementation does not take care of multi square objects.
      • isTop

        public boolean isTop​(@NotNull
                             G gameObject)
        Returns whether this game object is the top-most one.
        Parameters:
        gameObject - item to move to top
        Returns:
        whether this game object is the top-most one
        Throws:
        java.lang.IllegalArgumentException - if gameObject isn't in this container
      • isBottom

        public boolean isBottom​(@NotNull
                                G gameObject)
        Returns whether this game object is the bottom-most one.
        Parameters:
        gameObject - item to move to top
        Returns:
        whether this game object is the bottom-most one
        Throws:
        java.lang.IllegalArgumentException - if gameObject isn't in this container
      • moveTop

        public void moveTop​(@NotNull
                            G gameObject)
        Moves a game object to top.
        Parameters:
        gameObject - item to move to top
        Throws:
        java.lang.IllegalArgumentException - if gameObject isn't in this container
      • moveUp

        public void moveUp​(@NotNull
                           G gameObject)
        Moves a game object up.
        Parameters:
        gameObject - item to move up
        Throws:
        java.lang.IllegalArgumentException - if gameObject isn't in this container
      • moveDown

        public void moveDown​(@NotNull
                             G gameObject)
        Moves a game object down.
        Parameters:
        gameObject - item to move down
        Throws:
        java.lang.IllegalArgumentException - if gameObject isn't in this container
      • moveBottom

        public void moveBottom​(@NotNull
                               G gameObject)
        Moves a game object to bottom.
        Parameters:
        gameObject - item to move to bottom
        Throws:
        java.lang.IllegalArgumentException - if gameObject isn't in this container
      • addLast

        public void addLast​(@NotNull
                            G gameObject)
        Adds the given game object at the end of this container.
        Parameters:
        gameObject - the free yet unlinked GameObject to be placed in the inventory
        Throws:
        java.lang.IllegalArgumentException - if gameObject already is inside another container
      • addFirst

        public void addFirst​(@NotNull
                             G gameObject)
        Adds the given game object at the end of this container.
        Parameters:
        gameObject - the free yet unlinked GameObject to be placed in the inventory
        Throws:
        java.lang.IllegalArgumentException - if gameObject already is inside another container
      • insertAfter

        public void insertAfter​(@Nullable
                                G previousGameObject,
                                @NotNull
                                G gameObject)
        Adds a game object after another.
        Parameters:
        previousGameObject - previous anchor or null to insert last
        gameObject - GameObject to insert
        Throws:
        java.lang.IllegalArgumentException - if gameObject already is inside another container or previousGameObject isn't inside this container
      • insertBefore

        public void insertBefore​(@NotNull
                                 G gameObject,
                                 @Nullable
                                 G nextGameObject)
        Adds a game object before another.
        Parameters:
        gameObject - GameObject to insert
        nextGameObject - nextGameObject anchor or null to add first
        Throws:
        java.lang.IllegalArgumentException - if gameObject already is inside another container or prev isn't inside this container
      • replace

        public void replace​(@NotNull
                            G oldGameObject,
                            @NotNull
                            G newGameObject)
        Replaces a game object with another one.
        Parameters:
        oldGameObject - old GameObject to be replaced
        newGameObject - new GameObject that replaces oldGameObject
        Throws:
        java.lang.IllegalArgumentException - if oldGameObject isn't in this container or is a multi-part object
      • getMapSquare

        @NotNull
        public abstract @NotNull MapSquare<G,​A,​R> getMapSquare()
        Returns the MapSquare this game object is part of.
        Returns:
        the map square
        Throws:
        java.lang.IllegalStateException - if the game object is not part of a map
      • getMapSquareOptional

        @Nullable
        public abstract @Nullable MapSquare<G,​A,​R> getMapSquareOptional()
        Returns the MapSquare this game object is part of.
        Returns:
        the map square or null if the game object is not part of a map
      • notifyBeginChange

        protected abstract void notifyBeginChange()
        Notifies the map model that this container is about to change.
      • notifyEndChange

        protected abstract void notifyEndChange()
        Notifies the map model that this container has changed.
      • clone

        @NotNull
        protected @NotNull java.lang.Object clone()
        Overrides:
        clone in class java.lang.Object
      • hasSameContents

        protected boolean hasSameContents​(@NotNull
                                          @NotNull GameObjectContainer<?,​?,​?> gameObjectContainer)
        Compares this object to another game object container.
        Parameters:
        gameObjectContainer - the other game object container
        Returns:
        true if this object equals the other object
      • setThisContainer

        protected abstract void setThisContainer​(@NotNull
                                                 G gameObject)
        Sets a GameObject's container to this container.
        Parameters:
        gameObject - the game object
      • asGameObject

        @Nullable
        public abstract G asGameObject()
        Returns this instance as a GameObject or null if this instance is not a game object.
        Returns:
        this instance or null
      • toString

        @NotNull
        public @NotNull java.lang.String toString()
        Overrides:
        toString in class java.lang.Object