001: /*
002: * $Id: MandatoryArgumentChecker.java,v 1.22 2007/03/16 09:54:58 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common;
008:
009: /**
010: * Utility class used to check mandatory method arguments.
011: *
012: * @version $Revision: 1.22 $ $Date: 2007/03/16 09:54:58 $
013: * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
014: *
015: * @since XINS 1.0.0
016: */
017: public final class MandatoryArgumentChecker {
018:
019: /**
020: * Constructs a new <code>MandatoryArgumentChecker</code>. This constructor
021: * is private since this no instances of this class should be created.
022: */
023: private MandatoryArgumentChecker() {
024: // empty
025: }
026:
027: /**
028: * Checks if the specified argument value is <code>null</code>. If it is
029: * <code>null</code>, then an {@link IllegalArgumentException} is thrown.
030: *
031: * @param argName
032: * the name of the argument, cannot be <code>null</code>.
033: *
034: * @param argValue
035: * the value of the argument.
036: *
037: * @throws IllegalArgumentException
038: * if <code>argValue == null</code>.
039: */
040: public static void check(String argName, Object argValue)
041: throws IllegalArgumentException {
042:
043: // If both are non-null everything is okay, just short-circuit
044: if (argName != null && argValue != null) {
045: return;
046: }
047:
048: // Check if the name is null
049: if (argName == null) {
050: throw Utils.logProgrammingError("argName == null");
051: }
052:
053: // Otherwise the value is null
054: if (argValue == null) {
055: throw new IllegalArgumentException(argName + " == null");
056: }
057: }
058:
059: /**
060: * Checks if any of the two specified argument values is <code>null</code>.
061: * If at least one value is <code>null</code>, then an
062: * {@link IllegalArgumentException} is thrown.
063: *
064: * @param argName1
065: * the name of the first argument, cannot be <code>null</code>.
066: *
067: * @param argValue1
068: * the value of the first argument.
069: *
070: * @param argName2
071: * the name of the second argument, cannot be <code>null</code>.
072: *
073: * @param argValue2
074: * the value of the second argument.
075: *
076: * @throws IllegalArgumentException
077: * if <code>argValue1 == null || argValue2 == null</code>.
078: */
079: public static void check(String argName1, Object argValue1,
080: String argName2, Object argValue2)
081: throws IllegalArgumentException {
082:
083: // If all are non-null everything is okay, just short-circuit
084: if (argName1 != null && argValue1 != null && argName2 != null
085: && argValue2 != null) {
086: return;
087: }
088:
089: String message = "";
090:
091: // Check if any of the names is null
092: if (argName1 == null && argName2 == null) {
093: message = "argName1 == null && argName2 == null";
094: throw Utils.logProgrammingError(message);
095: }
096:
097: // Otherwise (at least) one of the values must be null
098: if (argValue1 == null && argValue2 == null) {
099: message = argName1 + " == null && " + argName2 + " == null";
100: } else {
101: check(argName1, argValue1);
102: check(argName2, argValue2);
103: }
104: throw new IllegalArgumentException(message);
105: }
106:
107: /**
108: * Checks if any of the three specified argument values is
109: * <code>null</code>. If at least one value is <code>null</code>, then an
110: * {@link IllegalArgumentException} is thrown.
111: *
112: * @param argName1
113: * the name of the first argument, cannot be <code>null</code>.
114: *
115: * @param argValue1
116: * the value of the first argument.
117: *
118: * @param argName2
119: * the name of the second argument, cannot be <code>null</code>.
120: *
121: * @param argValue2
122: * the value of the second argument.
123: *
124: * @param argName3
125: * the name of the third argument, cannot be <code>null</code>.
126: *
127: * @param argValue3
128: * the value of the third argument.
129: *
130: * @throws IllegalArgumentException
131: * if <code>argValue1 == null
132: * || argValue2 == null
133: * || argValue3 == null</code>.
134: */
135: public static void check(String argName1, Object argValue1,
136: String argName2, Object argValue2, String argName3,
137: Object argValue3) throws IllegalArgumentException {
138:
139: // If all are non-null everything is okay, just short-circuit
140: if (argName1 != null && argValue1 != null && argName2 != null
141: && argValue2 != null && argName3 != null
142: && argValue3 != null) {
143: return;
144: }
145:
146: // Check if any of the names is null
147: String message = "";
148: if (argName1 == null && argName2 == null && argName3 == null) {
149: message = "argName1 == null && " + "argName2 == null && "
150: + "argName3 == null";
151: throw Utils.logProgrammingError(message);
152: }
153:
154: // Otherwise (at least) one of the values must be null
155: if (argValue1 == null && argValue2 == null && argValue3 == null) {
156: message = argName1 + " == null && " + argName2
157: + " == null && " + argName3 + " == null";
158: } else {
159: check(argName1, argValue1, argName2, argValue2);
160: check(argName1, argValue1, argName3, argValue3);
161: check(argName2, argValue2, argName3, argValue3);
162: }
163: throw new IllegalArgumentException(message);
164: }
165:
166: /**
167: * Checks if any of the four specified argument values is
168: * <code>null</code>. If at least one value is <code>null</code>, then an
169: * {@link IllegalArgumentException} is thrown.
170: *
171: * @param argName1
172: * the name of the first argument, cannot be <code>null</code>.
173: *
174: * @param argValue1
175: * the value of the first argument.
176: *
177: * @param argName2
178: * the name of the second argument, cannot be <code>null</code>.
179: *
180: * @param argValue2
181: * the value of the second argument.
182: *
183: * @param argName3
184: * the name of the third argument, cannot be <code>null</code>.
185: *
186: * @param argValue3
187: * the value of the third argument.
188: *
189: * @param argName4
190: * the name of the fourth argument, cannot be <code>null</code>.
191: *
192: * @param argValue4
193: * the value of the fourth argument.
194: *
195: * @throws IllegalArgumentException
196: * if <code>argValue1 == null || argValue2 == null
197: * || argValue3 == null || argValue4 == null</code>.
198: */
199: public static void check(String argName1, Object argValue1,
200: String argName2, Object argValue2, String argName3,
201: Object argValue3, String argName4, Object argValue4)
202: throws IllegalArgumentException {
203:
204: // If all are non-null everything is okay, just short-circuit
205: if (argName1 != null && argValue1 != null && argName2 != null
206: && argValue2 != null && argName3 != null
207: && argValue3 != null && argName4 != null
208: && argValue4 != null) {
209: return;
210: }
211:
212: // Check if any of the names is null
213: String message = "";
214: if (argName1 == null && argName2 == null && argName3 == null
215: && argName4 == null) {
216: message = "argName1 == null && argName2 == null && "
217: + "argName3 == null && argName4 == null";
218: throw Utils.logProgrammingError(message);
219: }
220:
221: // Otherwise (at least) one of the values must be null
222: if (argValue1 == null && argValue2 == null && argValue3 == null
223: && argValue4 == null) {
224: message = argName1 + " == null && " + argName2
225: + " == null && " + argName3 + " == null && "
226: + argName4 + " == null";
227: } else {
228: check(argName1, argValue1, argName2, argValue2, argName3,
229: argValue3);
230: check(argName1, argValue1, argName2, argValue2, argName4,
231: argValue4);
232: check(argName2, argValue2, argName3, argValue3, argName4,
233: argValue4);
234: }
235: throw new IllegalArgumentException(message);
236: }
237: }
|