1 /***
2 * Simple Web Spider - <http://simplewebspider.sourceforge.net/>
3 * Copyright (C) 2009 <berendona@users.sourceforge.net>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 package simplespider.simplespider.util;
19
20 import java.util.Collection;
21 import java.util.Dictionary;
22 import java.util.Map;
23 import java.util.Properties;
24
25 public final class ValidityHelper {
26
27 public static void checkNotEmpty(final String name, final CharSequence value) {
28 checkNotEmptyInternal("name", name);
29 checkNotEmptyInternal(name, value);
30 }
31
32 public static void checkNotEmpty(final String name, final Collection<?> values) {
33 checkNotEmptyInternal("name", name);
34 checkNotNull(name, values);
35 if (isEmpty(values)) {
36 throw new IllegalArgumentException(name + " is empty");
37 }
38 }
39
40 private static void checkNotEmptyInternal(final String name, final CharSequence value) {
41 checkNotNullInternal(name, value);
42 if (isEmpty(value)) {
43 throw new IllegalArgumentException(name + " is empty");
44 }
45 }
46
47 public static void checkNotNull(final String name, final Object value) {
48 checkNotEmptyInternal("name", name);
49 if (value == null) {
50 throw new NullPointerException(name + " is null");
51 }
52 }
53
54 private static void checkNotNullInternal(final String name, final Object value) {
55 if (value == null) {
56 throw new NullPointerException(name + " is null");
57 }
58 }
59
60 public static boolean isEmpty(final CharSequence value) {
61 return value == null || value.length() == 0;
62 }
63
64 public static boolean isEmpty(final Collection<?> values) {
65 return values == null || values.size() == 0;
66 }
67
68 private ValidityHelper() {
69
70 }
71
72 public static <T, S> boolean isEmpty(final Map<T, S> values) {
73 return values == null || values.size() == 0;
74 }
75
76 public static <T, S> void checkNotEmpty(final String name, final Map<T, S> values) {
77 checkNotEmptyInternal("name", name);
78 checkNotNull(name, values);
79 if (isEmpty(values)) {
80 throw new IllegalArgumentException(name + " is empty");
81 }
82 }
83
84 public static <T, S> boolean isEmpty(final Dictionary<T, S> values) {
85 return values == null || values.size() == 0;
86 }
87
88 public static <T, S> void checkNotEmpty(final String name, final Dictionary<T, S> values) {
89 checkNotEmptyInternal("name", name);
90 checkNotNull(name, values);
91 if (isEmpty(values)) {
92 throw new IllegalArgumentException(name + " is empty");
93 }
94 }
95
96 public static boolean isEmpty(final Properties values) {
97 return values == null || values.size() == 0;
98 }
99
100 public static void checkNotEmpty(final String name, final Properties values) {
101 checkNotEmptyInternal("name", name);
102 checkNotNull(name, values);
103 if (isEmpty(values)) {
104 throw new IllegalArgumentException(name + " is empty");
105 }
106 }
107
108 public static boolean isEmpty(final char[] value) {
109 return value == null || value.length == 0;
110 }
111
112 public static void checkNotEmpty(final String name, final char[] values) {
113 checkNotEmptyInternal("name", name);
114 checkNotNull(name, values);
115 if (isEmpty(values)) {
116 throw new IllegalArgumentException(name + " is empty");
117 }
118 }
119
120 public static boolean isEmpty(final short[] value) {
121 return value == null || value.length == 0;
122 }
123
124 public static void checkNotEmpty(final String name, final short[] values) {
125 checkNotEmptyInternal("name", name);
126 checkNotNull(name, values);
127 if (isEmpty(values)) {
128 throw new IllegalArgumentException(name + " is empty");
129 }
130 }
131
132 public static boolean isEmpty(final int[] value) {
133 return value == null || value.length == 0;
134 }
135
136 public static void checkNotEmpty(final String name, final int[] values) {
137 checkNotEmptyInternal("name", name);
138 checkNotNull(name, values);
139 if (isEmpty(values)) {
140 throw new IllegalArgumentException(name + " is empty");
141 }
142 }
143
144 public static boolean isEmpty(final long[] value) {
145 return value == null || value.length == 0;
146 }
147
148 public static void checkNotEmpty(final String name, final long[] values) {
149 checkNotEmptyInternal("name", name);
150 checkNotNull(name, values);
151 if (isEmpty(values)) {
152 throw new IllegalArgumentException(name + " is empty");
153 }
154 }
155
156 public static boolean isEmpty(final boolean[] value) {
157 return value == null || value.length == 0;
158 }
159
160 public static void checkNotEmpty(final String name, final boolean[] values) {
161 checkNotEmptyInternal("name", name);
162 checkNotNull(name, values);
163 if (isEmpty(values)) {
164 throw new IllegalArgumentException(name + " is empty");
165 }
166 }
167
168 public static boolean isEmpty(final float[] value) {
169 return value == null || value.length == 0;
170 }
171
172 public static void checkNotEmpty(final String name, final float[] values) {
173 checkNotEmptyInternal("name", name);
174 checkNotNull(name, values);
175 if (isEmpty(values)) {
176 throw new IllegalArgumentException(name + " is empty");
177 }
178 }
179
180 public static boolean isEmpty(final double[] value) {
181 return value == null || value.length == 0;
182 }
183
184 public static void checkNotEmpty(final String name, final double[] values) {
185 checkNotEmptyInternal("name", name);
186 checkNotNull(name, values);
187 if (isEmpty(values)) {
188 throw new IllegalArgumentException(name + " is empty");
189 }
190 }
191
192 public static boolean isEmpty(final Object[] value) {
193 return value == null || value.length == 0;
194 }
195
196 public static void checkNotEmpty(final String name, final Object[] values) {
197 checkNotEmptyInternal("name", name);
198 checkNotNull(name, values);
199 if (isEmpty(values)) {
200 throw new IllegalArgumentException(name + " is empty");
201 }
202 }
203
204 }