lundi 25 juillet 2016

Google Autovalue Validation - Composite and Builder Pattern - Unique Property Value Only For Primary Root Object

I'm using a combination of the composite and builder pattern (this, with the help of Google's AutoValue library). However, for the root object, I'd like to set the field property to null; for non-root objects, it could be set to any value as long as this value is not null. Any suggestions on how to work around this? I'd hate to nest this class in another class just for this purpose (would cause repetitive code); but it's the only solution popping into my head right now.

@AutoValue
public abstract static class Composite {

    public static Builder builder() {
        return new AutoValue_Main_Composite.Builder()
                .property( "non-null" );
    }

    public abstract ImmutableSet<Composite> composites();

    public abstract String property();

    @AutoValue.Builder
    public static abstract class Builder {

        public abstract Builder property( String property );

        public abstract Builder composites( Set<Composite> composites );

        public abstract Composite build();

    }

}

Aucun commentaire:

Enregistrer un commentaire