001: /*
002: * Copyright 2002-2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.araneaframework.core;
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: /**
023: * <p>Assists in validating arguments.</p>
024: *
025: * <p>The class is based along the lines of JUnit. If an argument value is
026: * deemed invalid, an IllegalArgumentException is thrown. For example:</p>
027: *
028: * <pre>
029: * Assert.isTrue( i > 0, "The value must be greater than zero: ", i);
030: * Assert.notNull( surname, "The surname must not be null");
031: * </pre>
032: *
033: * <p>Copied from Jakarta Commons Lang for framework internal use.
034: * Please use the original from <a href="http://jakarta.apache.org/commons/lang/">http://jakarta.apache.org/commons/lang/</a>.</p>
035: *
036: * @author Ola Berg
037: * @author Stephen Colebourne
038: * @author Gary Gregory
039: * @author Norm Deane
040: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
041: */
042: public abstract class Assert {
043:
044: public static String this ToString(Object that) {
045: notNull(that,
046: "'this' can never be null, check what you passed to Assert!");
047: return " [this :: " + that.getClass().getName() + "]";
048: }
049:
050: public static void isTrue(Object that, boolean expression,
051: String message) {
052: if (expression == false) {
053: throw new IllegalArgumentException(message
054: + this ToString(that));
055: }
056: }
057:
058: public static void isTrue(boolean expression, String message) {
059: if (expression == false) {
060: throw new IllegalArgumentException(message);
061: }
062: }
063:
064: public static void notNull(Object object) {
065: if (object == null) {
066: throw new IllegalArgumentException(
067: "The object under assertion was null!");
068: }
069: }
070:
071: public static void notNull(Object that, Object object,
072: String message) {
073: if (object == null) {
074: throw new IllegalArgumentException(message
075: + this ToString(that));
076: }
077: }
078:
079: public static void notNull(Object object, String message) {
080: if (object == null) {
081: throw new IllegalArgumentException(message);
082: }
083: }
084:
085: public static void notNullParam(Object object, String parameterName) {
086: if (object == null) {
087: throw new IllegalArgumentException("Parameter '"
088: + parameterName + "' must not be null!");
089: }
090: }
091:
092: public static void notNullParam(Object that, Object object,
093: String parameterName) {
094: if (object == null) {
095: throw new IllegalArgumentException("Parameter '"
096: + parameterName + "' must not be null!"
097: + this ToString(that));
098: }
099: }
100:
101: public static void isInstanceOf(Object that, Class klass,
102: Object object, String message) {
103: if (object == null)
104: return;
105:
106: if (!klass.isAssignableFrom(object.getClass())) {
107: throw new IllegalArgumentException(message
108: + this ToString(that));
109: }
110: }
111:
112: public static void isInstanceOf(Class klass, Object object,
113: String message) {
114: if (object == null)
115: return;
116:
117: if (!klass.isAssignableFrom(object.getClass())) {
118: throw new IllegalArgumentException(message);
119: }
120: }
121:
122: public static void isInstanceOfParam(Object that, Class klass,
123: Object object, String parameterName) {
124: if (object == null)
125: return;
126:
127: if (!klass.isAssignableFrom(object.getClass())) {
128: throw new IllegalArgumentException("Parameter '"
129: + parameterName + "' must be of type '"
130: + klass.getName() + "' but is of type '"
131: + object.getClass().getName() + "'!"
132: + this ToString(that));
133: }
134: }
135:
136: public static void isInstanceOfParam(Class klass, Object object,
137: String parameterName) {
138: if (object == null)
139: return;
140:
141: if (!klass.isAssignableFrom(object.getClass())) {
142: throw new IllegalArgumentException("Parameter '"
143: + parameterName + "' must be of type '"
144: + klass.getName() + "' but is of type '"
145: + object.getClass().getName() + "'!");
146: }
147: }
148:
149: public static void notEmptyParam(String string, String parameterName) {
150: if (string == null || string.length() == 0) {
151: throw new IllegalArgumentException("Parameter '"
152: + parameterName + "' must not be empty!");
153: }
154: }
155:
156: public static void notEmptyParam(Object that, String string,
157: String parameterName) {
158: if (string == null || string.length() == 0) {
159: throw new IllegalArgumentException("Parameter '"
160: + parameterName + "' must not be empty!"
161: + this ToString(that));
162: }
163: }
164:
165: public static void notEmpty(Object[] array, String message) {
166: if (array == null || array.length == 0) {
167: throw new IllegalArgumentException(message);
168: }
169: }
170:
171: public static void notEmpty(Collection collection, String message) {
172: if (collection == null || collection.size() == 0) {
173: throw new IllegalArgumentException(message);
174: }
175: }
176:
177: public static void notEmpty(Map map, String message) {
178: if (map == null || map.size() == 0) {
179: throw new IllegalArgumentException(message);
180: }
181: }
182:
183: public static void notEmpty(String string, String message) {
184: if (string == null || string.length() == 0) {
185: throw new IllegalArgumentException(message);
186: }
187: }
188:
189: public static void noNullElementsParam(Collection collection,
190: String param) {
191: notNullParam(collection, param);
192: int i = 0;
193: for (Iterator it = collection.iterator(); it.hasNext();) {
194: if (it.next() == null)
195: throw new IllegalArgumentException(
196: "The validated collection contains null element at index: '"
197: + i + "'!");
198: i++;
199: }
200: }
201:
202: public static void noNullElements(Collection collection,
203: String message) {
204: notNull(collection);
205: int i = 0;
206: for (Iterator it = collection.iterator(); it.hasNext();) {
207: if (it.next() == null)
208: throw new IllegalArgumentException(message);
209: i++;
210: }
211: }
212:
213: public static void noNullElementsParam(Object that,
214: Collection collection, String param) {
215: notNullParam(collection, param);
216: int i = 0;
217: for (Iterator it = collection.iterator(); it.hasNext();) {
218: if (it.next() == null)
219: throw new IllegalArgumentException(
220: "The validated collection contains null element at index: '"
221: + i + "'!" + this ToString(that));
222: i++;
223: }
224: }
225:
226: public static void noNullElements(Object that,
227: Collection collection, String message) {
228: notNull(collection);
229: int i = 0;
230: for (Iterator it = collection.iterator(); it.hasNext();) {
231: if (it.next() == null)
232: throw new IllegalArgumentException(message
233: + thisToString(that));
234: i++;
235: }
236: }
237: }
|