@@ -126,6 +126,42 @@ public class CSVFormat implements Cloneable, Serializable {
126126 this .emptyLinesIgnored = emptyLinesIgnored ;
127127 }
128128
129+ /**
130+ * Returns true if the given character is a line break character.
131+ *
132+ * @param c the character to check
133+ *
134+ * @return true if <code>c</code> is a line break character
135+ */
136+ private static boolean isLineBreak (char c ) {
137+ return c == '\n' || c == '\r' ;
138+ }
139+
140+ /**
141+ * Verifies the consistency of the parameters and throws an IllegalArgumentException if necessary.
142+ */
143+ void validate () throws IllegalArgumentException {
144+ if (delimiter == encapsulator ) {
145+ throw new IllegalArgumentException ("The encapsulator character and the delimiter cannot be the same (\" " + encapsulator + "\" )" );
146+ }
147+
148+ if (delimiter == escape ) {
149+ throw new IllegalArgumentException ("The escape character and the delimiter cannot be the same (\" " + escape + "\" )" );
150+ }
151+
152+ if (delimiter == commentStart ) {
153+ throw new IllegalArgumentException ("The comment start character and the delimiter cannot be the same (\" " + commentStart + "\" )" );
154+ }
155+
156+ if (encapsulator != DISABLED && encapsulator == commentStart ) {
157+ throw new IllegalArgumentException ("The comment start character and the encapsulator cannot be the same (\" " + commentStart + "\" )" );
158+ }
159+
160+ if (escape != DISABLED && escape == commentStart ) {
161+ throw new IllegalArgumentException ("The comment start and the escape character cannot be the same (\" " + commentStart + "\" )" );
162+ }
163+ }
164+
129165 /**
130166 * Returns the character delimiting the values (typically ';', ',' or '\t').
131167 *
@@ -140,8 +176,13 @@ public char getDelimiter() {
140176 *
141177 * @param delimiter the delimiter character
142178 * @return A copy of this format using the specified delimiter character
179+ * @throws IllegalArgumentException thrown if the specified character is a line break
143180 */
144181 public CSVFormat withDelimiter (char delimiter ) {
182+ if (isLineBreak (delimiter )) {
183+ throw new IllegalArgumentException ("The delimiter cannot be a line break" );
184+ }
185+
145186 CSVFormat format = clone ();
146187 format .delimiter = delimiter ;
147188 return format ;
@@ -161,8 +202,13 @@ public char getEncapsulator() {
161202 *
162203 * @param encapsulator the encapsulator character
163204 * @return A copy of this format using the specified encapsulator character
205+ * @throws IllegalArgumentException thrown if the specified character is a line break
164206 */
165207 public CSVFormat withEncapsulator (char encapsulator ) {
208+ if (isLineBreak (encapsulator )) {
209+ throw new IllegalArgumentException ("The encapsulator cannot be a line break" );
210+ }
211+
166212 CSVFormat format = clone ();
167213 format .encapsulator = encapsulator ;
168214 return format ;
@@ -186,8 +232,13 @@ public char getCommentStart() {
186232 *
187233 * @param commentStart the comment start marker
188234 * @return A copy of this format using the specified character as the comment start marker
235+ * @throws IllegalArgumentException thrown if the specified character is a line break
189236 */
190237 public CSVFormat withCommentStart (char commentStart ) {
238+ if (isLineBreak (commentStart )) {
239+ throw new IllegalArgumentException ("The comment start character cannot be a line break" );
240+ }
241+
191242 CSVFormat format = clone ();
192243 format .commentStart = commentStart ;
193244 return format ;
@@ -216,8 +267,13 @@ public char getEscape() {
216267 *
217268 * @param escape the escape character
218269 * @return A copy of this format using the specified escape character
270+ * @throws IllegalArgumentException thrown if the specified character is a line break
219271 */
220272 public CSVFormat withEscape (char escape ) {
273+ if (isLineBreak (escape )) {
274+ throw new IllegalArgumentException ("The escape character cannot be a line break" );
275+ }
276+
221277 CSVFormat format = clone ();
222278 format .escape = escape ;
223279 return format ;
0 commit comments