|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Defense.java | 100% | 100% | 66.7% | 91.7% |
|
1 | // Copyright 2004, 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.hivemind.util; | |
16 | ||
17 | import org.apache.hivemind.HiveMindMessages; | |
18 | ||
19 | /** | |
20 | * Simple utilities for defensive programming. | |
21 | * | |
22 | * @author Howard M. Lewis Ship | |
23 | * @since 1.1 | |
24 | */ | |
25 | public final class Defense | |
26 | { | |
27 | 0 | private Defense() |
28 | { | |
29 | // Prevent instantiation | |
30 | } | |
31 | ||
32 | /** | |
33 | * Check for null parameter when not allowed. | |
34 | * | |
35 | * @throws NullPointerException | |
36 | * if parameter is null (the message indicates the name of the parameter). | |
37 | */ | |
38 | 5008 | public static void notNull(Object parameter, String parameterName) |
39 | { | |
40 | 5008 | if (parameter == null) |
41 | 1 | throw new NullPointerException(HiveMindMessages.paramNotNull(parameterName)); |
42 | } | |
43 | ||
44 | /** | |
45 | * Check that a parameter is not null, and assignable to a particular type. | |
46 | * | |
47 | * @throws ClassCastException | |
48 | * if not assignable (with a useable message) | |
49 | */ | |
50 | ||
51 | 3 | public static void isAssignable(Object parameter, Class expectedType, String parameterName) |
52 | { | |
53 | 3 | Defense.notNull(parameter, parameterName); |
54 | ||
55 | 3 | if (!expectedType.isAssignableFrom(parameter.getClass())) |
56 | 2 | throw new ClassCastException(HiveMindMessages.paramWrongType( |
57 | parameterName, | |
58 | parameter, | |
59 | expectedType)); | |
60 | } | |
61 | } |
|