Skip to content

Commit b4bb4d3

Browse files
committed
♻️ simplify polygon utilities
1 parent e767965 commit b4bb4d3

File tree

3 files changed

+28
-102
lines changed

3 files changed

+28
-102
lines changed

src/main/java/com/mindee/geometry/BboxUtils.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.mindee.geometry;
22

3-
import java.util.DoubleSummaryStatistics;
43
import java.util.List;
5-
import java.util.Optional;
64

75
/**
86
* Methods for working with BBoxes.
@@ -20,17 +18,9 @@ public static Bbox generate(Polygon polygon) {
2018
return null;
2119
}
2220

23-
DoubleSummaryStatistics statsX = polygon
24-
.getCoordinates()
25-
.stream()
26-
.mapToDouble(Point::getX)
27-
.summaryStatistics();
21+
var statsX = polygon.getCoordinates().stream().mapToDouble(Point::getX).summaryStatistics();
2822

29-
DoubleSummaryStatistics statsY = polygon
30-
.getCoordinates()
31-
.stream()
32-
.mapToDouble(Point::getY)
33-
.summaryStatistics();
23+
var statsY = polygon.getCoordinates().stream().mapToDouble(Point::getY).summaryStatistics();
3424

3525
return new Bbox(statsX.getMin(), statsX.getMax(), statsY.getMin(), statsY.getMax());
3626
}
@@ -44,13 +34,7 @@ public static Bbox generate(List<Polygon> polygons) {
4434
return null;
4535
}
4636

47-
Optional<Polygon> mergedPolygon = polygons.stream().reduce(PolygonUtils::combine);
48-
49-
if (!mergedPolygon.isPresent()) {
50-
return null;
51-
}
52-
53-
return generate(mergedPolygon.get());
37+
return polygons.stream().reduce(PolygonUtils::combine).map(BboxUtils::generate).orElse(null);
5438
}
5539

5640
/**
@@ -62,10 +46,10 @@ public static Bbox merge(List<Bbox> bboxes) {
6246
}
6347

6448
return new Bbox(
65-
bboxes.stream().map(Bbox::getMinX).min(Double::compare).get(),
66-
bboxes.stream().map(Bbox::getMaxX).max(Double::compare).get(),
67-
bboxes.stream().map(Bbox::getMinY).min(Double::compare).get(),
68-
bboxes.stream().map(Bbox::getMaxY).max(Double::compare).get()
49+
bboxes.stream().mapToDouble(Bbox::getMinX).min().getAsDouble(),
50+
bboxes.stream().mapToDouble(Bbox::getMaxX).max().getAsDouble(),
51+
bboxes.stream().mapToDouble(Bbox::getMinY).min().getAsDouble(),
52+
bboxes.stream().mapToDouble(Bbox::getMaxY).max().getAsDouble()
6953
);
7054
}
7155

src/main/java/com/mindee/geometry/PolygonUtils.java

Lines changed: 17 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.mindee.geometry;
22

3-
import java.util.Arrays;
4-
import java.util.Collections;
53
import java.util.List;
64
import java.util.stream.Collectors;
75

@@ -19,25 +17,25 @@ private PolygonUtils() {
1917
public static Point getCentroid(List<Point> vertices) {
2018
int verticesSum = vertices.size();
2119

22-
double xSum = vertices.stream().map(Point::getX).mapToDouble(Double::doubleValue).sum();
23-
double ySum = vertices.stream().map(Point::getY).mapToDouble(Double::doubleValue).sum();
20+
double xSum = vertices.stream().mapToDouble(Point::getX).sum();
21+
double ySum = vertices.stream().mapToDouble(Point::getY).sum();
2422
return new Point(xSum / verticesSum, ySum / verticesSum);
2523
}
2624

2725
/**
2826
* Get the maximum and minimum Y coordinates in a given list of Points.
2927
*/
3028
public static MinMax getMinMaxY(List<Point> vertices) {
31-
List<Double> points = vertices.stream().map(Point::getY).collect(Collectors.toList());
32-
return new MinMax(Collections.min(points), Collections.max(points));
29+
var stats = vertices.stream().mapToDouble(Point::getY).summaryStatistics();
30+
return new MinMax(stats.getMin(), stats.getMax());
3331
}
3432

3533
/**
3634
* Get the maximum and minimum X coordinates in a given list of Points.
3735
*/
3836
public static MinMax getMinMaxX(List<Point> vertices) {
39-
List<Double> points = vertices.stream().map(Point::getX).collect(Collectors.toList());
40-
return new MinMax(Collections.min(points), Collections.max(points));
37+
var stats = vertices.stream().mapToDouble(Point::getX).summaryStatistics();
38+
return new MinMax(stats.getMin(), stats.getMax());
4139
}
4240

4341
/**
@@ -70,77 +68,20 @@ public static Polygon combine(Polygon base, Polygon target) {
7068
target = base;
7169
}
7270

73-
Double maxx = Math
74-
.max(
75-
target
76-
.getCoordinates()
77-
.stream()
78-
.map(Point::getX)
79-
.max(Double::compareTo)
80-
.orElse(Double.MIN_VALUE),
81-
base
82-
.getCoordinates()
83-
.stream()
84-
.map(Point::getX)
85-
.max(Double::compareTo)
86-
.orElse(Double.MIN_VALUE)
87-
);
71+
var combinedCoords = java.util.stream.Stream
72+
.concat(base.getCoordinates().stream(), target.getCoordinates().stream())
73+
.collect(Collectors.toList());
8874

89-
Double minx = Math
90-
.min(
91-
target
92-
.getCoordinates()
93-
.stream()
94-
.map(Point::getX)
95-
.min(Double::compareTo)
96-
.orElse(Double.MAX_VALUE),
97-
base
98-
.getCoordinates()
99-
.stream()
100-
.map(Point::getX)
101-
.min(Double::compareTo)
102-
.orElse(Double.MAX_VALUE)
103-
);
104-
105-
Double maxy = Math
106-
.max(
107-
target
108-
.getCoordinates()
109-
.stream()
110-
.map(Point::getY)
111-
.max(Double::compareTo)
112-
.orElse(Double.MIN_VALUE),
113-
base
114-
.getCoordinates()
115-
.stream()
116-
.map(Point::getY)
117-
.max(Double::compareTo)
118-
.orElse(Double.MIN_VALUE)
119-
);
120-
121-
Double miny = Math
122-
.min(
123-
target
124-
.getCoordinates()
125-
.stream()
126-
.map(Point::getY)
127-
.min(Double::compareTo)
128-
.orElse(Double.MAX_VALUE),
129-
base
130-
.getCoordinates()
131-
.stream()
132-
.map(Point::getY)
133-
.min(Double::compareTo)
134-
.orElse(Double.MAX_VALUE)
135-
);
75+
var xStats = combinedCoords.stream().mapToDouble(Point::getX).summaryStatistics();
76+
var yStats = combinedCoords.stream().mapToDouble(Point::getY).summaryStatistics();
13677

13778
return new Polygon(
138-
Arrays
139-
.asList(
140-
new Point(minx, miny),
141-
new Point(maxx, miny),
142-
new Point(maxx, maxy),
143-
new Point(minx, maxy)
79+
List
80+
.of(
81+
new Point(xStats.getMin(), yStats.getMin()),
82+
new Point(xStats.getMax(), yStats.getMin()),
83+
new Point(xStats.getMax(), yStats.getMax()),
84+
new Point(xStats.getMin(), yStats.getMax())
14485
)
14586
);
14687
}

src/main/java/com/mindee/http/MindeeApiCommon.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
5+
import java.nio.charset.StandardCharsets;
56
import org.apache.hc.core5.http.HttpEntity;
67

78
/**
@@ -10,7 +11,7 @@
1011
public abstract class MindeeApiCommon {
1112
/**
1213
* Retrieves the user agent.
13-
*
14+
*
1415
* @return the user agent.
1516
*/
1617
protected String getUserAgent() {
@@ -36,7 +37,7 @@ protected String getUserAgent() {
3637

3738
/**
3839
* Checks if the status code is out of the 2xx-3xx range.
39-
*
40+
*
4041
* @param statusCode the status code to check.
4142
* @return {@code true} if the status code is in the 2xx range, false otherwise.
4243
*/
@@ -50,6 +51,6 @@ protected String readRawResponse(HttpEntity responseEntity) throws IOException {
5051
for (int length; (length = responseEntity.getContent().read(buffer)) != -1;) {
5152
contentRead.write(buffer, 0, length);
5253
}
53-
return contentRead.toString("UTF-8");
54+
return contentRead.toString(StandardCharsets.UTF_8);
5455
}
5556
}

0 commit comments

Comments
 (0)