1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.grouplens.grapht.util;
21
22 import org.grouplens.grapht.InjectionException;
23 import org.grouplens.grapht.InvalidBindingException;
24 import org.grouplens.grapht.reflect.Qualifiers;
25
26 import java.lang.annotation.Annotation;
27 import java.lang.reflect.Modifier;
28
29
30
31
32
33
34
35 public final class Preconditions {
36 private Preconditions() { }
37
38 @SuppressWarnings("unchecked")
39 public static void isQualifier(Class<?> type) {
40 if (!Annotation.class.isAssignableFrom(type)) {
41 throw new InvalidBindingException(type, "Type is not an Annotation");
42 }
43 if (!Qualifiers.isQualifier((Class<? extends Annotation>) type)) {
44 throw new InvalidBindingException(type, "Annotation is not annotated with @Qualifier");
45 }
46 }
47
48 public static void isAssignable(Class<?> source, Class<?> impl) {
49 if (!source.isAssignableFrom(impl)) {
50 throw new InvalidBindingException(impl, "Type is not assignable to " + source);
51 }
52 }
53
54 public static void notNull(String name, Object value) {
55 if (value == null) {
56 throw new NullPointerException(name + " cannot be null");
57 }
58 }
59
60 public static void inRange(int value, int min, int max) {
61 if (value < min || value >= max) {
62 throw new IndexOutOfBoundsException(value + " must be in [" + min + ", " + max + ")");
63 }
64 }
65 }