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$
29 */
30
31 package palmed.edit.util;
32
33 import java.io.DataInputStream;
34 import java.io.DataOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import palmed.io.ISerializable;
39
40 /***
41 * This class encapsulates a two integers coordinate.
42 *
43 * @author Mathieu Champlon
44 * @version $Revision$ $Date$
45 */
46 public final class Coordinate implements ISerializable
47 {
48 /***
49 * The size of an integer in bytes.
50 */
51 private static final int SIZE_OF_INT = 16;
52 /***
53 * The abcissa value.
54 */
55 public int x_;
56 /***
57 * The ordinate value.
58 */
59 public int y_;
60
61 /***
62 * Create a coordinate (0,0).
63 */
64 public Coordinate()
65 {
66 }
67
68 /***
69 * Create a coordinate (x,y).
70 *
71 * @param x abcissa
72 * @param y ordinate
73 */
74 public Coordinate( final int x, final int y )
75 {
76 x_ = x;
77 y_ = y;
78 }
79
80 /***
81 * {@inheritDoc}
82 */
83 public boolean equals( final Object object )
84 {
85 if( object instanceof Coordinate )
86 {
87 Coordinate coord = (Coordinate)object;
88 return coord.x_ == x_ && coord.y_ == y_;
89 }
90 return false;
91 }
92
93 /***
94 * {@inheritDoc}
95 */
96 public int hashCode()
97 {
98 return (x_ << SIZE_OF_INT) | y_;
99 }
100
101 /***
102 * Test if the coordinate is less than another.
103 *
104 * @param other the other coordinate
105 * @return whether the current coordinate is less than the other or not
106 */
107 public boolean lessThan( final Coordinate other )
108 {
109 if( other.y_ > y_ )
110 return true;
111 if( other.y_ < y_ )
112 return false;
113 return other.x_ > x_;
114 }
115
116 /***
117 * {@inheritDoc}
118 */
119 public String toString()
120 {
121 return "(" + x_ + ',' + y_ + ")";
122 }
123
124 /***
125 * {@inheritDoc}
126 */
127 public void unmarshall( final InputStream stream ) throws IOException
128 {
129 final DataInputStream input = new DataInputStream( stream );
130 x_ = input.readInt();
131 y_ = input.readInt();
132 }
133
134 /***
135 * {@inheritDoc}
136 */
137 public void marshall( final OutputStream stream ) throws IOException
138 {
139 final DataOutputStream output = new DataOutputStream( stream );
140 output.writeInt( x_ );
141 output.writeInt( y_ );
142 }
143 }