jrummikub.model
Class StoneTray<E extends Sizeable>

java.lang.Object
  extended by jrummikub.model.StoneTray<E>
Type Parameters:
E - Type of positioned objects (must implement Sizeable)
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<Pair<E,Position>>, IStoneTray<E>
Direct Known Subclasses:
Hand, Table

public class StoneTray<E extends Sizeable>
extends java.lang.Object
implements IStoneTray<E>

A StoneTray is a collection of positioned objects (for example Stones or StoneSets.

See Also:
Serialized Form

Nested Class Summary
protected static class StoneTray.Direction
          Possible move directions in case of overlapping Stones/Sets
 
Field Summary
protected  java.util.HashMap<E,Pair<E,Position>> objects
           
private static long serialVersionUID
           
 
Constructor Summary
StoneTray()
           
 
Method Summary
 IStoneTray<E> clone()
          Create a clone of the StoneTray
 boolean contains(E object)
          Search for an object within the stone tray
 void drop(E object, Position position)
          Adds object to the tray
private  void drop(E object, Position position, StoneTray.Direction direction)
          Subroutine to "drop" to consider and determine the direction the objects dropped one collides with position-wise evade in
private  void dropUnchecked(E object, Position position, StoneTray.Direction direction)
          Subroutine to "drop" to execute the actual drop
protected  Pair<Position,StoneTray.Direction> fixInvalidDrop(E object, Position pos, StoneTray.Direction dir)
          Checks whether the object may be placed on the given position, computes new position if not
private  StoneTray.Direction getMoveDirection(E object, Position position, Pair<E,Position> blocking)
          Returns the direction to move the object in
private  boolean getMoveOrientation(E object, Position position, Pair<E,Position> blocking)
          Will the object be moved horizontally or vertically
private  Position getNewPosition(E object, Position position, StoneTray.Direction newDirection, E currentObject, Position currentPosition)
           
 Position getPosition(E object)
          Returns the position of an object that is already on the tray
 int getSize()
          Return the number of objects on the tray
 java.util.Iterator<Pair<E,Position>> iterator()
           
private static boolean lessOrEqual(double d, double e)
          Static method for determining a less or equal relation considering a small fuzziness
private  boolean objectsOverlap(E object1, Position position1, E object2, Position position2)
          Tests whether two objects overlap
 boolean pickUp(E object)
          Tries to pick up (remove) a given object
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

objects

protected java.util.HashMap<E extends Sizeable,Pair<E extends Sizeable,Position>> objects
Constructor Detail

StoneTray

public StoneTray()
Method Detail

drop

public void drop(E object,
                 Position position)
Description copied from interface: IStoneTray
Adds object to the tray

Specified by:
drop in interface IStoneTray<E extends Sizeable>
Parameters:
object - object to add to Hand
position - Position to put the object

drop

private void drop(E object,
                  Position position,
                  StoneTray.Direction direction)
Subroutine to "drop" to consider and determine the direction the objects dropped one collides with position-wise evade in

Parameters:
object - the object to add to Hand
position - Position to put the object
direction - the direction the other stones evade in

dropUnchecked

private void dropUnchecked(E object,
                           Position position,
                           StoneTray.Direction direction)
Subroutine to "drop" to execute the actual drop

Parameters:
object - the object to add to Hand
position - Position to put the object
direction - the direction the other stones evade in

getNewPosition

private Position getNewPosition(E object,
                                Position position,
                                StoneTray.Direction newDirection,
                                E currentObject,
                                Position currentPosition)

fixInvalidDrop

protected Pair<Position,StoneTray.Direction> fixInvalidDrop(E object,
                                                            Position pos,
                                                            StoneTray.Direction dir)
Checks whether the object may be placed on the given position, computes new position if not

Parameters:
object - to be dropped
dir -
pos - the object is dropped at
Returns:
null if the drop is valid, new position otherwise

lessOrEqual

private static boolean lessOrEqual(double d,
                                   double e)
Static method for determining a less or equal relation considering a small fuzziness

Parameters:
d - the value to be less or equal
e - than the other one
Returns:
if d is less or equal e

objectsOverlap

private boolean objectsOverlap(E object1,
                               Position position1,
                               E object2,
                               Position position2)
Tests whether two objects overlap

Parameters:
object1 - first object
position1 - first object's position
object2 - second object
position2 - second object's position
Returns:
whether they overlap

getMoveDirection

private StoneTray.Direction getMoveDirection(E object,
                                             Position position,
                                             Pair<E,Position> blocking)
Returns the direction to move the object in

Parameters:
object - the object
position - the object's position
blocking - the object thats blocking
Returns:
the direction

getMoveOrientation

private boolean getMoveOrientation(E object,
                                   Position position,
                                   Pair<E,Position> blocking)
Will the object be moved horizontally or vertically

Parameters:
object - the object
position - the objects position
blocking - the object thats blocking
Returns:
boolean vertical movement

getPosition

public Position getPosition(E object)
Description copied from interface: IStoneTray
Returns the position of an object that is already on the tray

Specified by:
getPosition in interface IStoneTray<E extends Sizeable>
Parameters:
object - object whose position is requested
Returns:
position of the object or null when the object is not on the tray

contains

public boolean contains(E object)
Description copied from interface: IStoneTray
Search for an object within the stone tray

Specified by:
contains in interface IStoneTray<E extends Sizeable>
Parameters:
object - object to search for
Returns:
object found in stone tray

iterator

public java.util.Iterator<Pair<E,Position>> iterator()
Specified by:
iterator in interface java.lang.Iterable<Pair<E extends Sizeable,Position>>

pickUp

public boolean pickUp(E object)
Description copied from interface: IStoneTray
Tries to pick up (remove) a given object

Specified by:
pickUp in interface IStoneTray<E extends Sizeable>
Parameters:
object - object to pick up
Returns:
true when the object was successfully removed

clone

public IStoneTray<E> clone()
Description copied from interface: IStoneTray
Create a clone of the StoneTray

Specified by:
clone in interface IStoneTray<E extends Sizeable>
Overrides:
clone in class java.lang.Object
Returns:
cloned StoneTray

getSize

public int getSize()
Description copied from interface: IStoneTray
Return the number of objects on the tray

Specified by:
getSize in interface IStoneTray<E extends Sizeable>
Returns:
number of objects