aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/protect/Validator.java
blob: 0a7ead8cf57912851751df9303b93466c5b4d6f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.protect;

/**
 * Static utility methods for validating input.
 *
 * @author bratseth
 */
public abstract class Validator {

    /** Throws NullPointerException if the argument is null */
    public static void ensureNotNull(String argumentDescription, Object argument) {
        if (argument == null)
            throw new NullPointerException(argumentDescription + " can not be null");
    }

    /** Throws NullPointerException if the argument is null */
    public static void ensureNonEmpty(String argumentDescription, String argument) {
        if (argument.isEmpty())
            throw new IllegalArgumentException(argumentDescription + " can not be empty");
    }

    /**
     * Throws an IllegalStateException if the given field value
     * is initialized (not null)
     */
    public static void ensureNotInitialized(String fieldDescription, Object fieldOwner, Object fieldValue) {
        if (fieldValue != null) {
            throw new IllegalStateException(fieldDescription + " of " + fieldOwner +
                                            " cannot be changed, it is already set " + "to " + fieldValue);
        }
    }

    /**
     * Throws an IllegalArgumentException if the given argument is not
     * in the given range
     *
     * @param argumentDescription a description of the argument
     * @param from the range start, inclusive
     * @param to the range end, inclusive
     * @param argument the argument value to check
     */
    public static void ensureInRange(String argumentDescription, int from, int to, int argument) {
        if (argument < from || argument > to) {
            throw new IllegalArgumentException(argumentDescription + " is " + argument +
                                               " but must be between " + from + " and " + to);
        }
    }

    /**
     * Throws an IllegalArgumentException if the first argument is not strictly
     * smaller than the second argument
     *
     * @param smallDescription description of the smallest argument
     * @param small the smallest argument
     * @param largeDescription description of the lergest argument
     * @param large the largest argument
     */
    public static void ensureSmaller(String smallDescription, int small, String largeDescription, int large) {
        if (small >= large) {
            throw new IllegalArgumentException(smallDescription + " is " + small + " but should be " +
                                               "less than " + largeDescription + " " + large);
        }
    }

    /**
     * Throws an IllegalArgumentException if the first argument is not strictly
     * smaller than the second argument
     *
     * @param smallDescription description of the smallest argument
     * @param small the smallest argument
     * @param largeDescription description of the largest argument
     * @param large the largest argument
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void ensureSmaller(String smallDescription, Comparable small, String largeDescription, Comparable large) {
        if (small.compareTo(large) >= 0) {
            throw new IllegalArgumentException(smallDescription + " is " + small + " but should be " +
                                               "less than " + largeDescription + " " + large);
        }
    }

    /**
     * Ensures that the given argument is true
     *
     * @param description of what is the case if the condition is false
     * @param condition the condition to ensure is true
     * @throws IllegalArgumentException if the given condition was false
     */
    public static void ensure(String description, boolean condition) {
        if ( ! condition) {
            throw new IllegalArgumentException(description);
        }
    }

    /**
     * Ensure the given argument is true, if not throw IllegalArgumentException
     * concatenating the String representation of the description arguments.
     */
    public static void ensure(boolean condition, Object... description) {
        if ( ! condition) {
            StringBuilder msg = new StringBuilder();
            for (Object part : description) {
                msg.append(part.toString());
            }
            throw new IllegalArgumentException(msg.toString());
        }
    }

    /**
     * Ensures that an item is of a particular class
     *
     * @param description a description of the item to be checked
     * @param item the item to check the type of
     * @param type the type the given item should be instanceof
     * @throws IllegalArgumentException if the given item is not of the correct type
     */
    public static void ensureInstanceOf(String description, Object item, Class<?> type) {
        if ( ! type.isAssignableFrom(item.getClass())) {
            throw new IllegalArgumentException(description + " " + item + " should be an instance of " + type +
                                               " but is " + item.getClass());
        }
    }

    /**
     * Ensures that an item is not of a particular class
     *
     * @param description a description of the item to be checked
     * @param item the item to check the type of
     * @param type the type the given item should NOT be instanceof
     * @throws IllegalArgumentException if the given item is of the wrong type
     */
    public static void ensureNotInstanceOf(String description, Object item, Class<?> type) {
        if ( type.isAssignableFrom(item.getClass())) {
            throw new IllegalArgumentException(description + " " + item + " should NOT be an instance of " + type +
                    " but is " + item.getClass());
        }
    }

}