Skip to content

Commit 1dd0f3c

Browse files
authored
♻️ simplify polygon utilities, 💥 prefer objects over static classes (#312)
1 parent e767965 commit 1dd0f3c

File tree

15 files changed

+84
-203
lines changed

15 files changed

+84
-203
lines changed

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

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

3-
import java.util.Arrays;
43
import java.util.List;
54
import lombok.Getter;
65

@@ -34,8 +33,8 @@ public Bbox(double minX, double maxX, double minY, double maxY) {
3433
* Get the Bbox as a Polygon.
3534
*/
3635
public Polygon getAsPolygon() {
37-
List<Point> points = Arrays
38-
.asList(
36+
var points = List
37+
.of(
3938
new Point(this.minX, this.minY),
4039
new Point(this.maxX, this.minY),
4140
new Point(this.maxX, this.maxY),

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

Lines changed: 7 additions & 24 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,8 @@ 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();
28-
29-
DoubleSummaryStatistics statsY = polygon
30-
.getCoordinates()
31-
.stream()
32-
.mapToDouble(Point::getY)
33-
.summaryStatistics();
21+
var statsX = polygon.getCoordinates().stream().mapToDouble(Point::getX).summaryStatistics();
22+
var statsY = polygon.getCoordinates().stream().mapToDouble(Point::getY).summaryStatistics();
3423

3524
return new Bbox(statsX.getMin(), statsX.getMax(), statsY.getMin(), statsY.getMax());
3625
}
@@ -44,13 +33,7 @@ public static Bbox generate(List<Polygon> polygons) {
4433
return null;
4534
}
4635

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

5639
/**
@@ -62,10 +45,10 @@ public static Bbox merge(List<Bbox> bboxes) {
6245
}
6346

6447
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()
48+
bboxes.stream().mapToDouble(Bbox::getMinX).min().getAsDouble(),
49+
bboxes.stream().mapToDouble(Bbox::getMaxX).max().getAsDouble(),
50+
bboxes.stream().mapToDouble(Bbox::getMinY).min().getAsDouble(),
51+
bboxes.stream().mapToDouble(Bbox::getMaxY).max().getAsDouble()
6952
);
7053
}
7154

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ public Bbox getAsBbox() {
4242
return BboxUtils.generate(this);
4343
}
4444

45+
/**
46+
* Get a bounding box that encloses the Polygon.
47+
*/
48+
public Polygon getBoundingBox() {
49+
var minMaxX = getMinMaxX();
50+
var minMaxY = getMinMaxY();
51+
return new Polygon(
52+
List
53+
.of(
54+
new Point(minMaxX.getMin(), minMaxY.getMin()),
55+
new Point(minMaxX.getMax(), minMaxY.getMin()),
56+
new Point(minMaxX.getMax(), minMaxY.getMax()),
57+
new Point(minMaxX.getMin(), minMaxY.getMax())
58+
)
59+
);
60+
}
61+
4562
/**
4663
* Get the central coordinates (centroid) of the Polygon.
4764
*/
@@ -84,15 +101,7 @@ public String toString() {
84101
* String representation with precise coordinates.
85102
*/
86103
public String toStringPrecise() {
87-
StringBuilder builder = new StringBuilder("(");
88-
for (int i = 0; i < coordinates.size(); i++) {
89-
if (i > 0) {
90-
builder.append(", ");
91-
}
92-
builder.append(coordinates.get(i));
93-
}
94-
builder.append(")");
95-
return builder.toString();
104+
return coordinates.stream().map(Point::toString).collect(Collectors.joining(", ", "(", ")"));
96105
}
97106

98107
/**

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
}

src/main/java/com/mindee/image/ExtractedImage.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.mindee.input.LocalInputSource;
44
import java.awt.image.BufferedImage;
55
import java.io.ByteArrayOutputStream;
6-
import java.io.File;
76
import java.io.IOException;
87
import java.nio.file.Path;
98
import java.nio.file.Paths;
@@ -40,9 +39,7 @@ public ExtractedImage(BufferedImage image, String filename, String saveFormat) {
4039
* @throws IOException Throws if the file can't be accessed.
4140
*/
4241
public void writeToFile(String outputPath) throws IOException {
43-
Path imagePath = Paths.get(outputPath, this.filename);
44-
File outputfile = imagePath.toFile();
45-
ImageIO.write(this.image, this.saveFormat, outputfile);
42+
writeToFile(Paths.get(outputPath));
4643
}
4744

4845
/**
@@ -53,8 +50,8 @@ public void writeToFile(String outputPath) throws IOException {
5350
* @throws IOException Throws if the file can't be accessed.
5451
*/
5552
public void writeToFile(Path outputPath) throws IOException {
56-
Path imagePath = outputPath.resolve(this.filename);
57-
File outputfile = imagePath.toFile();
53+
var imagePath = outputPath.resolve(this.filename);
54+
var outputfile = imagePath.toFile();
5855
ImageIO.write(this.image, this.saveFormat, outputfile);
5956
}
6057

@@ -65,7 +62,7 @@ public void writeToFile(Path outputPath) throws IOException {
6562
* @throws IOException Throws if the file can't be accessed.
6663
*/
6764
public LocalInputSource asInputSource() throws IOException {
68-
ByteArrayOutputStream output = new ByteArrayOutputStream();
65+
var output = new ByteArrayOutputStream();
6966
ImageIO.write(this.image, this.saveFormat, output);
7067
return new LocalInputSource(output.toByteArray(), this.filename);
7168
}

0 commit comments

Comments
 (0)