wyvern.kernel.maps
Class GameHashMap

java.lang.Object
  extended byjava.util.AbstractMap
      extended byjava.util.HashMap
          extended bywyvern.kernel.maps.GameHashMap
All Implemented Interfaces:
java.lang.Cloneable, java.util.Map, java.io.Serializable

public class GameHashMap
extends java.util.HashMap

A version of MultiHashMap that uses MapCells as its buckets, so it can group similar GameObjects. This implementation is not synchronized. If multiple threads are going to access it concurrently, and at least one of the threads modifies the map structurally (adds or removes keys), you must synchronize access to all its read/write methods.

Version:
1.0, Jan 15, 1998
Author:
Steve Yegge
See Also:
Serialized Form

Constructor Summary
GameHashMap()
           
 
Method Summary
 void add(int x, int y, java.lang.Object value)
          Adds an object to the map.
 void add(java.lang.Object key, java.lang.Object value)
          Adds an object reference to the map.
 java.lang.Object get(java.lang.Object key)
          This operation is a bit ambiguous; it's not clear whether it should return the bucket or the first item in the bucket, so I'm just getting rid of it completely.
protected  Cell getBucket(int x, int y)
          Returns the bucket at the specified location.
protected  Cell getBucket(java.lang.Object key)
          Returns the bucket at the specified location.
 java.lang.Object getFirst(int x, int y)
          Returns the first object at the specified location.
 java.lang.Object getFirst(Point p)
          Returns the first object at the specified location.
 java.util.Iterator iterator(int x, int y)
          Returns an iterator over the objects at (x, y).
protected  Cell makeBucket(java.lang.Object key)
          Creates and returns a new bucket at the specified location.
 java.util.List objectsAt(int x, int y)
          Returns a list of the objects at location (x, y);
 java.util.List objectsAt(Point p)
          Returns a list of the objects at location p.
 java.util.Iterator objectsInRect(int x, int y, int w, int h)
          Returns an Iterator over the objects in the specified rectangle.
 void printTree()
          Prints out the contents of the map, for debugging.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Don't let them try the put() operation directly, since we use buckets as our values.
 void remove(int x, int y, java.lang.Object obj)
          Removes the specified object at the specified location.
 void remove(Point p, java.lang.Object obj)
          Removes the specified object from the specified location.
 java.util.Collection values()
          Returns a Collection view of the buckets (Cells) at the nonempty locations in this map.
 java.util.Collection values(java.lang.Object key)
          Returns a Collection view of the values under the given key.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, isEmpty, keySet, putAll, remove, size
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

GameHashMap

public GameHashMap()
Method Detail

get

public java.lang.Object get(java.lang.Object key)
This operation is a bit ambiguous; it's not clear whether it should return the bucket or the first item in the bucket, so I'm just getting rid of it completely.

Parameters:
key - a 2D map location to check for objects
Throws:
java.lang.UnsupportedOperationException - (always).

add

public void add(int x,
                int y,
                java.lang.Object value)
Adds an object to the map.

Parameters:
x - the x coordinate to add at
y - the y coordinate to add at
value - the object to add (must be of class GameObject)

add

public void add(java.lang.Object key,
                java.lang.Object value)
Adds an object reference to the map.

Parameters:
key - the 2D map coordinate at which to put the object
value - the GameObject to place at that coordinate
Throws:
java.lang.ClassCastException - if value isn't a GameObject

remove

public void remove(Point p,
                   java.lang.Object obj)
Removes the specified object from the specified location.

Parameters:
p - the location to remove it from
obj - the object to remove. Does nothing if not found.

remove

public void remove(int x,
                   int y,
                   java.lang.Object obj)
Removes the specified object at the specified location.

Parameters:
x - map x location
y - map y location
obj - object to remove. Does nothing if not found.

values

public java.util.Collection values()
Returns a Collection view of the buckets (Cells) at the nonempty locations in this map. An iterator over this Collection will return the buckets in the same order as their keys (top to bottom, left to right).

Returns:
a Collection view of the buckets in the map.

getFirst

public java.lang.Object getFirst(Point p)
Returns the first object at the specified location.

Parameters:
p - the map location to examine
Returns:
the first object found at the location, or null if none.

getFirst

public java.lang.Object getFirst(int x,
                                 int y)
Returns the first object at the specified location.

Parameters:
x - map x location
y - map y location
Returns:
the first object found at the location, or null if none.

values

public java.util.Collection values(java.lang.Object key)
Returns a Collection view of the values under the given key. The Collection supports the same semantics as that returned by MapCell.values().

Parameters:
key - the key to search for
Returns:
a Collection view of the bucket under that key (the bucket is a MapCell).

objectsAt

public java.util.List objectsAt(int x,
                                int y)
Returns a list of the objects at location (x, y);

Parameters:
x - map x location
y - map y location
Returns:
a List of objects at (x, y). Can return null if no objects are there, or an empty list.

iterator

public java.util.Iterator iterator(int x,
                                   int y)
Returns an iterator over the objects at (x, y).

Parameters:
x - map x location
y - map y location
Returns:
an Iterator over the objects at (x, y). Returns null if there are no objects at (x, y).

objectsAt

public java.util.List objectsAt(Point p)
Returns a list of the objects at location p.

Parameters:
p - the location to examine
Returns:
a List of objects at p (can be null or empty list)

objectsInRect

public java.util.Iterator objectsInRect(int x,
                                        int y,
                                        int w,
                                        int h)
Returns an Iterator over the objects in the specified rectangle.

Parameters:
x - map x location
y - map y location
w - rectangle width
h - rectangle height
Returns:
an Iterator that traverses the objects in the passed rectangle. The Iterator skips quickly over empty locations, so this is no more expensive than (and in fact often faster than) iterating over a rectangle in a dense array.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Don't let them try the put() operation directly, since we use buckets as our values.

Parameters:
key - the key
value - the value
Throws:
java.lang.UnsupportedOperationException - (always)

getBucket

protected Cell getBucket(java.lang.Object key)
Returns the bucket at the specified location.

Parameters:
key - the Point at which to look for a bucket
Returns:
the bucket, or null if none found

getBucket

protected Cell getBucket(int x,
                         int y)
Returns the bucket at the specified location.

Parameters:
x - map x location
y - map y location

makeBucket

protected Cell makeBucket(java.lang.Object key)
Creates and returns a new bucket at the specified location.

Parameters:
key - the Point at which to place the bucket
Returns:
the created bucket

printTree

public void printTree()
Prints out the contents of the map, for debugging.