improved Stringify
This commit is contained in:
@ -4,22 +4,26 @@ import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// TODO.refa: use this instead of toDisplayName everywhere and add JavaDoc
|
||||
public class Stringify<B> {
|
||||
import static java.lang.Boolean.TRUE;
|
||||
|
||||
public final class Stringify<B> {
|
||||
|
||||
private final Class<B> clazz;
|
||||
private final String name;
|
||||
private final List<Property<B>> props = new ArrayList<>();
|
||||
private String separator = ", ";
|
||||
private Boolean quotedValues = null;
|
||||
|
||||
public static <B> Stringify<B> stringify(final Class<B> clazz, final String name) {
|
||||
return new Stringify<B>(clazz, name);
|
||||
return new Stringify<>(clazz, name);
|
||||
}
|
||||
|
||||
public static <B> Stringify<B> stringify(final Class<B> clazz) {
|
||||
return new Stringify<B>(clazz, null);
|
||||
return new Stringify<>(clazz, null);
|
||||
}
|
||||
|
||||
private Stringify(final Class<B> clazz, final String name) {
|
||||
@ -28,7 +32,12 @@ public class Stringify<B> {
|
||||
}
|
||||
|
||||
public Stringify<B> withProp(final String propName, final Function<B, ?> getter) {
|
||||
props.add(new Property<B>(propName, getter));
|
||||
props.add(new Property<>(propName, getter));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Stringify<B> withProp(final Function<B, ?> getter) {
|
||||
props.add(new Property<>(null, getter));
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -42,20 +51,43 @@ public class Stringify<B> {
|
||||
}
|
||||
return propVal;
|
||||
})
|
||||
.map(propVal -> propVal.prop.name + "=" + optionallyQuoted(propVal))
|
||||
.collect(Collectors.joining(", "));
|
||||
.map(propVal -> propName(propVal, "=") + optionallyQuoted(propVal))
|
||||
.collect(Collectors.joining(separator));
|
||||
return (name != null ? name : object.getClass().getSimpleName()) + "(" + propValues + ")";
|
||||
}
|
||||
|
||||
public Stringify<B> withSeparator(final String separator) {
|
||||
this.separator = separator;
|
||||
return this;
|
||||
}
|
||||
|
||||
private String propName(final PropertyValue<B> propVal, final String delimiter) {
|
||||
return Optional.ofNullable(propVal.prop.name).map(v -> v + delimiter).orElse("");
|
||||
}
|
||||
|
||||
private String optionallyQuoted(final PropertyValue<B> propVal) {
|
||||
return (propVal.rawValue instanceof Number) || (propVal.rawValue instanceof Boolean)
|
||||
? propVal.value
|
||||
: "'" + propVal.value + "'";
|
||||
if (quotedValues == null)
|
||||
return quotedQuotedValueType(propVal)
|
||||
? ("'" + propVal.value + "'")
|
||||
: propVal.value;
|
||||
return TRUE == quotedValues
|
||||
? ("'" + propVal.value + "'")
|
||||
: propVal.value;
|
||||
}
|
||||
|
||||
private static <B> boolean quotedQuotedValueType(final PropertyValue<B> propVal) {
|
||||
return !(propVal.rawValue instanceof Number || propVal.rawValue instanceof Boolean);
|
||||
}
|
||||
|
||||
public Stringify<B> quotedValues(final boolean quotedValues) {
|
||||
this.quotedValues = quotedValues;
|
||||
return this;
|
||||
}
|
||||
|
||||
private record Property<B>(String name, Function<B, ?> getter) {}
|
||||
|
||||
private record PropertyValue<B>(Property<B> prop, Object rawValue, String value) {
|
||||
|
||||
static <B> PropertyValue<B> of(Property<B> prop, Object rawValue) {
|
||||
return rawValue != null ? new PropertyValue<>(prop, rawValue, rawValue.toString()) : null;
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package net.hostsharing.hsadminng.hs.office.partner;
|
||||
|
||||
import lombok.*;
|
||||
import net.hostsharing.hsadminng.Stringify;
|
||||
import net.hostsharing.hsadminng.Stringifyable;
|
||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
|
||||
|
||||
@ -8,6 +10,8 @@ import javax.persistence.*;
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.hostsharing.hsadminng.Stringify.stringify;
|
||||
|
||||
@Entity
|
||||
@Table(name = "hs_office_partner_rv")
|
||||
@Getter
|
||||
@ -15,7 +19,13 @@ import java.util.UUID;
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class HsOfficePartnerEntity {
|
||||
public class HsOfficePartnerEntity implements Stringifyable {
|
||||
|
||||
private static Stringify<HsOfficePartnerEntity> stringify = stringify(HsOfficePartnerEntity.class, "partner")
|
||||
.withProp(HsOfficePartnerEntity::getPerson)
|
||||
.withProp(HsOfficePartnerEntity::getContact)
|
||||
.withSeparator(": ")
|
||||
.quotedValues(false);
|
||||
|
||||
private @Id UUID uuid;
|
||||
|
||||
@ -33,7 +43,13 @@ public class HsOfficePartnerEntity {
|
||||
private @Column(name = "birthday") LocalDate birthday;
|
||||
private @Column(name = "dateofdeath") LocalDate dateOfDeath;
|
||||
|
||||
public String getDisplayName() {
|
||||
return "partner(%s, %s)".formatted(person.getDisplayName(), contact.getLabel());
|
||||
@Override
|
||||
public String toString() {
|
||||
return stringify.apply(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toShortString() {
|
||||
return person.toShortString();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user