Skip to content

Commit beac8b5

Browse files
committed
Add utility class for method argument checking
Introduces the `Require` class which provides a basic set of static methods which are useful for validating method arguments. These methods are: - `notNull` - `notNegative` - `validArrayIndex` - `validArraySlice` All methods exist in two versions: one that outputs a default exception message and second one which allows the caller to customize the exception message.
1 parent 973fa51 commit beac8b5

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertSame;
20+
21+
import org.junit.Test;
22+
23+
/**
24+
* Tests for class {@link Require}.
25+
*
26+
* @author Christoph Böhme
27+
*
28+
*/
29+
public final class RequireTest {
30+
31+
@Test(expected = IllegalArgumentException.class)
32+
public void notNullShouldThrowIllegalArgumentExceptionIfArgIsNull() {
33+
Require.notNull(null);
34+
}
35+
36+
@Test
37+
public void notNullShouldReturnArgIfArgIsNotNull() {
38+
final Object obj = new Object();
39+
assertSame(obj, Require.notNull(obj));
40+
}
41+
42+
@Test(expected = IllegalArgumentException.class)
43+
public void notNegativeShouldThrowIllegalArgumentExceptionIfArgIsNegative() {
44+
Require.notNegative(-1);
45+
}
46+
47+
@Test
48+
public void notNegativeShouldReturnArgIfArgIsNotNegative() {
49+
assertEquals(0, Require.notNegative(0));
50+
}
51+
52+
@Test(expected = IndexOutOfBoundsException.class)
53+
public void validArrayIndexShouldThrowIndexOutOfBoundsExceptionIfIndexIsNegative() {
54+
Require.validArrayIndex(-1, 2);
55+
}
56+
57+
@Test(expected = IndexOutOfBoundsException.class)
58+
public void validArrayIndexShouldThrowIndexOutOfBoundsExceptionIfIndexIsGreaterThanArrayLength() {
59+
Require.validArrayIndex(2, 2);
60+
}
61+
62+
@Test
63+
public void validArrayIndexShouldDoNothingIfIndexIsWithinArrayBounds() {
64+
assertEquals(1, Require.validArrayIndex(1, 2));
65+
}
66+
67+
@Test(expected = IndexOutOfBoundsException.class)
68+
public void validArraySliceShouldThrowIndexOutOfBoundsExceptionIfIndexIsNegative() {
69+
Require.validArraySlice(-1, 1, 2);
70+
}
71+
72+
@Test(expected = IndexOutOfBoundsException.class)
73+
public void validArraySliceShouldThrowIndexOutOfBoundsExceptionIfLengthIsNegative() {
74+
Require.validArraySlice(0, -1, 2);
75+
}
76+
77+
@Test(expected = IndexOutOfBoundsException.class)
78+
public void validArraySliceShouldThrowIndexOutOfBoundsExceptionIfIndexPlusLengthIsGreaterThanArrayLength() {
79+
Require.validArraySlice(1, 2, 2);
80+
}
81+
82+
@Test
83+
public void validArraySliceShouldDoNothingIfIndexAndLengthAreWithinArrayBounds() {
84+
Require.validArraySlice(0, 1, 2);
85+
}
86+
87+
}

0 commit comments

Comments
 (0)