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;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s