View Javadoc

1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: Grid.java,v 1.5 2005/09/09 22:12:34 mat007 Exp $
29   */
30  
31  package jtge.util.grid;
32  
33  import java.util.Hashtable;
34  import java.util.Iterator;
35  import java.util.Vector;
36  import jtge.util.grid.direction.IDirection;
37  import jtge.util.grid.directiongroup.IDirectionGroup;
38  import jtge.util.grid.torus.ITorus;
39  import jtge.util.grid.torus.NullTorus;
40  
41  /***
42   * Implements a grid.
43   *
44   * @author Jean-Laurent Fabre de Morlhon
45   * @version $Id: Grid.java,v 1.5 2005/09/09 22:12:34 mat007 Exp $
46   */
47  public class Grid implements IGrid
48  {
49      private final int width;
50      private final int height;
51      private final Hashtable map;
52      private final IDirectionGroup directionGroup;
53      private final ITorus torus;
54  
55      /***
56       * Create a grid.
57       *
58       * @param width the grid width in tiles
59       * @param height the grid height in tiles
60       * @param directionGroup the direction group strategy
61       * @param torus the torus
62       */
63      public Grid( final int width, final int height, final IDirectionGroup directionGroup, final ITorus torus )
64      {
65          if( width <= 0 )
66              throw new IllegalArgumentException( "invalid width" );
67          if( height <= 0 )
68              throw new IllegalArgumentException( "invalid height" );
69          if( directionGroup == null )
70              throw new IllegalArgumentException( "argument 'directionGroup' is null" );
71          if( torus == null )
72              throw new IllegalArgumentException( "argument 'torus' is null" );
73          this.width = width;
74          this.height = height;
75          this.map = new Hashtable();
76          this.directionGroup = directionGroup;
77          this.torus = torus;
78      }
79  
80      /***
81       * Create a grid.
82       *
83       * @param width the grid width in tiles
84       * @param height the grid height in tiles
85       * @param directionGroup the direction group strategy
86       */
87      public Grid( final int width, final int height, final IDirectionGroup directionGroup )
88      {
89          this( width, height, directionGroup, new NullTorus() );
90      }
91  
92      /***
93       * {@inheritDoc}
94       */
95      public final int getWidth()
96      {
97          return this.width;
98      }
99  
100     /***
101      * {@inheritDoc}
102      */
103     public final int getHeight()
104     {
105         return this.height;
106     }
107 
108     /***
109      * {@inheritDoc}
110      */
111     public final int size()
112     {
113         return width * height;
114     }
115 
116     /***
117      * {@inheritDoc}
118      */
119     public final void setTile( final ITile tile, final Coordinate coordinate )
120     {
121         if( !isValid( coordinate ) )
122             throw new OutOfBoundException( coordinate );
123         map.put( coordinate, tile );
124     }
125 
126     /***
127      * {@inheritDoc}
128      */
129     public final ITile getTile( final Coordinate coordinate )
130     {
131         if( coordinate == null )
132             return null;
133         return (ITile)map.get( coordinate );
134     }
135 
136     /***
137      * {@inheritDoc}
138      */
139     public final boolean isValid( final Coordinate coordinate )
140     {
141         if( coordinate == null )
142             return false;
143         return coordinate.getX() >= 0 && coordinate.getX() < width && coordinate.getY() >= 0
144                 && coordinate.getY() < height;
145     }
146 
147     /***
148      * {@inheritDoc}
149      */
150     public final Coordinate getAdjacent( final Coordinate coordinate, final IDirection direction )
151     {
152         final Coordinate result = torus.transform( direction.transform( coordinate, directionGroup ) );
153         if( !isValid( result ) )
154             return null;
155         return result;
156     }
157 
158     /***
159      * {@inheritDoc}
160      */
161     public final Iterator adjacentIterator( final Coordinate coordinate )
162     {
163         final Vector coordinates = new Vector();
164         final Iterator iterator = directionGroup.iterator();
165         while( iterator.hasNext() )
166         {
167             final Coordinate current = getAdjacent( coordinate, (IDirection)iterator.next() );
168             if( current != null )
169                 coordinates.add( current );
170         }
171         return coordinates.iterator();
172     }
173 
174     /***
175      * {@inheritDoc}
176      */
177     public final Iterator adjacentTileIterator( final Coordinate coordinate )
178     {
179         final Vector tiles = new Vector();
180         final Iterator iterator = adjacentIterator( coordinate );
181         while( iterator.hasNext() )
182         {
183             final ITile tile = getTile( (Coordinate)iterator.next() );
184             if( tile != null )
185                 tiles.add( tile );
186         }
187         return tiles.iterator();
188     }
189 
190     /***
191      * {@inheritDoc}
192      */
193     public final Iterator linearIterator()
194     {
195         return new LinearIterator( this );
196     }
197 
198     /***
199      * {@inheritDoc}
200      */
201     public final Iterator linearTileIterator()
202     {
203         return new LinearTileIterator( this );
204     }
205 }