]> granicus.if.org Git - postgresql/blob - src/interfaces/jdbc/org/postgresql/geometric/PGpath.java
60a4c92488373e78f191e0d4ddeb9324f816006f
[postgresql] / src / interfaces / jdbc / org / postgresql / geometric / PGpath.java
1 package org.postgresql.geometric;
2
3 import java.io.*;
4 import java.sql.*;
5 import org.postgresql.util.*;
6
7 /*
8  * This implements a path (a multiple segmented line, which may be closed)
9  */
10 public class PGpath extends PGobject implements Serializable, Cloneable
11 {
12         /*
13          * True if the path is open, false if closed
14          */
15         public boolean open;
16
17         /*
18          * The points defining this path
19          */
20         public PGpoint points[];
21
22         /*
23          * @param points the PGpoints that define the path
24          * @param open True if the path is open, false if closed
25          */
26         public PGpath(PGpoint[] points, boolean open)
27         {
28                 this();
29                 this.points = points;
30                 this.open = open;
31         }
32
33         /*
34          * Required by the driver
35          */
36         public PGpath()
37         {
38                 setType("path");
39         }
40
41         /*
42          * @param s definition of the circle in PostgreSQL's syntax.
43          * @exception SQLException on conversion failure
44          */
45         public PGpath(String s) throws SQLException
46         {
47                 this();
48                 setValue(s);
49         }
50
51         /*
52          * @param s Definition of the path in PostgreSQL's syntax
53          * @exception SQLException on conversion failure
54          */
55         public void setValue(String s) throws SQLException
56         {
57                 // First test to see if were open
58                 if (s.startsWith("[") && s.endsWith("]"))
59                 {
60                         open = true;
61                         s = PGtokenizer.removeBox(s);
62                 }
63                 else if (s.startsWith("(") && s.endsWith(")"))
64                 {
65                         open = false;
66                         s = PGtokenizer.removePara(s);
67                 }
68                 else
69                         throw new PSQLException("postgresql.geo.path");
70
71                 PGtokenizer t = new PGtokenizer(s, ',');
72                 int npoints = t.getSize();
73                 points = new PGpoint[npoints];
74                 for (int p = 0;p < npoints;p++)
75                         points[p] = new PGpoint(t.getToken(p));
76         }
77
78         /*
79          * @param obj Object to compare with
80          * @return true if the two boxes are identical
81          */
82         public boolean equals(Object obj)
83         {
84                 if (obj instanceof PGpath)
85                 {
86                         PGpath p = (PGpath)obj;
87
88                         if (p.points.length != points.length)
89                                 return false;
90
91                         if (p.open != open)
92                                 return false;
93
94                         for (int i = 0;i < points.length;i++)
95                                 if (!points[i].equals(p.points[i]))
96                                         return false;
97
98                         return true;
99                 }
100                 return false;
101         }
102
103         /*
104          * This must be overidden to allow the object to be cloned
105          */
106         public Object clone()
107         {
108                 PGpoint ary[] = new PGpoint[points.length];
109                 for (int i = 0;i < points.length;i++)
110                         ary[i] = (PGpoint)points[i].clone();
111                 return new PGpath(ary, open);
112         }
113
114         /*
115          * This returns the polygon in the syntax expected by org.postgresql
116          */
117         public String getValue()
118         {
119                 StringBuffer b = new StringBuffer(open ? "[" : "(");
120
121                 for (int p = 0;p < points.length;p++)
122                 {
123                         if (p > 0)
124                                 b.append(",");
125                         b.append(points[p].toString());
126                 }
127                 b.append(open ? "]" : ")");
128
129                 return b.toString();
130         }
131
132         public boolean isOpen()
133         {
134                 return open;
135         }
136
137         public boolean isClosed()
138         {
139                 return !open;
140         }
141
142         public void closePath()
143         {
144                 open = false;
145         }
146
147         public void openPath()
148         {
149                 open = true;
150         }
151 }