blob: 21d17938c6cd2f22fe2154978de9e6247089d71f [file] [log] [blame]
Jesse Wilson66e84b92009-12-10 17:45:56 -08001package javax.annotation;
2
3import java.lang.annotation.Documented;
4import java.lang.annotation.Retention;
5import java.lang.annotation.RetentionPolicy;
6
7import javax.annotation.meta.TypeQualifier;
8import javax.annotation.meta.TypeQualifierValidator;
9import javax.annotation.meta.When;
10
11/** Used to annotate a value that should only contain nonnegative values */
12@Documented
13@TypeQualifier(applicableTo = Number.class)
14@Retention(RetentionPolicy.RUNTIME)
15public @interface Nonnegative {
16 When when() default When.ALWAYS;
17
18 class Checker implements TypeQualifierValidator<Nonnegative> {
19
20 public When forConstantValue(Nonnegative annotation, Object v) {
21 if (!(v instanceof Number))
22 return When.NEVER;
23 boolean isNegative;
24 Number value = (Number) v;
25 if (value instanceof Long)
26 isNegative = value.longValue() < 0;
27 else if (value instanceof Double)
28 isNegative = value.doubleValue() < 0;
29 else if (value instanceof Float)
30 isNegative = value.floatValue() < 0;
31 else
32 isNegative = value.intValue() < 0;
33
34 if (isNegative)
35 return When.NEVER;
36 else
37 return When.ALWAYS;
38
39 }
40 }
41}