|
| 1 | +/* |
| 2 | + * Copyright 2014 Christoph Böhme |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.culturegraph.mf.util; |
| 17 | + |
| 18 | +/** |
| 19 | + * Defines static methods for method argument validation. |
| 20 | + * |
| 21 | + * @author Christoph Böhme |
| 22 | + * |
| 23 | + */ |
| 24 | +public final class Require { |
| 25 | + |
| 26 | + private Require() { |
| 27 | + // No instances allowed |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Throws an {@link IllegalArgumentException} if {@code object} is |
| 32 | + * {@literal null}. |
| 33 | + * |
| 34 | + * @return {@code object} |
| 35 | + */ |
| 36 | + public static <T> T notNull(final T object) { |
| 37 | + return notNull(object, "parameter must not be null"); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Throws an {@link IllegalArgumentException} if {@code object} is |
| 42 | + * {@literal null}. |
| 43 | + * |
| 44 | + * @param message |
| 45 | + * exception message |
| 46 | + * @return {@code object} |
| 47 | + */ |
| 48 | + public static <T> T notNull(final T object, final String message) { |
| 49 | + if (object == null) { |
| 50 | + throw new IllegalArgumentException(message); |
| 51 | + } |
| 52 | + return object; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Throws an {@link IllegalArgumentException} if {@code value} is negative. |
| 57 | + * |
| 58 | + * @return {@code value} |
| 59 | + */ |
| 60 | + public static int notNegative(final int value) { |
| 61 | + return notNegative(value, "parameter must not be negative"); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Throws an {@link IllegalArgumentException} if {@code value} is negative. |
| 66 | + * |
| 67 | + * @param message |
| 68 | + * exception message |
| 69 | + * @return {@code value} |
| 70 | + */ |
| 71 | + public static int notNegative(final int value, final String message) { |
| 72 | + if (value < 0) { |
| 73 | + throw new IllegalArgumentException(message); |
| 74 | + } |
| 75 | + return value; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Throws an {@link IndexOutOfBoundsException} if {@code index} is negative |
| 80 | + * or equal to or greater than {@code arrayLength}. |
| 81 | + * |
| 82 | + * @return {@code index} |
| 83 | + */ |
| 84 | + public static int validArrayIndex(final int index, final int arrayLength) { |
| 85 | + return validArrayIndex(index, arrayLength, "array index out of range"); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Throws an {@link IndexOutOfBoundsException} if {@code index} is negative |
| 90 | + * or equal to or greater than {@code arrayLength}. |
| 91 | + * |
| 92 | + * @param message |
| 93 | + * exception message |
| 94 | + * @return {@code index} |
| 95 | + */ |
| 96 | + public static int validArrayIndex(final int index, final int arrayLength, |
| 97 | + final String message) { |
| 98 | + if (index < 0 || index >= arrayLength) { |
| 99 | + throw new IndexOutOfBoundsException(message); |
| 100 | + } |
| 101 | + return index; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Throws an {@link IndexOutOfBoundsException} if {@code sliceFrom} or |
| 106 | + * {@code sliceLength} is negative or the sum of both is greater than |
| 107 | + * {@code arrayLength}. Note that this means that a slice of length zero |
| 108 | + * starting at array length is a valid slice. |
| 109 | + */ |
| 110 | + public static void validArraySlice(final int sliceFrom, |
| 111 | + final int sliceLength, final int arrayLength) { |
| 112 | + validArraySlice(sliceFrom, sliceLength, arrayLength, |
| 113 | + "array slice out of range"); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Throws an {@link IndexOutOfBoundsException} if {@code sliceFrom} or |
| 118 | + * {@code sliceLength} is negative or the sum of both is greater than |
| 119 | + * {@code arrayLength}. Note that this means that a slice of length zero |
| 120 | + * starting at array length is a valid slice. |
| 121 | + * |
| 122 | + * |
| 123 | + * @param message |
| 124 | + * exception message |
| 125 | + */ |
| 126 | + public static void validArraySlice(final int sliceFrom, |
| 127 | + final int sliceLength, final int arrayLength, final String message) { |
| 128 | + if (sliceFrom < 0 || sliceLength < 0) { |
| 129 | + throw new IndexOutOfBoundsException(message); |
| 130 | + } |
| 131 | + if (sliceFrom + sliceLength > arrayLength) { |
| 132 | + throw new IndexOutOfBoundsException(message); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments