summaryrefslogtreecommitdiff
path: root/src/interfaces/jdbc/postgresql/PGpath.java
blob: d87bbedbaa434aa2cc0a238bcdf4d3afd815e2ef (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
/**
 *
 * This implements a path (a multiple segmented line, which may be closed)
 *
 */

package postgresql;

import java.io.*;
import java.sql.*;

public class PGpath implements Serializable
{
  public int npoints;
  public boolean open;
  public PGpoint point[];
  
  public PGpath(int num,PGpoint[] points,boolean open)
  {
    npoints = num;
    this.point = points;
    this.open = open;
  }
  
  /**
   * This constructor is used by the driver.
   */
  public PGpath(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 SQLException("cannot tell if path is open or closed");
    
    PGtokenizer t = new PGtokenizer(s,',');
    npoints = t.getSize();
    point = new PGpoint[npoints];
    for(int p=0;p<npoints;p++)
      point[p] = new PGpoint(t.getToken(p));
  }
  
  public boolean equals(Object obj)
  {
    PGpath p = (PGpath)obj;
    
    if(p.npoints != npoints)
      return false;
    
    if(p.open != open)
      return false;
    
    for(int i=0;i<npoints;i++)
      if(!point[i].equals(p.point[i]))
	return false;
    
    return true;
  }
  
  /**
   * This returns the polygon in the syntax expected by postgresql
   */
  public String toString()
  {
    StringBuffer b = new StringBuffer(open?"[":"(");
    
    for(int p=0;p<npoints;p++)
      b.append(point[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;
  }
  
}