polygene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niclas Hedhman <nic...@hedhman.org>
Subject Re: Predicates as Constraints
Date Mon, 07 Sep 2015 06:04:07 GMT
Thinking a little bit outside our current box....

public interface Abc
{
    void doSomething( @URL String url );
}

module.values( Abc.class ).for( URL.class ).constrainWith( url ->
checkUrlFormat(url) );
module.for( URL.class ).constrainWith( new UrlChecker() );

I have started to experiment a little around this... More follows later.

Ok, after digging in this for an hour or so, I have discovered more
features...

The above is the default implementation, and it is then possible to throw
other implementations at the composite, and in that way override the
Constraint check declared in the annotation. There is also the Composite
Constraint feature (which I want to replace with factory instead)

The mechanism uses the fact that the Constraint implementation contains the
Annotation type as a generics argument, and that is how they are matched.
IF I go with Predicate, there is no such possibility, unless the Predicate
itself is annotated (I think not so nice).

So, the immediate question is; How desirable is this "override" feature? Is
there any other way to achieve the same functionality?

Cheers
Niclas


On Sat, Sep 5, 2015 at 3:49 PM, Niclas Hedhman <niclas@hedhman.org> wrote:

> Gang,
> ZEST-36...
>
> I think it makes sense to get rid of the Constraint interface and use the
> Predicate instead.
> And at the same time support a ConstraintFactory concept, where the
> Predicate can be created by a factory, to support the CompositeConstraint
> concept that exists (I didn't know that).
>
> So, something like this;
>
> public interface Customer
> {
>     Property<String> name();
>
>     @Age
>     Property<Integer> age();
> }
>
> @ConstraintDeclaration
> @Retention( RUNTIME )
> @Constraints( AgePredicate.class )
> public @interface Age
> {
>     int min() default 18;
>     int max() default 95;
> }
>
> public static class AgePredicate
>     implements Predicate<Integer>
> {
>     private Age range;
>
>     @Override
>     public boolean test( Integer argument )
>     {
>         return argument > range.min() && argument < range.max();
>     }
> }
>
> I will think a little bit more about the ConstrainFactory, but it should
> probably get access to a bit more than just the Annotation.
>
> To make the codebase lean, I would like to remove the Constraint variant
> completely, although @Constraints remain as previously.
>
> WDYT?
>
> Cheers
> --
> Niclas Hedhman, Software Developer
> http://zest.apache.org - New Energy for Java
>



-- 
Niclas Hedhman, Software Developer
http://zest.apache.org - New Energy for Java

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message