1 module hunt.database.driver.postgresql.data.Polygon;
2 
3 import hunt.database.driver.postgresql.data.Point;
4 
5 import hunt.collection.ArrayList;
6 import hunt.collection.List;
7 import hunt.util.StringBuilder;
8 
9 /**
10  * Polygon data type in Postgres represented by lists of points (the vertexes of the polygon).
11  * Polygons are very similar to closed paths, but are stored differently and have their own set of support routines.
12  */
13 class Polygon {
14     private List!(Point) points;
15 
16     this() {
17         this(new ArrayList!(Point)());
18     }
19 
20     this(List!(Point) points) {
21         this.points = points;
22     }
23 
24     // this(JsonObject json) {
25     //     PolygonConverter.fromJson(json, this);
26     // }
27 
28     List!(Point) getPoints() {
29         return points;
30     }
31 
32     void setPoints(List!(Point) points) {
33         this.points = points;
34     }
35 
36     override bool opEquals(Object o) {
37         if (this is o)
38             return true;
39         Polygon polygon = cast(Polygon) o;
40         if (polygon is null)
41             return false;
42 
43         return points == polygon.points;
44     }
45 
46     override size_t toHash() @trusted nothrow {
47         return points.toHash();
48     }
49 
50     override string toString() {
51         StringBuilder stringBuilder = new StringBuilder();
52         stringBuilder.append("Polygon");
53         stringBuilder.append("(");
54         for (int i = 0; i < points.size(); i++) {
55             Point point = points.get(i);
56             stringBuilder.append(point.toString());
57             if (i != points.size() - 1) {
58                 // not the last one
59                 stringBuilder.append(",");
60             }
61         }
62         stringBuilder.append(")");
63         return stringBuilder.toString();
64     }
65 
66     // JsonObject toJson() {
67     //     JsonObject json = new JsonObject();
68     //     PolygonConverter.toJson(this, json);
69     //     return json;
70     // }
71 }