001: /*-------------------------------------------------------------------------
002: *
003: * Copyright (c) 2003-2005, PostgreSQL Global Development Group
004: *
005: * IDENTIFICATION
006: * $PostgreSQL: pgjdbc/org/postgresql/geometric/PGbox.java,v 1.14 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 the box datatype within org.postgresql.
023: */
024: public class PGbox extends PGobject implements Serializable, Cloneable {
025: /**
026: * These are the two points.
027: */
028: public PGpoint point[] = new PGpoint[2];
029:
030: /**
031: * @param x1 first x coordinate
032: * @param y1 first y coordinate
033: * @param x2 second x coordinate
034: * @param y2 second y coordinate
035: */
036: public PGbox(double x1, double y1, double x2, double y2) {
037: this ();
038: this .point[0] = new PGpoint(x1, y1);
039: this .point[1] = new PGpoint(x2, y2);
040: }
041:
042: /**
043: * @param p1 first point
044: * @param p2 second point
045: */
046: public PGbox(PGpoint p1, PGpoint p2) {
047: this ();
048: this .point[0] = p1;
049: this .point[1] = p2;
050: }
051:
052: /**
053: * @param s Box definition in PostgreSQL syntax
054: * @exception SQLException if definition is invalid
055: */
056: public PGbox(String s) throws SQLException {
057: this ();
058: setValue(s);
059: }
060:
061: /**
062: * Required constructor
063: */
064: public PGbox() {
065: setType("box");
066: }
067:
068: /**
069: * This method sets the value of this object. It should be overidden,
070: * but still called by subclasses.
071: *
072: * @param value a string representation of the value of the object
073: * @exception SQLException thrown if value is invalid for this type
074: */
075: public void setValue(String value) throws SQLException {
076: PGtokenizer t = new PGtokenizer(value, ',');
077: if (t.getSize() != 2)
078: throw new PSQLException(GT.tr(
079: "Conversion to type {0} failed: {1}.",
080: new Object[] { type, value }),
081: PSQLState.DATA_TYPE_MISMATCH);
082:
083: point[0] = new PGpoint(t.getToken(0));
084: point[1] = new PGpoint(t.getToken(1));
085: }
086:
087: /**
088: * @param obj Object to compare with
089: * @return true if the two boxes are identical
090: */
091: public boolean equals(Object obj) {
092: if (obj instanceof PGbox) {
093: PGbox p = (PGbox) obj;
094:
095: // Same points.
096: if (p.point[0].equals(point[0])
097: && p.point[1].equals(point[1]))
098: return true;
099:
100: // Points swapped.
101: if (p.point[0].equals(point[1])
102: && p.point[1].equals(point[0]))
103: return true;
104:
105: // Using the opposite two points of the box:
106: // (x1,y1),(x2,y2) -> (x1,y2),(x2,y1)
107: if (p.point[0].x == point[0].x
108: && p.point[0].y == point[1].y
109: && p.point[1].x == point[1].x
110: && p.point[1].y == point[0].y)
111: return true;
112:
113: // Using the opposite two points of the box, and the points are swapped
114: // (x1,y1),(x2,y2) -> (x2,y1),(x1,y2)
115: if (p.point[0].x == point[1].x
116: && p.point[0].y == point[0].y
117: && p.point[1].x == point[0].x
118: && p.point[1].y == point[1].y)
119: return true;
120: }
121:
122: return false;
123: }
124:
125: public int hashCode() {
126: // This relies on the behaviour of point's hashcode being an exclusive-OR of
127: // its X and Y components; we end up with an exclusive-OR of the two X and
128: // two Y components, which is equal whenever equals() would return true
129: // since xor is commutative.
130: return point[0].hashCode() ^ point[1].hashCode();
131: }
132:
133: public Object clone() throws CloneNotSupportedException {
134: PGbox newPGbox = (PGbox) super .clone();
135: if (newPGbox.point != null) {
136: newPGbox.point = (PGpoint[]) newPGbox.point.clone();
137: for (int i = 0; i < newPGbox.point.length; ++i)
138: if (newPGbox.point[i] != null)
139: newPGbox.point[i] = (PGpoint) newPGbox.point[i]
140: .clone();
141: }
142: return newPGbox;
143: }
144:
145: /**
146: * @return the PGbox in the syntax expected by org.postgresql
147: */
148: public String getValue() {
149: return point[0].toString() + "," + point[1].toString();
150: }
151: }
|