Tagged: default-methods

delegation with default methods


@Entity
public class Property extends BaseEntity {

    public static interface Owner<T extends BaseEntity & Owner<T>> {

        List<Property> getProperties();

        default void setProperties(List<Property> properties) {
            getProperties().clear();
            getProperties().addAll(properties);
        }

        @SuppressWarnings("unchecked")
        default T properties(List<Property> properties) {
            setProperties(properties);
            return (T) this;
        }

        @SuppressWarnings("unchecked")
        default T addProperty(@NotNull final Property property) {
            getProperties().add(property);
            return (T) this;
        }

        default T addProperty(@NotNull final String name,
                              @NotNull final String value) {
            return addProperty(new Property().name(name).value(value));
        }

        default Stream<Property> propertyStream() {
            return getProperties().stream();
        }

        default T filterProperties(@NotNull final java.util.function.Predicate<Property> predicate) {
            return properties(propertyStream().filter(predicate).collect(toList()));
        }
    }

    @Column(name = "NAME", nullable = false)
    private String name;

    @Column(name = "VALUE_", nullable = false)
    private String value;
}
@Entity
public class Event extends BaseEntity implements Property.Owner<Event> {

    @Override
    public List<Property> getProperties() {
        return (properties = ofNullable(properties).orElse(new ArrayList<>()));
    }

    @OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER,
               mappedBy = "event", orphanRemoval = true)
    private List<Property> properties;
}