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: Coordinate.java,v 1.8 2005/09/10 14:05:52 mat007 Exp $
29   */
30  
31  package jtge.util.grid;
32  
33  import java.io.Serializable;
34  
35  /***
36   * Represents a coordinate in a two dimensions space.
37   * <p>
38   * FIXME this class is responsible for the cycles in jtge.util.grid
39   *
40   * @author Jean-Laurent Fabre de Morlhon
41   * @version $Id: Coordinate.java,v 1.8 2005/09/10 14:05:52 mat007 Exp $
42   */
43  public final class Coordinate implements Cloneable, Serializable
44  {
45      private static final long serialVersionUID = 1855298069277238723L;
46      /***
47       * The origin of the coordinate system.
48       */
49      public static final Coordinate ORIGIN = new Coordinate( 0, 0 );
50      private int x;
51      private int y;
52  
53      /***
54       * Create a coordinate.
55       * <p>
56       * x and y are initialized with the origin.
57       */
58      public Coordinate()
59      {
60          this.x = ORIGIN.x;
61          this.y = ORIGIN.y;
62      }
63  
64      /***
65       * Create a coordinate with the supplied x and y.
66       *
67       * @param x the x to set.
68       * @param y the y to set.
69       */
70      public Coordinate( final int x, final int y )
71      {
72          this.x = x;
73          this.y = y;
74      }
75  
76      /***
77       * Getter for the x field.
78       *
79       * @return the x of the current coordinate
80       */
81      public int getX()
82      {
83          return x;
84      }
85  
86      /***
87       * Getter for the y field.
88       *
89       * @return the y of the current coordinate
90       */
91      public int getY()
92      {
93          return y;
94      }
95  
96      /***
97       * Setter for the x field.
98       *
99       * @param x the x to set
100      */
101     public void setX( final int x )
102     {
103         this.x = x;
104     }
105 
106     /***
107      * Setter for the y field.
108      *
109      * @param y the y to set
110      */
111     public void setY( final int y )
112     {
113         this.y = y;
114     }
115 
116     /***
117      * {@inheritDoc}
118      */
119     public boolean equals( final Object object )
120     {
121         if( !(object instanceof Coordinate) )
122             return false;
123         Coordinate stranger = (Coordinate)object;
124         return x == stranger.x && y == stranger.y;
125     }
126 
127     /***
128      * {@inheritDoc}
129      */
130     public int hashCode()
131     {
132         // Note : 851 = 23 * 37
133         // See : http://www.javapractices.com/Topic28.cjp
134         return (851 + x) * 37 + y;
135     }
136 
137     /***
138      * {@inheritDoc}
139      */
140     public String toString()
141     {
142         return x + "," + y;
143     }
144 
145     /***
146      * {@inheritDoc}
147      */
148     public Object clone()
149     {
150         return new Coordinate( x, y );
151     }
152 }