summaryrefslogtreecommitdiff
path: root/src/interfaces/jdbc/org/postgresql/geometric/PGline.java
blob: 5a108bd0b7a50ffbccdf787de7f1896a9ecbfecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*-------------------------------------------------------------------------
 *
 * PGline.java
 *     This implements a line consisting of two points.
 *
 * Copyright (c) 2003, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/geometric/Attic/PGline.java,v 1.4 2003/03/07 18:39:42 barry Exp $
 *
 *-------------------------------------------------------------------------
 */
package org.postgresql.geometric;

import java.io.Serializable;
import java.sql.SQLException;
import org.postgresql.util.PGobject;
import org.postgresql.util.PGtokenizer;
import org.postgresql.util.PSQLException;

/*
 * 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] + "]";
	}
}