NebulaGraph Java Client  release-3.8
PolygonWrapper.java
1 /* Copyright (c) 2021 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 package com.vesoft.nebula.client.graph.data;
7 
8 import com.vesoft.nebula.Coordinate;
9 import com.vesoft.nebula.Polygon;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Objects;
13 
14 public class PolygonWrapper extends BaseDataObject {
15  private final Polygon polygon;
16 
17  public PolygonWrapper(Polygon polygon) {
18  this.polygon = polygon;
19  }
20 
21  public List<List<CoordinateWrapper>> getCoordListList() {
22  List<List<CoordinateWrapper>> coordListList = new ArrayList<>();
23  for (List<Coordinate> cl: polygon.getCoordListList()) {
24  List<CoordinateWrapper> coordList = new ArrayList<>();
25  for (Coordinate coordinate : cl) {
26  coordList.add(new CoordinateWrapper(coordinate));
27  }
28  coordListList.add(coordList);
29  }
30  return coordListList;
31  }
32 
33  @Override
34  public int hashCode() {
35  return Objects.hash(polygon);
36  }
37 
38  @Override
39  public boolean equals(Object o) {
40  if (this == o) {
41  return true;
42  }
43  if (o == null || getClass() != o.getClass()) {
44  return false;
45  }
46  PolygonWrapper that = (PolygonWrapper) o;
47  List<List<CoordinateWrapper>> thisListList = getCoordListList();
48  List<List<CoordinateWrapper>> thatListList = that.getCoordListList();
49  if (thisListList.size() != thatListList.size()) {
50  return false;
51  }
52  for (int i = 0; i < thisListList.size(); i++) {
53  List<CoordinateWrapper> thisList = thisListList.get(i);
54  List<CoordinateWrapper> thatList = thatListList.get(i);
55  if (thisList.size() != thatList.size()) {
56  return false;
57  }
58  for (int j = 0; j < thisList.size(); j++) {
59  if (!thisList.get(j).equals(thatList.get(j))) {
60  return false;
61  }
62  }
63  }
64  return true;
65  }
66 
67  @Override
68  public String toString() {
69  StringBuilder sb = new StringBuilder();
70  sb.append("POLYGON");
71  sb.append('(');
72  if (polygon.getCoordListList() != null) {
73  for (List<Coordinate> cl : polygon.getCoordListList()) {
74  sb.append('(');
75  for (Coordinate coordinate : cl) {
76  sb.append(coordinate.getX());
77  sb.append(' ');
78  sb.append(coordinate.getY());
79  sb.append(',');
80  }
81  if (sb.charAt(sb.length() - 1) == ',') {
82  sb.deleteCharAt(sb.length() - 1);
83  }
84  sb.append(')');
85  sb.append(',');
86  }
87  if (sb.charAt(sb.length() - 1) == ',') {
88  sb.deleteCharAt(sb.length() - 1);
89  }
90  }
91  sb.append(')');
92 
93  return sb.toString();
94  }
95 }