001: /*-------------------------------------------------------------------------
002: *
003: * Copyright (c) 2003-2005, PostgreSQL Global Development Group
004: *
005: * IDENTIFICATION
006: * $PostgreSQL: pgjdbc/org/postgresql/geometric/PGpoint.java,v 1.15 2007/07/16 15:02:53 jurka Exp $
007: *
008: *-------------------------------------------------------------------------
009: */
010: package org.postgresql.geometric;
011:
012: import org.postgresql.util.GT;
013: import org.postgresql.util.PGobject;
014: import org.postgresql.util.PGtokenizer;
015: import org.postgresql.util.PSQLException;
016: import org.postgresql.util.PSQLState;
017:
018: import java.awt.Point;
019: import java.io.Serializable;
020: import java.sql.SQLException;
021:
022: /**
023: * It maps to the point datatype in org.postgresql.
024: *
025: * This implements a version of java.awt.Point, except it uses double
026: * to represent the coordinates.
027: */
028: public class PGpoint extends PGobject implements Serializable,
029: Cloneable {
030: /**
031: * The X coordinate of the point
032: */
033: public double x;
034:
035: /**
036: * The Y coordinate of the point
037: */
038: public double y;
039:
040: /**
041: * @param x coordinate
042: * @param y coordinate
043: */
044: public PGpoint(double x, double y) {
045: this ();
046: this .x = x;
047: this .y = y;
048: }
049:
050: /**
051: * This is called mainly from the other geometric types, when a
052: * point is embedded within their definition.
053: *
054: * @param value Definition of this point in PostgreSQL's syntax
055: */
056: public PGpoint(String value) throws SQLException {
057: this ();
058: setValue(value);
059: }
060:
061: /**
062: * Required by the driver
063: */
064: public PGpoint() {
065: setType("point");
066: }
067:
068: /**
069: * @param s Definition of this point in PostgreSQL's syntax
070: * @exception SQLException on conversion failure
071: */
072: public void setValue(String s) throws SQLException {
073: PGtokenizer t = new PGtokenizer(PGtokenizer.removePara(s), ',');
074: try {
075: x = Double.valueOf(t.getToken(0)).doubleValue();
076: y = Double.valueOf(t.getToken(1)).doubleValue();
077: } catch (NumberFormatException e) {
078: throw new PSQLException(GT.tr(
079: "Conversion to type {0} failed: {1}.",
080: new Object[] { type, s }),
081: PSQLState.DATA_TYPE_MISMATCH, e);
082: }
083: }
084:
085: /**
086: * @param obj Object to compare with
087: * @return true if the two points are identical
088: */
089: public boolean equals(Object obj) {
090: if (obj instanceof PGpoint) {
091: PGpoint p = (PGpoint) obj;
092: return x == p.x && y == p.y;
093: }
094: return false;
095: }
096:
097: public int hashCode() {
098: long v1 = Double.doubleToLongBits(x);
099: long v2 = Double.doubleToLongBits(y);
100: return (int) (v1 ^ v2 ^ (v1 >>> 32) ^ (v2 >>> 32));
101: }
102:
103: /**
104: * @return the PGpoint in the syntax expected by org.postgresql
105: */
106: public String getValue() {
107: return "(" + x + "," + y + ")";
108: }
109:
110: /**
111: * Translate the point by the supplied amount.
112: * @param x integer amount to add on the x axis
113: * @param y integer amount to add on the y axis
114: */
115: public void translate(int x, int y) {
116: translate((double) x, (double) y);
117: }
118:
119: /**
120: * Translate the point by the supplied amount.
121: * @param x double amount to add on the x axis
122: * @param y double amount to add on the y axis
123: */
124: public void translate(double x, double y) {
125: this .x += x;
126: this .y += y;
127: }
128:
129: /**
130: * Moves the point to the supplied coordinates.
131: * @param x integer coordinate
132: * @param y integer coordinate
133: */
134: public void move(int x, int y) {
135: setLocation(x, y);
136: }
137:
138: /**
139: * Moves the point to the supplied coordinates.
140: * @param x double coordinate
141: * @param y double coordinate
142: */
143: public void move(double x, double y) {
144: this .x = x;
145: this .y = y;
146: }
147:
148: /**
149: * Moves the point to the supplied coordinates.
150: * refer to java.awt.Point for description of this
151: * @param x integer coordinate
152: * @param y integer coordinate
153: * @see java.awt.Point
154: */
155: public void setLocation(int x, int y) {
156: move((double) x, (double) y);
157: }
158:
159: /**
160: * Moves the point to the supplied java.awt.Point
161: * refer to java.awt.Point for description of this
162: * @param p Point to move to
163: * @see java.awt.Point
164: */
165: public void setLocation(Point p) {
166: setLocation(p.x, p.y);
167: }
168:
169: }
|