001: /*-------------------------------------------------------------------------
002: *
003: * Copyright (c) 2003-2005, PostgreSQL Global Development Group
004: *
005: * IDENTIFICATION
006: * $PostgreSQL: pgjdbc/org/postgresql/geometric/PGpath.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 implements a path (a multiple segmented line, which may be closed)
023: */
024: public class PGpath extends PGobject implements Serializable, Cloneable {
025: /**
026: * True if the path is open, false if closed
027: */
028: public boolean open;
029:
030: /**
031: * The points defining this path
032: */
033: public PGpoint points[];
034:
035: /**
036: * @param points the PGpoints that define the path
037: * @param open True if the path is open, false if closed
038: */
039: public PGpath(PGpoint[] points, boolean open) {
040: this ();
041: this .points = points;
042: this .open = open;
043: }
044:
045: /**
046: * Required by the driver
047: */
048: public PGpath() {
049: setType("path");
050: }
051:
052: /**
053: * @param s definition of the path in PostgreSQL's syntax.
054: * @exception SQLException on conversion failure
055: */
056: public PGpath(String s) throws SQLException {
057: this ();
058: setValue(s);
059: }
060:
061: /**
062: * @param s Definition of the path in PostgreSQL's syntax
063: * @exception SQLException on conversion failure
064: */
065: public void setValue(String s) throws SQLException {
066: // First test to see if were open
067: if (s.startsWith("[") && s.endsWith("]")) {
068: open = true;
069: s = PGtokenizer.removeBox(s);
070: } else if (s.startsWith("(") && s.endsWith(")")) {
071: open = false;
072: s = PGtokenizer.removePara(s);
073: } else
074: throw new PSQLException(GT.tr(
075: "Cannot tell if path is open or closed: {0}.", s),
076: PSQLState.DATA_TYPE_MISMATCH);
077:
078: PGtokenizer t = new PGtokenizer(s, ',');
079: int npoints = t.getSize();
080: points = new PGpoint[npoints];
081: for (int p = 0; p < npoints; p++)
082: points[p] = new PGpoint(t.getToken(p));
083: }
084:
085: /**
086: * @param obj Object to compare with
087: * @return true if the two paths are identical
088: */
089: public boolean equals(Object obj) {
090: if (obj instanceof PGpath) {
091: PGpath p = (PGpath) obj;
092:
093: if (p.points.length != points.length)
094: return false;
095:
096: if (p.open != open)
097: return false;
098:
099: for (int i = 0; i < points.length; i++)
100: if (!points[i].equals(p.points[i]))
101: return false;
102:
103: return true;
104: }
105: return false;
106: }
107:
108: public int hashCode() {
109: // XXX not very good..
110: int hash = 0;
111: for (int i = 0; i < points.length && i < 5; ++i) {
112: hash = hash ^ points[i].hashCode();
113: }
114: return hash;
115: }
116:
117: public Object clone() throws CloneNotSupportedException {
118: PGpath newPGpath = (PGpath) super .clone();
119: if (newPGpath.points != null) {
120: newPGpath.points = (PGpoint[]) newPGpath.points.clone();
121: for (int i = 0; i < newPGpath.points.length; ++i)
122: newPGpath.points[i] = (PGpoint) newPGpath.points[i]
123: .clone();
124: }
125: return newPGpath;
126: }
127:
128: /**
129: * This returns the path in the syntax expected by org.postgresql
130: */
131: public String getValue() {
132: StringBuffer b = new StringBuffer(open ? "[" : "(");
133:
134: for (int p = 0; p < points.length; p++) {
135: if (p > 0)
136: b.append(",");
137: b.append(points[p].toString());
138: }
139: b.append(open ? "]" : ")");
140:
141: return b.toString();
142: }
143:
144: public boolean isOpen() {
145: return open;
146: }
147:
148: public boolean isClosed() {
149: return !open;
150: }
151:
152: public void closePath() {
153: open = false;
154: }
155:
156: public void openPath() {
157: open = true;
158: }
159: }
|