Skip to content

Commit

Permalink
feat: update builder at reflection
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Mar 11, 2024
1 parent 7390310 commit d357c41
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ abstract class AbstractFieldMetadata implements FieldMetadata {

protected final Class<?> type;

protected final String udt;

AbstractFieldMetadata(MappingType mappingType, Field field, String name,
Class<? extends AttributeConverter<?, ?>> converter, FieldReader reader, FieldWriter writer) {
Class<? extends AttributeConverter<?, ?>> converter,
FieldReader reader, FieldWriter writer, String udt) {
this.mappingType = mappingType;
this.field = field;
this.name = name;
Expand All @@ -61,6 +64,7 @@ abstract class AbstractFieldMetadata implements FieldMetadata {
this.reader = reader;
this.writer = writer;
this.type = field.getType();
this.udt = udt;
}

@Override
Expand All @@ -78,6 +82,14 @@ public String fieldName() {
return fieldName;
}

@Override
public Optional<String> udt(){
if(udt == null|| udt.isEmpty() || udt.isBlank()){
return Optional.empty();
}
return Optional.of(udt);
}

@Override
public Object read(Object bean) {
Objects.requireNonNull(bean, "bean is required");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class DefaultFieldMetadata extends AbstractFieldMetadata {

DefaultFieldMetadata(MappingType type, Field field, String name,
Class<? extends AttributeConverter<?, ?>> converter, boolean id,
FieldReader reader, FieldWriter writer) {
super(type, field, name, converter, reader, writer);
FieldReader reader, FieldWriter writer, String udt) {
super(type, field, name, converter, reader, writer, udt);
this.id = id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ final class DefaultGenericFieldMetadata extends AbstractFieldMetadata implement
private final TypeSupplier<?> typeSupplier;

DefaultGenericFieldMetadata(MappingType type, Field field, String name, TypeSupplier<?> typeSupplier,
Class<? extends AttributeConverter<?, ?>> converter, FieldReader reader, FieldWriter writer) {
super(type, field, name, converter, reader, writer);
Class<? extends AttributeConverter<?, ?>> converter,
FieldReader reader, FieldWriter writer, String udt) {
super(type, field, name, converter, reader, writer, udt);
this.typeSupplier = typeSupplier;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ final class EmbeddedFieldMetadata extends AbstractFieldMetadata {
private final String entityName;

public EmbeddedFieldMetadata(MappingType type, Field field, String name, String entityName,
FieldReader reader, FieldWriter writer) {
super(type, field, name, null, reader, writer);
FieldReader reader, FieldWriter writer, String udt) {
super(type, field, name, null, reader, writer, udt);
this.entityName = entityName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,62 +41,69 @@ class FieldMappingBuilder {

private FieldWriter writer;

private String udt;

public FieldMappingBuilder withType(MappingType type) {

public FieldMappingBuilder type(MappingType type) {
this.type = type;
return this;
}

public FieldMappingBuilder withField(Field field) {
public FieldMappingBuilder field(Field field) {
this.field = field;
return this;
}

public FieldMappingBuilder withName(String name) {
public FieldMappingBuilder name(String name) {
this.name = name;
return this;
}

public FieldMappingBuilder withTypeSupplier(TypeSupplier<?> typeSupplier) {
public FieldMappingBuilder typeSupplier(TypeSupplier<?> typeSupplier) {
this.typeSupplier = typeSupplier;
return this;
}

public FieldMappingBuilder withEntityName(String entityName) {
public FieldMappingBuilder entityName(String entityName) {
this.entityName = entityName;
return this;
}

public FieldMappingBuilder withConverter(Class<? extends AttributeConverter<?, ?>> converter) {
public FieldMappingBuilder converter(Class<? extends AttributeConverter<?, ?>> converter) {
this.converter = converter;
return this;
}

public FieldMappingBuilder withId(boolean id) {
public FieldMappingBuilder id(boolean id) {
this.id = id;
return this;
}

public FieldMappingBuilder withWriter(FieldWriter writer) {
public FieldMappingBuilder writer(FieldWriter writer) {
this.writer = writer;
return this;
}

public FieldMappingBuilder withReader(FieldReader reader) {
public FieldMappingBuilder udt(String udt) {
this.udt = udt;
return this;
}

public FieldMappingBuilder reader(FieldReader reader) {
this.reader = reader;
return this;
}

public DefaultFieldMetadata buildDefault() {
return new DefaultFieldMetadata(type, field, name, converter, id, reader, writer);
return new DefaultFieldMetadata(type, field, name, converter, id, reader, writer, udt);
}

public GenericFieldMetadata buildGeneric() {
return new DefaultGenericFieldMetadata(type, field, name, typeSupplier, converter, reader, writer);
return new DefaultGenericFieldMetadata(type, field, name, typeSupplier, converter, reader, writer, udt);
}

public EmbeddedFieldMetadata buildEmbedded() {
return new EmbeddedFieldMetadata(type, field, name, entityName, reader, writer);
return new EmbeddedFieldMetadata(type, field, name, entityName, reader, writer, udt);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,21 @@ private FieldMetadata to(Field field) {
boolean id = reflections.isIdField(field);
String columnName = id ? reflections.getIdName(field) : reflections.getColumnName(field);

FieldMappingBuilder builder = new FieldMappingBuilder().withName(columnName)
.withField(field).withType(mappingType).withId(id)
.withReader(bean -> reflections.getValue(bean, field))
.withWriter( (bean, value) -> reflections.setValue(bean, field, value));
FieldMappingBuilder builder = new FieldMappingBuilder().name(columnName)
.field(field).type(mappingType).id(id)
.reader(bean -> reflections.getValue(bean, field))
.writer( (bean, value) -> reflections.setValue(bean, field, value));

if (nonNull(convert)) {
builder.withConverter(convert.value());
builder.converter(convert.value());
}
switch (mappingType) {
case COLLECTION, MAP -> {
builder.withTypeSupplier(field::getGenericType);
builder.typeSupplier(field::getGenericType);
return builder.buildGeneric();
}
case EMBEDDED -> {
return builder.withEntityName(reflections.getEntityName(field.getType())).buildEmbedded();
return builder.entityName(reflections.getEntityName(field.getType())).buildEmbedded();
}
default -> {
return builder.buildDefault();
Expand Down

0 comments on commit d357c41

Please sign in to comment.