1 /*
2 * Copyright 2011-2012 smartics, Kronseder & Reiner GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package de.smartics.util.lang;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.annotation.concurrent.NotThreadSafe;
23
24 /**
25 * Collects exceptions. Especially useful for builders.
26 */
27 @NotThreadSafe
28 public final class ArgumentChecker implements Serializable
29 {
30 // ********************************* Fields *********************************
31
32 // --- constants ------------------------------------------------------------
33
34 /**
35 * The class version identifier.
36 */
37 private static final long serialVersionUID = 1L;
38
39 // --- members --------------------------------------------------------------
40
41 /**
42 * The message to prepend to the message if throwables have been collected.
43 *
44 * @serial
45 */
46 private final String message;
47
48 /**
49 * The collected throwables.
50 *
51 * @serial
52 */
53 private final List<Throwable> throwables = new ArrayList<Throwable>();
54
55 // ****************************** Initializer *******************************
56
57 // ****************************** Constructors ******************************
58
59 /**
60 * Default constructor.
61 *
62 * @param message the message to prepend to the message if throwables have
63 * been collected.
64 * @throws IllegalArgumentException if message is blank.
65 */
66 public ArgumentChecker(final String message) throws IllegalArgumentException
67 {
68 Arguments.checkNotBlank("message", message);
69
70 this.message = message;
71 }
72
73 // ****************************** Inner Classes *****************************
74
75 // ********************************* Methods ********************************
76
77 // --- init -----------------------------------------------------------------
78
79 // --- get&set --------------------------------------------------------------
80
81 // --- business -------------------------------------------------------------
82
83 /**
84 * Checks if {@code value} is <code>null</code>.
85 *
86 * @param name the name of the argument of error reporting. Not used if no
87 * exception is thrown. May be <code>null</code>, although not
88 * recommended.
89 * @param value the value of the argument to check to be not {@code null}.
90 */
91 public void notNull(final String name, final Object value)
92 {
93 try
94 {
95 Arguments.checkNotNull(name, value);
96 }
97 catch (final NullPointerException e) // NOPMD
98 {
99 throwables.add(e);
100 }
101 }
102
103 /**
104 * Checks if {@code value} is blank.
105 *
106 * @param name the name of the argument of error reporting. Not used if no
107 * exception is thrown. May be <code>null</code>, although not
108 * recommended.
109 * @param value the value of the argument to check to be not blank.
110 */
111 public void notBlank(final String name, final String value)
112 {
113 try
114 {
115 Arguments.checkNotBlank(name, value);
116 }
117 catch (final IllegalArgumentException e)
118 {
119 throwables.add(e);
120 }
121 }
122
123 /**
124 * Checks if {@code value} is blank except <code>null</code>.
125 *
126 * @param name the name of the argument of error reporting. Not used if no
127 * exception is thrown. May be <code>null</code>, although not
128 * recommended.
129 * @param value the value of the argument to check to be not blank but may be
130 * <code>null</code>.
131 */
132 public void notBlankExceptNull(final String name, final String value)
133 {
134 try
135 {
136 Arguments.checkNotBlankExceptNull(name, value);
137 }
138 catch (final IllegalArgumentException e)
139 {
140 throwables.add(e);
141 }
142 }
143
144 /**
145 * Adds a throwable.
146 *
147 * @param t the throwable to add.
148 */
149 public void add(final Throwable t)
150 {
151 throwables.add(t);
152 }
153
154 /**
155 * Checks if an exception has been encountered.
156 *
157 * @throws IllegalArgumentException if at least on exception has been
158 * encountered.
159 */
160 public void check() throws IllegalArgumentException
161 {
162 if (!throwables.isEmpty())
163 {
164 final StringBuilder buffer = new StringBuilder(128);
165 buffer.append(this.message);
166
167 for (final Throwable t : throwables)
168 {
169 buffer.append('\n').append(t.getLocalizedMessage());
170 }
171
172 final String message = buffer.toString();
173 throw new IllegalArgumentException(message);
174 }
175 }
176
177 // --- object basics --------------------------------------------------------
178
179 }