~ubuntu-branches/ubuntu/maverick/libpgjava/maverick

« back to all changes in this revision

Viewing changes to org/postgresql/geometric/PGpolygon.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2006-04-25 00:07:07 UTC
  • mfrom: (1.3.1 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20060425000707-6lr2s0awuz4z48hm
* Drop support for the old jdbc2 driver (can be reverted if wanted)
  (closes: #358345).
* New upstream (thanks to Wolfgang Baer).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
*
 
3
* Copyright (c) 2003-2005, PostgreSQL Global Development Group
 
4
*
 
5
* IDENTIFICATION
 
6
*   $PostgreSQL: pgjdbc/org/postgresql/geometric/PGpolygon.java,v 1.11 2005/01/11 08:25:45 jurka Exp $
 
7
*
 
8
*-------------------------------------------------------------------------
 
9
*/
 
10
package org.postgresql.geometric;
 
11
 
 
12
import org.postgresql.util.PGobject;
 
13
import org.postgresql.util.PGtokenizer;
 
14
import java.io.Serializable;
 
15
import java.sql.SQLException;
 
16
 
 
17
/**
 
18
 *     This implements the polygon datatype within PostgreSQL.
 
19
 */
 
20
public class PGpolygon extends PGobject implements Serializable, Cloneable
 
21
{
 
22
    /**
 
23
     * The points defining the polygon
 
24
     */
 
25
    public PGpoint points[];
 
26
 
 
27
    /**
 
28
     * Creates a polygon using an array of PGpoints
 
29
     *
 
30
     * @param points the points defining the polygon
 
31
     */
 
32
    public PGpolygon(PGpoint[] points)
 
33
    {
 
34
        this();
 
35
        this.points = points;
 
36
    }
 
37
 
 
38
    /**
 
39
     * @param s definition of the polygon in PostgreSQL's syntax.
 
40
     * @exception SQLException on conversion failure
 
41
     */
 
42
    public PGpolygon(String s) throws SQLException
 
43
    {
 
44
        this();
 
45
        setValue(s);
 
46
    }
 
47
 
 
48
    /**
 
49
     * Required by the driver
 
50
     */
 
51
    public PGpolygon()
 
52
    {
 
53
        setType("polygon");
 
54
    }
 
55
 
 
56
    /**
 
57
     * @param s Definition of the polygon in PostgreSQL's syntax
 
58
     * @exception SQLException on conversion failure
 
59
     */
 
60
    public void setValue(String s) throws SQLException
 
61
    {
 
62
        PGtokenizer t = new PGtokenizer(PGtokenizer.removePara(s), ',');
 
63
        int npoints = t.getSize();
 
64
        points = new PGpoint[npoints];
 
65
        for (int p = 0;p < npoints;p++)
 
66
            points[p] = new PGpoint(t.getToken(p));
 
67
    }
 
68
 
 
69
    /**
 
70
     * @param obj Object to compare with
 
71
     * @return true if the two polygons are identical
 
72
     */
 
73
    public boolean equals(Object obj)
 
74
    {
 
75
        if (obj instanceof PGpolygon)
 
76
        {
 
77
            PGpolygon p = (PGpolygon)obj;
 
78
 
 
79
            if (p.points.length != points.length)
 
80
                return false;
 
81
 
 
82
            for (int i = 0;i < points.length;i++)
 
83
                if (!points[i].equals(p.points[i]))
 
84
                    return false;
 
85
 
 
86
            return true;
 
87
        }
 
88
        return false;
 
89
    }
 
90
 
 
91
    public int hashCode() {
 
92
        // XXX not very good..
 
93
        int hash = 0;
 
94
        for (int i = 0; i < points.length && i < 5; ++i)
 
95
        {
 
96
            hash = hash ^ points[i].hashCode();
 
97
        }
 
98
        return hash;
 
99
    }
 
100
 
 
101
    public Object clone()
 
102
    {
 
103
        PGpoint ary[] = new PGpoint[points.length];
 
104
        for (int i = 0;i < points.length;i++)
 
105
            ary[i] = (PGpoint)points[i].clone();
 
106
        return new PGpolygon(ary);
 
107
    }
 
108
 
 
109
    /**
 
110
     * @return the PGpolygon in the syntax expected by org.postgresql
 
111
     */
 
112
    public String getValue()
 
113
    {
 
114
        StringBuffer b = new StringBuffer();
 
115
        b.append("(");
 
116
        for (int p = 0;p < points.length;p++)
 
117
        {
 
118
            if (p > 0)
 
119
                b.append(",");
 
120
            b.append(points[p].toString());
 
121
        }
 
122
        b.append(")");
 
123
        return b.toString();
 
124
    }
 
125
}