Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/java/com/mindee/parsing/generated/GeneratedObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mindee.geometry.Polygon;
import com.mindee.geometry.PolygonUtils;
import com.mindee.parsing.standard.AmountField;
import com.mindee.parsing.standard.BooleanField;
import com.mindee.parsing.standard.ClassificationField;
import com.mindee.parsing.standard.DateField;
import com.mindee.parsing.standard.StringField;
Expand All @@ -17,6 +18,7 @@ public class GeneratedObject extends HashMap<String, Object> {

/**
* Represent the object as a standard {@link StringField}.
* @return A {@link StringField} containing a string value.
*/
public StringField asStringField() {
return new StringField(
Expand All @@ -30,6 +32,7 @@ public StringField asStringField() {

/**
* Represent the object as a standard {@link AmountField}.
* @return An {@link AmountField} containing a double value.
*/
public AmountField asAmountField() {
Double value;
Expand All @@ -51,8 +54,21 @@ else if (rawValue instanceof Double) {
);
}

/**
* Represent the object as a standard {@link BooleanField}.
* @return A {@link BooleanField} containing a boolean value.
*/
public BooleanField asBooleanField() {
Object rawValue = this.get("value");
if (rawValue instanceof Boolean) {
return new BooleanField((Boolean) rawValue, this.getConfidence(), this.getPolygon(), this.getPageId());
}
throw new ClassCastException("Cannot cast " + rawValue + " to Boolean");
}

/**
* Represent the object as a standard {@link DateField}.
* @return A {@link DateField} containing a date value.
*/
public DateField asDateField() {
return new DateField(
Expand All @@ -66,6 +82,7 @@ public DateField asDateField() {

/**
* Represent the object as a standard {@link ClassificationField}.
* @return A {@link ClassificationField} containing a string value.
*/
public ClassificationField asClassificationField() {
return new ClassificationField(
Expand All @@ -75,13 +92,16 @@ public ClassificationField asClassificationField() {

/**
* Get the polygon, if present.
* @return A {@link Polygon}.
*/
public Polygon getPolygon() {
return this.getAsPolygon("polygon");
}

/**
* Get the specified key as a {@link Polygon} object.
* @param key Key to retrieve the polygon from.
* @return A {@link Polygon}.
*/
public Polygon getAsPolygon(String key) {
if (this.containsKey(key)) {
Expand All @@ -92,20 +112,23 @@ public Polygon getAsPolygon(String key) {

/**
* Get the page ID, if present.
* @return A page ID, as an integer.
*/
public Integer getPageId() {
return this.get("page_id") != null ? (Integer) this.get("page_id") : null;
}

/**
* Get the confidence score, if present.
* @return The confidence score, as a double.
*/
public Double getConfidence() {
return this.get("confidence") != null ? (Double) this.get("confidence") : null;
}

/**
* Get the information on whether the date field was extracted.
* @return A boolean representation of the field.
*/
public Boolean getIsComputed(){
return (Boolean) this.get("is_computed");
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/mindee/product/generated/GeneratedV1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mindee.parsing.generated.GeneratedFeature;
import com.mindee.parsing.generated.GeneratedObject;
import com.mindee.parsing.standard.AmountField;
import com.mindee.parsing.standard.BooleanField;
import com.mindee.parsing.standard.ClassificationField;
import com.mindee.parsing.standard.DateField;
import com.mindee.parsing.standard.StringField;
Expand Down Expand Up @@ -260,4 +261,20 @@ void whenAmountDeserialized_mustCastToDouble() {
doubleObject.put("value", "I will fail miserably");
Assertions.assertThrows(ClassCastException.class, badStringObject::asAmountField);
}

void whenBooleanDeserialized_mustCastToBoolean() {
GeneratedObject boolObject = new GeneratedObject();
boolObject.put("value", true);
BooleanField booleanField = boolObject.asBooleanField();
Assertions.assertEquals(true, booleanField.getValue());

boolObject.put("value", false);
booleanField = boolObject.asBooleanField();
Assertions.assertEquals(true, booleanField.getValue());


boolObject.put("value", null);
booleanField = boolObject.asBooleanField();
Assertions.assertNull(booleanField.getValue());
}
}