1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-28 18:48:04 +03:00

Another attempt at 7.0

This commit is contained in:
Peter Mount
2000-04-17 20:07:56 +00:00
parent aafff4af16
commit 25dadc8514
38 changed files with 17359 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
package org.postgresql.geometric;
import java.io.*;
import java.sql.*;
import org.postgresql.util.*;
/**
* This represents the box datatype within org.postgresql.
*/
public class PGbox extends PGobject implements Serializable,Cloneable
{
/**
* These are the two points.
*/
public PGpoint point[] = new PGpoint[2];
/**
* @param x1 first x coordinate
* @param y1 first y coordinate
* @param x2 second x coordinate
* @param y2 second y coordinate
*/
public PGbox(double x1,double y1,double x2,double y2)
{
this();
this.point[0] = new PGpoint(x1,y1);
this.point[1] = new PGpoint(x2,y2);
}
/**
* @param p1 first point
* @param p2 second point
*/
public PGbox(PGpoint p1,PGpoint p2)
{
this();
this.point[0] = p1;
this.point[1] = p2;
}
/**
* @param s Box definition in PostgreSQL syntax
* @exception SQLException if definition is invalid
*/
public PGbox(String s) throws SQLException
{
this();
setValue(s);
}
/**
* Required constructor
*/
public PGbox()
{
setType("box");
}
/**
* This method sets the value of this object. It should be overidden,
* but still called by subclasses.
*
* @param value a string representation of the value of the object
* @exception SQLException thrown if value is invalid for this type
*/
public void setValue(String value) throws SQLException
{
PGtokenizer t = new PGtokenizer(value,',');
if(t.getSize() != 2)
throw new PSQLException("postgresql.geo.box",value);
point[0] = new PGpoint(t.getToken(0));
point[1] = new PGpoint(t.getToken(1));
}
/**
* @param obj Object to compare with
* @return true if the two boxes are identical
*/
public boolean equals(Object obj)
{
if(obj instanceof PGbox) {
PGbox p = (PGbox)obj;
return (p.point[0].equals(point[0]) && p.point[1].equals(point[1])) ||
(p.point[0].equals(point[1]) && p.point[1].equals(point[0]));
}
return false;
}
/**
* This must be overidden to allow the object to be cloned
*/
public Object clone()
{
return new PGbox((PGpoint)point[0].clone(),(PGpoint)point[1].clone());
}
/**
* @return the PGbox in the syntax expected by org.postgresql
*/
public String getValue()
{
return point[0].toString()+","+point[1].toString();
}
}

View File

@@ -0,0 +1,108 @@
package org.postgresql.geometric;
import java.io.*;
import java.sql.*;
import org.postgresql.util.*;
/**
* This represents org.postgresql's circle datatype, consisting of a point and
* a radius
*/
public class PGcircle extends PGobject implements Serializable,Cloneable
{
/**
* This is the centre point
*/
public PGpoint center;
/**
* This is the radius
*/
double radius;
/**
* @param x coordinate of centre
* @param y coordinate of centre
* @param r radius of circle
*/
public PGcircle(double x,double y,double r)
{
this(new PGpoint(x,y),r);
}
/**
* @param c PGpoint describing the circle's centre
* @param r radius of circle
*/
public PGcircle(PGpoint c,double r)
{
this();
this.center = c;
this.radius = r;
}
/**
* @param s definition of the circle in PostgreSQL's syntax.
* @exception SQLException on conversion failure
*/
public PGcircle(String s) throws SQLException
{
this();
setValue(s);
}
/**
* This constructor is used by the driver.
*/
public PGcircle()
{
setType("circle");
}
/**
* @param s definition of the circle in PostgreSQL's syntax.
* @exception SQLException on conversion failure
*/
public void setValue(String s) throws SQLException
{
PGtokenizer t = new PGtokenizer(PGtokenizer.removeAngle(s),',');
if(t.getSize() != 2)
throw new PSQLException("postgresql.geo.circle",s);
try {
center = new PGpoint(t.getToken(0));
radius = Double.valueOf(t.getToken(1)).doubleValue();
} catch(NumberFormatException e) {
throw new PSQLException("postgresql.geo.circle",e);
}
}
/**
* @param obj Object to compare with
* @return true if the two boxes are identical
*/
public boolean equals(Object obj)
{
if(obj instanceof PGcircle) {
PGcircle p = (PGcircle)obj;
return p.center.equals(center) && p.radius==radius;
}
return false;
}
/**
* This must be overidden to allow the object to be cloned
*/
public Object clone()
{
return new PGcircle((PGpoint)center.clone(),radius);
}
/**
* @return the PGcircle in the syntax expected by org.postgresql
*/
public String getValue()
{
return "<"+center+","+radius+">";
}
}

View File

@@ -0,0 +1,103 @@
package org.postgresql.geometric;
import java.io.*;
import java.sql.*;
import org.postgresql.util.*;
/**
* This implements a line consisting of two points.
*
* Currently line is not yet implemented in the backend, but this class
* ensures that when it's done were ready for it.
*/
public class PGline extends PGobject implements Serializable,Cloneable
{
/**
* These are the two points.
*/
public PGpoint point[] = new PGpoint[2];
/**
* @param x1 coordinate for first point
* @param y1 coordinate for first point
* @param x2 coordinate for second point
* @param y2 coordinate for second point
*/
public PGline(double x1,double y1,double x2,double y2)
{
this(new PGpoint(x1,y1),new PGpoint(x2,y2));
}
/**
* @param p1 first point
* @param p2 second point
*/
public PGline(PGpoint p1,PGpoint p2)
{
this();
this.point[0] = p1;
this.point[1] = p2;
}
/**
* @param s definition of the circle in PostgreSQL's syntax.
* @exception SQLException on conversion failure
*/
public PGline(String s) throws SQLException
{
this();
setValue(s);
}
/**
* reuired by the driver
*/
public PGline()
{
setType("line");
}
/**
* @param s Definition of the line segment in PostgreSQL's syntax
* @exception SQLException on conversion failure
*/
public void setValue(String s) throws SQLException
{
PGtokenizer t = new PGtokenizer(PGtokenizer.removeBox(s),',');
if(t.getSize() != 2)
throw new PSQLException("postgresql.geo.line",s);
point[0] = new PGpoint(t.getToken(0));
point[1] = new PGpoint(t.getToken(1));
}
/**
* @param obj Object to compare with
* @return true if the two boxes are identical
*/
public boolean equals(Object obj)
{
if(obj instanceof PGline) {
PGline p = (PGline)obj;
return (p.point[0].equals(point[0]) && p.point[1].equals(point[1])) ||
(p.point[0].equals(point[1]) && p.point[1].equals(point[0]));
}
return false;
}
/**
* This must be overidden to allow the object to be cloned
*/
public Object clone()
{
return new PGline((PGpoint)point[0].clone(),(PGpoint)point[1].clone());
}
/**
* @return the PGline in the syntax expected by org.postgresql
*/
public String getValue()
{
return "["+point[0]+","+point[1]+"]";
}
}

View File

@@ -0,0 +1,100 @@
package org.postgresql.geometric;
import java.io.*;
import java.sql.*;
import org.postgresql.util.*;
/**
* This implements a lseg (line segment) consisting of two points
*/
public class PGlseg extends PGobject implements Serializable,Cloneable
{
/**
* These are the two points.
*/
public PGpoint point[] = new PGpoint[2];
/**
* @param x1 coordinate for first point
* @param y1 coordinate for first point
* @param x2 coordinate for second point
* @param y2 coordinate for second point
*/
public PGlseg(double x1,double y1,double x2,double y2)
{
this(new PGpoint(x1,y1),new PGpoint(x2,y2));
}
/**
* @param p1 first point
* @param p2 second point
*/
public PGlseg(PGpoint p1,PGpoint p2)
{
this();
this.point[0] = p1;
this.point[1] = p2;
}
/**
* @param s definition of the circle in PostgreSQL's syntax.
* @exception SQLException on conversion failure
*/
public PGlseg(String s) throws SQLException
{
this();
setValue(s);
}
/**
* reuired by the driver
*/
public PGlseg()
{
setType("lseg");
}
/**
* @param s Definition of the line segment in PostgreSQL's syntax
* @exception SQLException on conversion failure
*/
public void setValue(String s) throws SQLException
{
PGtokenizer t = new PGtokenizer(PGtokenizer.removeBox(s),',');
if(t.getSize() != 2)
throw new PSQLException("postgresql.geo.lseg");
point[0] = new PGpoint(t.getToken(0));
point[1] = new PGpoint(t.getToken(1));
}
/**
* @param obj Object to compare with
* @return true if the two boxes are identical
*/
public boolean equals(Object obj)
{
if(obj instanceof PGlseg) {
PGlseg p = (PGlseg)obj;
return (p.point[0].equals(point[0]) && p.point[1].equals(point[1])) ||
(p.point[0].equals(point[1]) && p.point[1].equals(point[0]));
}
return false;
}
/**
* This must be overidden to allow the object to be cloned
*/
public Object clone()
{
return new PGlseg((PGpoint)point[0].clone(),(PGpoint)point[1].clone());
}
/**
* @return the PGlseg in the syntax expected by org.postgresql
*/
public String getValue()
{
return "["+point[0]+","+point[1]+"]";
}
}

View File

@@ -0,0 +1,145 @@
package org.postgresql.geometric;
import java.io.*;
import java.sql.*;
import org.postgresql.util.*;
/**
* This implements a path (a multiple segmented line, which may be closed)
*/
public class PGpath extends PGobject implements Serializable,Cloneable
{
/**
* True if the path is open, false if closed
*/
public boolean open;
/**
* The points defining this path
*/
public PGpoint points[];
/**
* @param points the PGpoints that define the path
* @param open True if the path is open, false if closed
*/
public PGpath(PGpoint[] points,boolean open)
{
this();
this.points = points;
this.open = open;
}
/**
* Required by the driver
*/
public PGpath()
{
setType("path");
}
/**
* @param s definition of the circle in PostgreSQL's syntax.
* @exception SQLException on conversion failure
*/
public PGpath(String s) throws SQLException
{
this();
setValue(s);
}
/**
* @param s Definition of the path in PostgreSQL's syntax
* @exception SQLException on conversion failure
*/
public void setValue(String s) throws SQLException
{
// First test to see if were open
if(s.startsWith("[") && s.endsWith("]")) {
open = true;
s = PGtokenizer.removeBox(s);
} else if(s.startsWith("(") && s.endsWith(")")) {
open = false;
s = PGtokenizer.removePara(s);
} else
throw new PSQLException("postgresql.geo.path");
PGtokenizer t = new PGtokenizer(s,',');
int npoints = t.getSize();
points = new PGpoint[npoints];
for(int p=0;p<npoints;p++)
points[p] = new PGpoint(t.getToken(p));
}
/**
* @param obj Object to compare with
* @return true if the two boxes are identical
*/
public boolean equals(Object obj)
{
if(obj instanceof PGpath) {
PGpath p = (PGpath)obj;
if(p.points.length != points.length)
return false;
if(p.open != open)
return false;
for(int i=0;i<points.length;i++)
if(!points[i].equals(p.points[i]))
return false;
return true;
}
return false;
}
/**
* This must be overidden to allow the object to be cloned
*/
public Object clone()
{
PGpoint ary[] = new PGpoint[points.length];
for(int i=0;i<points.length;i++)
ary[i]=(PGpoint)points[i].clone();
return new PGpath(ary,open);
}
/**
* This returns the polygon in the syntax expected by org.postgresql
*/
public String getValue()
{
StringBuffer b = new StringBuffer(open?"[":"(");
for(int p=0;p<points.length;p++) {
if(p>0) b.append(",");
b.append(points[p].toString());
}
b.append(open?"]":")");
return b.toString();
}
public boolean isOpen()
{
return open;
}
public boolean isClosed()
{
return !open;
}
public void closePath()
{
open = false;
}
public void openPath()
{
open = true;
}
}

View File

@@ -0,0 +1,167 @@
package org.postgresql.geometric;
import java.awt.Point;
import java.io.*;
import java.sql.*;
import org.postgresql.util.*;
/**
* This implements a version of java.awt.Point, except it uses double
* to represent the coordinates.
*
* <p>It maps to the point datatype in org.postgresql.
*/
public class PGpoint extends PGobject implements Serializable,Cloneable
{
/**
* The X coordinate of the point
*/
public double x;
/**
* The Y coordinate of the point
*/
public double y;
/**
* @param x coordinate
* @param y coordinate
*/
public PGpoint(double x,double y)
{
this();
this.x = x;
this.y = y;
}
/**
* This is called mainly from the other geometric types, when a
* point is imbeded within their definition.
*
* @param value Definition of this point in PostgreSQL's syntax
*/
public PGpoint(String value) throws SQLException
{
this();
setValue(value);
}
/**
* Required by the driver
*/
public PGpoint()
{
setType("point");
}
/**
* @param s Definition of this point in PostgreSQL's syntax
* @exception SQLException on conversion failure
*/
public void setValue(String s) throws SQLException
{
PGtokenizer t = new PGtokenizer(PGtokenizer.removePara(s),',');
try {
x = Double.valueOf(t.getToken(0)).doubleValue();
y = Double.valueOf(t.getToken(1)).doubleValue();
} catch(NumberFormatException e) {
throw new PSQLException("postgresql.geo.point",e.toString());
}
}
/**
* @param obj Object to compare with
* @return true if the two boxes are identical
*/
public boolean equals(Object obj)
{
if(obj instanceof PGpoint) {
PGpoint p = (PGpoint)obj;
return x == p.x && y == p.y;
}
return false;
}
/**
* This must be overidden to allow the object to be cloned
*/
public Object clone()
{
return new PGpoint(x,y);
}
/**
* @return the PGpoint in the syntax expected by org.postgresql
*/
public String getValue()
{
return "("+x+","+y+")";
}
/**
* Translate the point with the supplied amount.
* @param x integer amount to add on the x axis
* @param y integer amount to add on the y axis
*/
public void translate(int x,int y)
{
translate((double)x,(double)y);
}
/**
* Translate the point with the supplied amount.
* @param x double amount to add on the x axis
* @param y double amount to add on the y axis
*/
public void translate(double x,double y)
{
this.x += x;
this.y += y;
}
/**
* Moves the point to the supplied coordinates.
* @param x integer coordinate
* @param y integer coordinate
*/
public void move(int x,int y)
{
setLocation(x,y);
}
/**
* Moves the point to the supplied coordinates.
* @param x double coordinate
* @param y double coordinate
*/
public void move(double x,double y)
{
this.x = x;
this.y = y;
}
/**
* Moves the point to the supplied coordinates.
* refer to java.awt.Point for description of this
* @param x integer coordinate
* @param y integer coordinate
* @see java.awt.Point
*/
public void setLocation(int x,int y)
{
move((double)x,(double)y);
}
/**
* Moves the point to the supplied java.awt.Point
* refer to java.awt.Point for description of this
* @param p Point to move to
* @see java.awt.Point
*/
public void setLocation(Point p)
{
setLocation(p.x,p.y);
}
}

View File

@@ -0,0 +1,105 @@
package org.postgresql.geometric;
import java.io.*;
import java.sql.*;
import org.postgresql.util.*;
/**
* This implements the polygon datatype within PostgreSQL.
*/
public class PGpolygon extends PGobject implements Serializable,Cloneable
{
/**
* The points defining the polygon
*/
public PGpoint points[];
/**
* Creates a polygon using an array of PGpoints
*
* @param points the points defining the polygon
*/
public PGpolygon(PGpoint[] points)
{
this();
this.points = points;
}
/**
* @param s definition of the circle in PostgreSQL's syntax.
* @exception SQLException on conversion failure
*/
public PGpolygon(String s) throws SQLException
{
this();
setValue(s);
}
/**
* Required by the driver
*/
public PGpolygon()
{
setType("polygon");
}
/**
* @param s Definition of the polygon in PostgreSQL's syntax
* @exception SQLException on conversion failure
*/
public void setValue(String s) throws SQLException
{
PGtokenizer t = new PGtokenizer(PGtokenizer.removePara(s),',');
int npoints = t.getSize();
points = new PGpoint[npoints];
for(int p=0;p<npoints;p++)
points[p] = new PGpoint(t.getToken(p));
}
/**
* @param obj Object to compare with
* @return true if the two boxes are identical
*/
public boolean equals(Object obj)
{
if(obj instanceof PGpolygon) {
PGpolygon p = (PGpolygon)obj;
if(p.points.length != points.length)
return false;
for(int i=0;i<points.length;i++)
if(!points[i].equals(p.points[i]))
return false;
return true;
}
return false;
}
/**
* This must be overidden to allow the object to be cloned
*/
public Object clone()
{
PGpoint ary[] = new PGpoint[points.length];
for(int i=0;i<points.length;i++)
ary[i] = (PGpoint)points[i].clone();
return new PGpolygon(ary);
}
/**
* @return the PGpolygon in the syntax expected by org.postgresql
*/
public String getValue()
{
StringBuffer b = new StringBuffer();
b.append("(");
for(int p=0;p<points.length;p++) {
if(p>0) b.append(",");
b.append(points[p].toString());
}
b.append(")");
return b.toString();
}
}