summaryrefslogtreecommitdiff
path: root/src/interfaces/jdbc/postgresql/PGpoint.java
blob: 77a61730f93cb1964a249b80e282eae1a22eb304 (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
/**
 *
 * This implements a version of java.awt.Point, except it uses double
 * to represent the coordinates.
 *
 * It maps to the point datatype in postgresql.
 */

package postgresql;

import java.awt.Point;
import java.io.*;
import java.sql.*;

public class PGpoint  implements Serializable
{
  /**
   * These are the coordinates.
   * These are public, because their equivalents in java.awt.Point are
   */
  public double x,y;
  
  public PGpoint(double x,double y)
  {
    this.x = x;
    this.y = y;
  }
  
  public PGpoint(PGpoint p)
  {
    this(p.x,p.y);
  }
  
  /**
   * This constructor is used by the driver.
   */
  public PGpoint(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 SQLException("conversion of point failed - "+e.toString());
    }
  }
  
  public boolean equals(Object obj)
  {
    PGpoint p = (PGpoint)obj;
    return x == p.x && y == p.y;
  }
  
  /**
   * This returns the point in the syntax expected by postgresql
   */
  public String toString()
  {
    return "("+x+","+y+")";
  }
  
  public void translate(int x,int y)
  {
    translate((double)x,(double)y);
  }
  
  public void translate(double x,double y)
  {
    this.x += x;
    this.y += y;
  }
  
  public void move(int x,int y)
  {
    setLocation(x,y);
  }
  
  public void move(double x,double y)
  {
    this.x = x;
    this.y = y;
  }
  
  // refer to java.awt.Point for description of this
  public void setLocation(int x,int y)
  {
    move((double)x,(double)y);
  }
  
  // refer to java.awt.Point for description of this
  public void setLocation(Point p)
  {
    setLocation(p.x,p.y);
  }
  
}