001: /*-------------------------------------------------------------------------
002: *
003: * Copyright (c) 2003-2005, PostgreSQL Global Development Group
004: *
005: * IDENTIFICATION
006: * $PostgreSQL: pgjdbc/org/postgresql/geometric/PGcircle.java,v 1.16 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.io.Serializable;
019: import java.sql.SQLException;
020:
021: /**
022: * This represents org.postgresql's circle datatype, consisting of a point
023: * and a radius
024: */
025: public class PGcircle extends PGobject implements Serializable,
026: Cloneable {
027: /**
028: * This is the center point
029: */
030: public PGpoint center;
031:
032: /**
033: * This is the radius
034: */
035: public double radius;
036:
037: /**
038: * @param x coordinate of center
039: * @param y coordinate of center
040: * @param r radius of circle
041: */
042: public PGcircle(double x, double y, double r) {
043: this (new PGpoint(x, y), r);
044: }
045:
046: /**
047: * @param c PGpoint describing the circle's center
048: * @param r radius of circle
049: */
050: public PGcircle(PGpoint c, double r) {
051: this ();
052: this .center = c;
053: this .radius = r;
054: }
055:
056: /**
057: * @param s definition of the circle in PostgreSQL's syntax.
058: * @exception SQLException on conversion failure
059: */
060: public PGcircle(String s) throws SQLException {
061: this ();
062: setValue(s);
063: }
064:
065: /**
066: * This constructor is used by the driver.
067: */
068: public PGcircle() {
069: setType("circle");
070: }
071:
072: /**
073: * @param s definition of the circle in PostgreSQL's syntax.
074: * @exception SQLException on conversion failure
075: */
076: public void setValue(String s) throws SQLException {
077: PGtokenizer t = new PGtokenizer(PGtokenizer.removeAngle(s), ',');
078: if (t.getSize() != 2)
079: throw new PSQLException(GT.tr(
080: "Conversion to type {0} failed: {1}.",
081: new Object[] { type, s }),
082: PSQLState.DATA_TYPE_MISMATCH);
083:
084: try {
085: center = new PGpoint(t.getToken(0));
086: radius = Double.valueOf(t.getToken(1)).doubleValue();
087: } catch (NumberFormatException e) {
088: throw new PSQLException(GT.tr(
089: "Conversion to type {0} failed: {1}.",
090: new Object[] { type, s }),
091: PSQLState.DATA_TYPE_MISMATCH, e);
092: }
093: }
094:
095: /**
096: * @param obj Object to compare with
097: * @return true if the two circles are identical
098: */
099: public boolean equals(Object obj) {
100: if (obj instanceof PGcircle) {
101: PGcircle p = (PGcircle) obj;
102: return p.center.equals(center) && p.radius == radius;
103: }
104: return false;
105: }
106:
107: public int hashCode() {
108: long v = Double.doubleToLongBits(radius);
109: return (int) (center.hashCode() ^ v ^ (v >>> 32));
110: }
111:
112: public Object clone() throws CloneNotSupportedException {
113: PGcircle newPGcircle = (PGcircle) super .clone();
114: if (newPGcircle.center != null)
115: newPGcircle.center = (PGpoint) newPGcircle.center.clone();
116: return newPGcircle;
117: }
118:
119: /**
120: * @return the PGcircle in the syntax expected by org.postgresql
121: */
122: public String getValue() {
123: return "<" + center + "," + radius + ">";
124: }
125: }
|