Package net.sf.gridarta.model.baseobject
Class GameObjectContainer<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>
- java.lang.Object
-
- net.sf.gridarta.model.baseobject.GameObjectContainer<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.SerializableBase class for classes that contain GameObjects as children in the sense of containment. The interface serves 2 main purposes:GameObjectextends this class for containing other GameObjects, like inventory contents e.g. of a bag or attached events.MapSquareextends 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 protectedGameObjectContainer()Creates a new instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddFirst(G gameObject)Adds the given game object at the end of this container.voidaddLast(G gameObject)Adds the given game object at the end of this container.abstract GasGameObject()Returns this instance as aGameObjectornullif this instance is not a game object.protected @NotNull java.lang.Objectclone()GgetFirst()Returns the first GameObject contained in this container.GgetLast()Returns the last GameObject contained in this container.abstract @NotNull MapSquare<G,A,R>getMapSquare()Returns theMapSquarethis game object is part of.abstract @Nullable MapSquare<G,A,R>getMapSquareOptional()Returns theMapSquarethis game object is part of.GgetNext(G gameObject)Returns theGameObjectsucceeding a given game object.GgetPrev(G gameObject)Returns theGameObjectpreceding a given game object.protected booleanhasSameContents(@NotNull GameObjectContainer<?,?,?> gameObjectContainer)Compares this object to another game object container.voidinsertAfter(G previousGameObject, G gameObject)Adds a game object after another.voidinsertBefore(G gameObject, G nextGameObject)Adds a game object before another.booleanisBottom(G gameObject)Returns whether this game object is the bottom-most one.booleanisEmpty()Check whether this square is empty.booleanisTop(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.voidmoveBottom(G gameObject)Moves a game object to bottom.voidmoveDown(G gameObject)Moves a game object down.voidmoveTop(G gameObject)Moves a game object to top.voidmoveUp(G gameObject)Moves a game object up.protected abstract voidnotifyBeginChange()Notifies the map model that this container is about to change.protected abstract voidnotifyEndChange()Notifies the map model that this container has changed.@NotNull java.lang.Iterable<G>recursive()Returns an object that is a recursive representation.voidremove(G gameObject)Removes a game object from this container.voidremoveAll()Removes all game objects from this container.voidreplace(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 voidsetThisContainer(G gameObject)Sets aGameObject's container to this container.@NotNull java.lang.StringtoString()
-
-
-
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:
iteratorin interfacejava.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:
trueif this square is empty, otherwisefalse
-
getFirst
@Nullable public G getFirst()
Returns the first GameObject contained in this container.- Returns:
- first GameObject contained or
nullifisEmpty()returnstrue
-
getPrev
@Nullable public G getPrev(@NotNull G gameObject)
Returns theGameObjectpreceding a given game object.- Parameters:
gameObject- the given game object- Returns:
- the preceding game object or
nullif no preceding game object exists.
-
getNext
@Nullable public G getNext(@NotNull G gameObject)
Returns theGameObjectsucceeding a given game object.- Parameters:
gameObject- the given game object- Returns:
- the successor game object or
nullif 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
nullifisEmpty()returnstrue
-
remove
public void remove(@NotNull G gameObject)Removes a game object from this container.- Parameters:
gameObject- GameObject to remove- Throws:
java.lang.IllegalArgumentException- ifgameObjectisn'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- ifgameObjectisn'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- ifgameObjectisn'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- ifgameObjectisn'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- ifgameObjectisn'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- ifgameObjectisn'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- ifgameObjectisn'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 unlinkedGameObjectto be placed in the inventory- Throws:
java.lang.IllegalArgumentException- ifgameObjectalready 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 unlinkedGameObjectto be placed in the inventory- Throws:
java.lang.IllegalArgumentException- ifgameObjectalready is inside another container
-
insertAfter
public void insertAfter(@Nullable G previousGameObject, @NotNull G gameObject)Adds a game object after another.- Parameters:
previousGameObject- previous anchor ornullto insert lastgameObject- GameObject to insert- Throws:
java.lang.IllegalArgumentException- ifgameObjectalready is inside another container orpreviousGameObjectisn't inside this container
-
insertBefore
public void insertBefore(@NotNull G gameObject, @Nullable G nextGameObject)Adds a game object before another.- Parameters:
gameObject- GameObject to insertnextGameObject- nextGameObject anchor ornullto add first- Throws:
java.lang.IllegalArgumentException- ifgameObjectalready is inside another container orprevisn'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 replacednewGameObject- new GameObject that replaces oldGameObject- Throws:
java.lang.IllegalArgumentException- ifoldGameObjectisn't in this container or is a multi-part object
-
getMapSquare
@NotNull public abstract @NotNull MapSquare<G,A,R> getMapSquare()
Returns theMapSquarethis 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 theMapSquarethis game object is part of.- Returns:
- the map square or
nullif 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:
clonein classjava.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:
trueif this object equals the other object
-
setThisContainer
protected abstract void setThisContainer(@NotNull G gameObject)Sets aGameObject's container to this container.- Parameters:
gameObject- the game object
-
asGameObject
@Nullable public abstract G asGameObject()
Returns this instance as aGameObjectornullif this instance is not a game object.- Returns:
- this instance or
null
-
toString
@NotNull public @NotNull java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-