Jesse Wilson | 66e84b9 | 2009-12-10 17:45:56 -0800 | [diff] [blame^] | 1 | package javax.annotation; |
| 2 | |
| 3 | import java.lang.annotation.Documented; |
| 4 | import java.lang.annotation.Retention; |
| 5 | import java.lang.annotation.RetentionPolicy; |
| 6 | |
| 7 | import javax.annotation.meta.TypeQualifier; |
| 8 | import javax.annotation.meta.TypeQualifierValidator; |
| 9 | import 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) |
| 15 | public @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 | } |