1 module hunt.database.driver.postgresql.data.Path;
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  * Path data type in Postgres represented by lists of connected points.
11  * Paths can be open, where the first and last points in the list are considered not connected,
12  * or closed, where the first and last points are considered connected.
13  */
14 class Path {
15     private bool _isOpen;
16     private List!(Point) points;
17 
18     this() {
19         this(false, new ArrayList!(Point)());
20     }
21 
22     this(bool isOpen, List!(Point) points) {
23         this._isOpen = isOpen;
24         this.points = points;
25     }
26 
27     // this(JsonObject json) {
28     //     PathConverter.fromJson(json, this);
29     // }
30 
31     bool isOpen() {
32         return _isOpen;
33     }
34 
35     void setOpen(bool open) {
36         _isOpen = open;
37     }
38 
39     List!(Point) getPoints() {
40         return points;
41     }
42 
43     void setPoints(List!(Point) points) {
44         this.points = points;
45     }
46 
47     override bool opEquals(Object o) {
48         if (this is o)
49             return true;
50 
51         Path path = cast(Path) o;
52         if (path is null)
53             return false;
54 
55         if (_isOpen != path._isOpen)
56             return false;
57         return points == path.points;
58     }
59 
60     override size_t toHash() @trusted nothrow {
61         size_t result = (_isOpen ? 1 : 0);
62         result = 31 * result + points.toHash();
63         return result;
64     }
65 
66     override string toString() {
67         string left;
68         string right;
69         if (_isOpen) {
70             left = "[";
71             right = "]";
72         } else {
73             left = "(";
74             right = ")";
75         }
76         StringBuilder stringBuilder = new StringBuilder();
77         stringBuilder.append("Path");
78         stringBuilder.append(left);
79         for (int i = 0; i < points.size(); i++) {
80             Point point = points.get(i);
81             stringBuilder.append(point.toString());
82             if (i != points.size() - 1) {
83                 // not the last one
84                 stringBuilder.append(",");
85             }
86         }
87         stringBuilder.append(right);
88         return stringBuilder.toString();
89     }
90 
91     // JsonObject toJson() {
92     //     JsonObject json = new JsonObject();
93     //     PathConverter.toJson(this, json);
94     //     return json;
95     // }
96 }