Skip to content

Commit 60b77e9

Browse files
authored
✨ add easier accessors (#267)
1 parent f3225a4 commit 60b77e9

File tree

5 files changed

+169
-18
lines changed

5 files changed

+169
-18
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
@Getter
1313
@JsonDeserialize(using = PolygonDeserializer.class)
1414
public class Polygon {
15+
/**
16+
* Position information as a list of points in clockwise order.
17+
*/
1518
private List<Point> coordinates = new ArrayList<>();
1619

1720
@Builder

src/main/java/com/mindee/parsing/v2/field/DynamicField.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,50 @@ public static DynamicField of(ListField value) {
5050
return new DynamicField(FieldType.LIST_FIELD, null, value, null);
5151
}
5252

53+
public SimpleField getSimpleField() throws IllegalStateException {
54+
if (type != FieldType.SIMPLE_FIELD) {
55+
throw new IllegalStateException("Field is not a simple field");
56+
}
57+
return simpleField;
58+
}
59+
60+
public ListField getListField() throws IllegalStateException {
61+
if (type != FieldType.LIST_FIELD) {
62+
throw new IllegalStateException("Field is not a list field");
63+
}
64+
return listField;
65+
}
66+
67+
public ObjectField getObjectField() throws IllegalStateException {
68+
if (type != FieldType.OBJECT_FIELD) {
69+
throw new IllegalStateException("Field is not an object field");
70+
}
71+
return objectField;
72+
}
73+
74+
/**
75+
* Returns the field as the specified class.
76+
*
77+
* @param type the class representing the desired field type
78+
* @param <T> the type of field to return
79+
* @throws IllegalArgumentException if the requested type is not SimpleField, ListField, or ObjectField
80+
* @throws IllegalStateException if the field's internal type does not match the requested type
81+
*/
82+
public <T extends BaseField> T getField(Class<T> type) throws IllegalArgumentException {
83+
if (type == SimpleField.class) {
84+
return (T) this.getSimpleField();
85+
}
86+
if (type == ListField.class) {
87+
return (T) this.getListField();
88+
}
89+
if (type == ObjectField.class) {
90+
return (T) this.getObjectField();
91+
}
92+
throw new IllegalArgumentException(
93+
"Cannot cast to " + type.getSimpleName()
94+
);
95+
}
96+
5397
@Override
5498
public String toString() {
5599
if (simpleField != null) return simpleField.toString();

src/main/java/com/mindee/parsing/v2/field/FieldLocation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class FieldLocation {
2828
private Polygon polygon;
2929

3030
/**
31-
* Page ID.
31+
* 0-based page index of where the polygon is located.
3232
*/
3333
@JsonProperty("page")
3434
private int page;

src/main/java/com/mindee/parsing/v2/field/InferenceFields.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,59 @@
1212
@EqualsAndHashCode(callSuper = true)
1313
@JsonIgnoreProperties(ignoreUnknown = true)
1414
public final class InferenceFields extends LinkedHashMap<String, DynamicField> {
15+
16+
/**
17+
* Retrieves the field as a `SimpleField`.
18+
*
19+
* @param fieldName the name of the field
20+
* @throws IllegalStateException if the field is not a SimpleField
21+
*/
22+
public SimpleField getSimpleField(String fieldName) throws IllegalStateException {
23+
return this.get(fieldName).getSimpleField();
24+
}
25+
26+
/**
27+
* Retrieves the field as a `ListField`.
28+
*
29+
* @param fieldName the name of the field
30+
* @throws IllegalStateException if the field is not a ListField
31+
*/
32+
public ListField getListField(String fieldName) throws IllegalStateException {
33+
return this.get(fieldName).getListField();
34+
}
35+
36+
/**
37+
* Retrieves the field as an `ObjectField`.
38+
*
39+
* @param fieldName the name of the field
40+
* @throws IllegalStateException if the field is not a ObjectField
41+
*/
42+
public ObjectField getObjectField(String fieldName) throws IllegalStateException {
43+
return this.get(fieldName).getObjectField();
44+
}
45+
1546
public String toString(int indent) {
1647
String padding = String.join("", java.util.Collections.nCopies(indent, " "));
1748
if (this.isEmpty()) {
1849
return "";
1950
}
2051
StringJoiner joiner = new StringJoiner("\n");
2152

22-
this.forEach((fieldKey, fieldValue) -> {
53+
this.forEach((fieldKey, fieldInstance) -> {
2354
StringBuilder strBuilder = new StringBuilder();
2455
strBuilder.append(padding).append(":").append(fieldKey).append(": ");
2556

26-
if (fieldValue.getListField() != null) {
27-
ListField listField = fieldValue.getListField();
57+
if (fieldInstance.getType() == DynamicField.FieldType.LIST_FIELD) {
58+
ListField listField = fieldInstance.getListField();
2859
if (listField.getItems() != null && !listField.getItems().isEmpty()) {
2960
strBuilder.append(listField);
3061
}
31-
} else if (fieldValue.getObjectField() != null) {
32-
strBuilder.append(fieldValue.getObjectField());
33-
} else if (fieldValue.getSimpleField() != null) {
62+
} else if (fieldInstance.getType() == DynamicField.FieldType.OBJECT_FIELD) {
63+
strBuilder.append(fieldInstance.getObjectField());
64+
} else if (fieldInstance.getType() == DynamicField.FieldType.SIMPLE_FIELD) {
3465
strBuilder.append(
35-
fieldValue.getSimpleField().getValue() != null
36-
? fieldValue.getSimpleField().toString()
66+
fieldInstance.getSimpleField().getValue() != null
67+
? fieldInstance.getSimpleField().toString()
3768
: ""
3869
);
3970

src/test/java/com/mindee/parsing/v2/InferenceTest.java

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package com.mindee.parsing.v2;
22

3+
import com.mindee.geometry.Point;
4+
import com.mindee.geometry.Polygon;
35
import com.mindee.input.LocalResponse;
4-
import com.mindee.parsing.v2.field.*;
6+
import com.mindee.parsing.v2.field.DynamicField;
7+
import com.mindee.parsing.v2.field.FieldConfidence;
8+
import com.mindee.parsing.v2.field.FieldLocation;
9+
import com.mindee.parsing.v2.field.InferenceFields;
10+
import com.mindee.parsing.v2.field.SimpleField;
11+
import com.mindee.parsing.v2.field.ListField;
12+
import com.mindee.parsing.v2.field.ObjectField;
513
import com.mindee.parsing.v2.field.DynamicField.FieldType;
614
import java.io.IOException;
715
import java.util.List;
@@ -62,21 +70,19 @@ void asyncPredict_whenEmpty_mustHaveValidProperties() throws IOException {
6270
switch (type) {
6371
case LIST_FIELD:
6472
assertNotNull(value.getListField(), entry.getKey() + " – ListField expected");
65-
assertNull(value.getObjectField(), entry.getKey() + " – ObjectField must be null");
66-
assertNull(value.getSimpleField(), entry.getKey() + " – SimpleField must be null");
73+
assertThrows(IllegalStateException.class, value::getSimpleField);
74+
assertThrows(IllegalStateException.class, value::getObjectField);
6775
break;
68-
6976
case OBJECT_FIELD:
7077
assertNotNull(value.getObjectField(), entry.getKey() + " – ObjectField expected");
71-
assertNull(value.getListField(), entry.getKey() + " – ListField must be null");
72-
assertNull(value.getSimpleField(), entry.getKey() + " – SimpleField must be null");
78+
assertThrows(IllegalStateException.class, value::getSimpleField);
79+
assertThrows(IllegalStateException.class, value::getListField);
7380
break;
74-
7581
case SIMPLE_FIELD:
7682
default:
7783
assertNotNull(value.getSimpleField(), entry.getKey() + " – SimpleField expected");
78-
assertNull(value.getListField(), entry.getKey() + " – ListField must be null");
79-
assertNull(value.getObjectField(), entry.getKey() + " – ObjectField must be null");
84+
assertThrows(IllegalStateException.class, value::getListField);
85+
assertThrows(IllegalStateException.class, value::getObjectField);
8086
break;
8187
}
8288
}
@@ -263,6 +269,73 @@ void standardFieldTypes_mustExposeCorrectTypes() throws IOException {
263269
}
264270
}
265271

272+
@Test
273+
@DisplayName("allow getting fields using generics")
274+
void standardFieldTypes_getWithGenerics() throws IOException {
275+
InferenceResponse response = loadFromResource("v2/inference/standard_field_types.json");
276+
Inference inference = response.getInference();
277+
assertNotNull(inference);
278+
InferenceFields fields = inference.getResult().getFields();
279+
280+
assertEquals(
281+
fields.get("field_simple_bool").getSimpleField(),
282+
fields.get("field_simple_bool").getField(SimpleField.class)
283+
);
284+
assertEquals(
285+
fields.get("field_simple_bool").getSimpleField(),
286+
fields.getSimpleField("field_simple_bool")
287+
);
288+
289+
assertEquals(
290+
fields.get("field_simple_list").getListField(),
291+
fields.get("field_simple_list").getField(ListField.class)
292+
);
293+
assertEquals(
294+
fields.get("field_simple_list").getListField(),
295+
fields.getListField("field_simple_list")
296+
);
297+
298+
assertEquals(
299+
fields.get("field_object").getObjectField(),
300+
fields.get("field_object").getField(ObjectField.class)
301+
);
302+
assertEquals(
303+
fields.get("field_object").getObjectField(),
304+
fields.getObjectField("field_object")
305+
);
306+
}
307+
308+
@Test
309+
@DisplayName("confidence and locations must be usable")
310+
void standardFieldTypes_confidenceAndLocations() throws IOException {
311+
InferenceResponse response = loadFromResource("v2/inference/standard_field_types.json");
312+
Inference inference = response.getInference();
313+
assertNotNull(inference);
314+
315+
InferenceFields fields = inference.getResult().getFields();
316+
317+
SimpleField fieldSimpleString = fields.get("field_simple_string").getField(SimpleField.class);
318+
FieldConfidence confidence = fieldSimpleString.getConfidence();
319+
boolean isCertain = confidence == FieldConfidence.Certain;
320+
assertTrue(isCertain);
321+
322+
List<FieldLocation> locations = fieldSimpleString.getLocations();
323+
assertEquals(1, locations.size());
324+
FieldLocation location = locations.get(0);
325+
326+
Polygon polygon = location.getPolygon();
327+
List<Point> coords = polygon.getCoordinates();
328+
assertEquals(4, coords.size());
329+
double topX = coords.get(0).getX();
330+
assertEquals(0.0, topX);
331+
332+
Point center = polygon.getCentroid();
333+
assertEquals(0.5, center.getX(), 0.00001);
334+
335+
int pageIndex = location.getPage();
336+
assertEquals(0, pageIndex);
337+
}
338+
266339
@Nested
267340
@DisplayName("raw_texts.json")
268341
class RawTexts {

0 commit comments

Comments
 (0)