01: package com.sun.portal.app.filesharing.util;
02:
03: import java.io.UnsupportedEncodingException;
04:
05: /**
06: * @author Alejandro Abdelnur
07: */
08: public class ParameterCheck {
09:
10: /**
11: * Verifies if a parameter is NULL. If NULL throws an IllegalArgumentException.
12: * <p>
13: * @param paramName name of the parameter, for the exception message.
14: * @param param value of the parameter to check for NULL
15: * @throws IllegalArgumentException thrown if the parameter is NULL.
16: *
17: */
18: public static void checkNotNull(String paramName, Object param) {
19: if (param == null) {
20: throw new IllegalArgumentException("Parameter '"
21: + paramName + "' cannot be null");
22: }
23: }
24:
25: /**
26: * Verifies if a parameter is not empty. If empty throws an IllegalArgumentException.
27: * <p>
28: * @param paramName name of the parameter, for the exception message.
29: * @param param value of the parameter to check for not empty.
30: * @param trim indicates if the parameter value should be trimmed before check.
31: * @throws IllegalArgumentException thrown if the parameter is NULL.
32: *
33: */
34: public static void checkNotEmpty(String paramName, String param,
35: boolean trim) {
36: checkNotNull(paramName, param);
37: param = (trim) ? param.trim() : param;
38: if (param.length() == 0) {
39: throw new IllegalArgumentException("Parameter '"
40: + paramName + "' cannot be an empty String");
41: }
42: }
43:
44: /**
45: * decoding parameter by given charset
46: * @param s String for decoding
47: * @param charset The target charset
48: * @return return decoded string if success,
49: * or original string including null.
50: */
51: public static String decodeCharset(String s, String charset) {
52: if (s == null) {
53: return null;
54: }
55: if (charset == null) {
56: return s;
57: }
58:
59: try {
60: byte buf[] = s.getBytes("ISO-8859-1");
61: return new String(buf, 0, buf.length, charset);
62: } catch (UnsupportedEncodingException uee) {
63: return s;
64: }
65: }
66:
67: }
|