10

I have multiple processes in which the bean properties must have different values. Example:

@Min( value=0, groups=ProcessA.class )
@Min( value=20, groups=ProcessB.class )
private int temperature;

Unfortunately the bean validation JSR 303 has not set @Repeatable on javax.validation.constraints.Min so this approach does not work. I found "Min.List" but without any doc about how to use it. Instead the official Oracle doc states at http://docs.oracle.com/javaee/7/api/javax/validation/constraints/class-use/Min.List.html

No usage of javax.validation.constraints.Min.List

So at moment this looks like a specification error?!?

Jemolah
  • 1,962
  • 3
  • 24
  • 41
  • The documentation also says "Defines several Min annotations on the same element.". So it looks exactly like what you want to do. Have you tried using it? What's the difficulty? – JB Nizet Apr 07 '15 at 22:06
  • javac throws a compile error: Duplicate annotation of non-repeatable type @ Min. Only annotation types marked @Repeatable can be used multiple times at one target. – Jemolah Apr 07 '15 at 22:19
  • That's what it says when you use `@Min` twice, as in the code you posted. And that's why you should use `@Min.List` instead, which is documented as "Defines several Min annotations on the same element.". What happens when you use `@Min.List`? – JB Nizet Apr 07 '15 at 22:20
  • Interesting idea. Really. But I don't get any correct syntax for it. Writing @Min( value=0, groups=ProcessA.class ) results in: "The attribute groups is undefined for the annotation type Min.List" – Jemolah Apr 07 '15 at 22:25
  • Min.List is specified as an internal interface of Min itself: @interface List { Min[] value(); } – Jemolah Apr 07 '15 at 22:32

1 Answers1

11

The syntax for Min.List, as for any other annotation taking an array of annotations as one of its attributes, is

@Min.List({ @Min(value = 0, groups = ProcessA.class),
            @Min(value = 20, groups = ProcessB.class) })
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks. That's it. Any idea why this is better than just having Min multiple times? At least it decreases readability. – Jemolah Apr 07 '15 at 22:35
  • 4
    Annotations are repeatable since Java 8. Bean validation was designed on Java 5 or 6. So this possibility didn't exist, hence the workaround of Min.List. If Bean Validation was designed now, Min.List would probably not exist. – JB Nizet Apr 07 '15 at 22:56
  • 1
    Good point. Thanks. I will make this an enhancement request to bean validation. – Jemolah Apr 08 '15 at 09:16