1 package org.codehaus.mojo.jaxb2.shared;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 /**
23 * Simple argument validator, inspired by the commons-lang.
24 *
25 * @author <a href="mailto:lj@jguru.se">Lennart Jörelid</a>
26 * @since 2.0
27 */
28 public final class Validate {
29
30 /**
31 * Hide constructor for utility classes.
32 */
33 private Validate() {
34 }
35
36 /**
37 * Validates that the supplied object is not null, and throws a NullPointerException otherwise.
38 *
39 * @param object The object to validate for {@code null}-ness.
40 * @param argumentName The argument name of the object to validate. If supplied (i.e. non-{@code null}),
41 * this value is used in composing a better exception message.
42 */
43 public static void notNull(final Object object, final String argumentName) {
44 if (object == null) {
45 throw new NullPointerException(getMessage("null", argumentName));
46 }
47 }
48
49 /**
50 * Validates that the supplied object is not null, and throws an IllegalArgumentException otherwise.
51 *
52 * @param aString The string to validate for emptyness.
53 * @param argumentName The argument name of the object to validate.
54 * If supplied (i.e. non-{@code null}), this value is used in composing
55 * a better exception message.
56 */
57 public static void notEmpty(final String aString, final String argumentName) {
58
59 // Check sanity
60 notNull(aString, argumentName);
61
62 if (aString.length() == 0) {
63 throw new IllegalArgumentException(getMessage("empty", argumentName));
64 }
65 }
66
67 /**
68 * Validates that the supplied condition is true, and throws an IllegalArgumentException otherwise.
69 *
70 * @param condition The condition to validate for truth.
71 * @param message The exception message used within the IllegalArgumentException if the condition is false.
72 */
73 public static void isTrue(final boolean condition, final String message) {
74
75 if (!condition) {
76 throw new IllegalArgumentException(message);
77 }
78 }
79
80 //
81 // Private helpers
82 //
83
84 private static String getMessage(final String exceptionDefinition, final String argumentName) {
85 return "Cannot handle "
86 + exceptionDefinition
87 + (argumentName == null ? "" : " '" + argumentName + "'")
88 + " argument.";
89 }
90 }