From dc6f3620d897ac6799fb57774f989129975d0646 Mon Sep 17 00:00:00 2001 From: Sean Sundberg Date: Sun, 2 Jun 2024 09:04:05 -0500 Subject: [PATCH] Fixes fieldgroup bug (#4) Signed-off-by: Sean Sundberg --- .../openpages/support/models/BaseField.java | 78 +++++++++++++++++++ .../ibm/openpages/support/models/Field.java | 74 ++---------------- .../openpages/support/models/FieldGroup.java | 15 ++-- 3 files changed, 94 insertions(+), 73 deletions(-) create mode 100644 src/main/java/com/ibm/openpages/support/models/BaseField.java diff --git a/src/main/java/com/ibm/openpages/support/models/BaseField.java b/src/main/java/com/ibm/openpages/support/models/BaseField.java new file mode 100644 index 0000000..0357a43 --- /dev/null +++ b/src/main/java/com/ibm/openpages/support/models/BaseField.java @@ -0,0 +1,78 @@ +package com.ibm.openpages.support.models; + +import com.ibm.openpages.support.util.FieldType; + +public abstract class BaseField implements FieldMetadata, ResultLabel { + final protected String originalValue; + final protected String value; + final protected String label; + final protected FieldType fieldType; + + protected BaseField(FieldGroup fieldGroup, String value, FieldType fieldType) { + this(fieldGroup, value, value, fieldType); + } + + protected BaseField(FieldGroup fieldGroup, String value, String label, FieldType fieldType) { + this.originalValue = value; + this.value = fieldGroup != null ? fieldGroup.fieldName(value) : value; + this.label = label; + this.fieldType = fieldType; + } + + @Override + public boolean isEnumField() { + return fieldType.isEnumField(); + } + + @Override + public boolean isStringField() { + return fieldType.isStringField(); + } + + @Override + public boolean isIntegerField() { + return fieldType.isIntegerField(); + } + + @Override + public boolean isDecimalField() { + return fieldType.isDecimalField(); + } + + @Override + public boolean isDateField() { + return fieldType.isDateField(); + } + + @Override + public boolean isUserField() { + return fieldType.isUserField(); + } + + @Override + public boolean isBooleanField() { + return fieldType.isBooleanField(); + } + + @Override + public boolean isCurrencyField() { + return fieldType.isCurrencyField(); + } + + @Override + public FieldType fieldType() { + return fieldType; + } + + @Override + public String value() { + return value; + } + + @Override + public String label() { + return label; + } + + abstract protected T withFieldGroup(FieldGroup fieldGroup); +} diff --git a/src/main/java/com/ibm/openpages/support/models/Field.java b/src/main/java/com/ibm/openpages/support/models/Field.java index 6b5e334..a8cc710 100644 --- a/src/main/java/com/ibm/openpages/support/models/Field.java +++ b/src/main/java/com/ibm/openpages/support/models/Field.java @@ -2,79 +2,21 @@ import com.ibm.openpages.support.util.FieldType; -public class Field implements FieldMetadata, ResultLabel { - private FieldGroup fieldGroup; - final private String value; - final private String label; - final private FieldType fieldType; +public class Field extends BaseField { - protected Field(FieldGroup fieldGroup, String fieldName, FieldType fieldType) { - this(fieldGroup, fieldName, fieldName, fieldType); + protected Field(FieldGroup fieldGroup, String value, FieldType fieldType) { + super(fieldGroup, value, value, fieldType); } protected Field(FieldGroup fieldGroup, String value, String label, FieldType fieldType) { - this.fieldGroup = fieldGroup; - this.value = fieldGroup.fieldName(value); - this.label = label; - this.fieldType = fieldType; + super(fieldGroup, value, label, fieldType); } - @Override - public boolean isEnumField() { - return fieldType.isEnumField(); + protected Field withFieldGroup(FieldGroup fieldGroup) { + return new Field(fieldGroup, this.originalValue, this.label, this.fieldType); } - @Override - public boolean isStringField() { - return fieldType.isStringField(); - } - - @Override - public boolean isIntegerField() { - return fieldType.isIntegerField(); - } - - @Override - public boolean isDecimalField() { - return fieldType.isDecimalField(); - } - - @Override - public boolean isDateField() { - return fieldType.isDateField(); - } - - @Override - public boolean isUserField() { - return fieldType.isUserField(); - } - - @Override - public boolean isBooleanField() { - return fieldType.isBooleanField(); - } - - @Override - public boolean isCurrencyField() { - return fieldType.isCurrencyField(); - } - - @Override - public FieldType fieldType() { - return fieldType; - } - - @Override - public String value() { - return value; - } - - @Override - public String label() { - return label; - } - - protected void setFieldGroup(FieldGroup fieldGroup) { - this.fieldGroup = fieldGroup; + public String toString() { + return "[Field: value=" + value + ", label=" + label + ", fieldType=" + fieldType + "]"; } } diff --git a/src/main/java/com/ibm/openpages/support/models/FieldGroup.java b/src/main/java/com/ibm/openpages/support/models/FieldGroup.java index c781874..56b6b6e 100644 --- a/src/main/java/com/ibm/openpages/support/models/FieldGroup.java +++ b/src/main/java/com/ibm/openpages/support/models/FieldGroup.java @@ -8,7 +8,7 @@ import java.util.Objects; public class FieldGroup { - private final List fields; + private final List> fields; private final String value; public FieldGroup(final String value) { @@ -16,7 +16,7 @@ public FieldGroup(final String value) { fields = new ArrayList<>(); } - public List fields() { + public List> fields() { return Collections.unmodifiableList(fields); } @@ -36,16 +36,17 @@ public Field addField(String value, String label, FieldType fieldType) { return field; } - public T addField(T field) { - field.setFieldGroup(this); + public > T addField(T field) { + final T groupedField = field.withFieldGroup(this); - fields.add(field); + fields.add(groupedField); - return field; + return groupedField; } - public Field field(String name) { + public BaseField field(String name) { return fields.stream() + .map(field -> (BaseField)field) .filter(field -> field.value().equals(fieldName(name))) .findFirst() .orElseThrow(() -> new IllegalArgumentException("Field " + name + " not found"));