Source code: postgresql/geometric/PGpoint.java
1 package postgresql.geometric;
2
3 import java.awt.Point;
4 import java.io.*;
5 import java.sql.*;
6
7 import postgresql.util.*;
8
9 /**
10 * This implements a version of java.awt.Point, except it uses double
11 * to represent the coordinates.
12 *
13 * <p>It maps to the point datatype in postgresql.
14 */
15 public class PGpoint extends PGobject implements Serializable,Cloneable
16 {
17 /**
18 * The X coordinate of the point
19 */
20 public double x;
21
22 /**
23 * The Y coordinate of the point
24 */
25 public double y;
26
27 /**
28 * @param x coordinate
29 * @param y coordinate
30 */
31 public PGpoint(double x,double y)
32 {
33 this();
34 this.x = x;
35 this.y = y;
36 }
37
38 /**
39 * This is called mainly from the other geometric types, when a
40 * point is imbeded within their definition.
41 *
42 * @param value Definition of this point in PostgreSQL's syntax
43 */
44 public PGpoint(String value) throws SQLException
45 {
46 this();
47 setValue(value);
48 }
49
50 /**
51 * Required by the driver
52 */
53 public PGpoint()
54 {
55 setType("point");
56 }
57
58 /**
59 * @param s Definition of this point in PostgreSQL's syntax
60 * @exception SQLException on conversion failure
61 */
62 public void setValue(String s) throws SQLException
63 {
64 PGtokenizer t = new PGtokenizer(PGtokenizer.removePara(s),',');
65 try {
66 x = Double.valueOf(t.getToken(0)).doubleValue();
67 y = Double.valueOf(t.getToken(1)).doubleValue();
68 } catch(NumberFormatException e) {
69 throw new PSQLException("postgresql.geo.point",e.toString());
70 }
71 }
72
73 /**
74 * @param obj Object to compare with
75 * @return true if the two boxes are identical
76 */
77 public boolean equals(Object obj)
78 {
79 if(obj instanceof PGpoint) {
80 PGpoint p = (PGpoint)obj;
81 return x == p.x && y == p.y;
82 }
83 return false;
84 }
85
86 /**
87 * This must be overidden to allow the object to be cloned
88 */
89 public Object clone()
90 {
91 return new PGpoint(x,y);
92 }
93
94 /**
95 * @return the PGpoint in the syntax expected by postgresql
96 */
97 public String getValue()
98 {
99 return "("+x+","+y+")";
100 }
101
102 /**
103 * Translate the point with the supplied amount.
104 * @param x integer amount to add on the x axis
105 * @param y integer amount to add on the y axis
106 */
107 public void translate(int x,int y)
108 {
109 translate((double)x,(double)y);
110 }
111
112 /**
113 * Translate the point with the supplied amount.
114 * @param x double amount to add on the x axis
115 * @param y double amount to add on the y axis
116 */
117 public void translate(double x,double y)
118 {
119 this.x += x;
120 this.y += y;
121 }
122
123 /**
124 * Moves the point to the supplied coordinates.
125 * @param x integer coordinate
126 * @param y integer coordinate
127 */
128 public void move(int x,int y)
129 {
130 setLocation(x,y);
131 }
132
133 /**
134 * Moves the point to the supplied coordinates.
135 * @param x double coordinate
136 * @param y double coordinate
137 */
138 public void move(double x,double y)
139 {
140 this.x = x;
141 this.y = y;
142 }
143
144 /**
145 * Moves the point to the supplied coordinates.
146 * refer to java.awt.Point for description of this
147 * @param x integer coordinate
148 * @param y integer coordinate
149 * @see java.awt.Point
150 */
151 public void setLocation(int x,int y)
152 {
153 move((double)x,(double)y);
154 }
155
156 /**
157 * Moves the point to the supplied java.awt.Point
158 * refer to java.awt.Point for description of this
159 * @param p Point to move to
160 * @see java.awt.Point
161 */
162 public void setLocation(Point p)
163 {
164 setLocation(p.x,p.y);
165 }
166
167 }