Skip to content

Commit

Permalink
Fixes fieldgroup bug (#4)
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Sundberg <[email protected]>
  • Loading branch information
seansund authored Jun 2, 2024
1 parent f12ee89 commit dc6f362
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 73 deletions.
78 changes: 78 additions & 0 deletions src/main/java/com/ibm/openpages/support/models/BaseField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.ibm.openpages.support.models;

import com.ibm.openpages.support.util.FieldType;

public abstract class BaseField<T> 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);
}
74 changes: 8 additions & 66 deletions src/main/java/com/ibm/openpages/support/models/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Field> {

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 + "]";
}
}
15 changes: 8 additions & 7 deletions src/main/java/com/ibm/openpages/support/models/FieldGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import java.util.Objects;

public class FieldGroup {
private final List<Field> fields;
private final List<? super BaseField<?>> fields;
private final String value;

public FieldGroup(final String value) {
this.value = value;
fields = new ArrayList<>();
}

public List<Field> fields() {
public List<? super BaseField<?>> fields() {
return Collections.unmodifiableList(fields);
}

Expand All @@ -36,16 +36,17 @@ public Field addField(String value, String label, FieldType<?> fieldType) {
return field;
}

public <T extends Field> T addField(T field) {
field.setFieldGroup(this);
public <T extends BaseField<T>> 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"));
Expand Down

0 comments on commit dc6f362

Please sign in to comment.