1 /* 2 * Copyright 2007 Kasper B. Graversen 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 org.supercsv.comment; 17 18 import java.util.regex.Pattern; 19 import java.util.regex.PatternSyntaxException; 20 21 /** 22 * CommentMatcher that matches lines that match a specified regular expression. 23 */ 24 public class CommentMatches implements CommentMatcher { 25 26 private final Pattern pattern; 27 28 /** 29 * Constructs a new <tt>CommentMatches</tt> comment matcher. Ensure that the regex is efficient (ideally matching start/end 30 * characters) as a complex regex can significantly slow down reading. 31 * 32 * @param regex 33 * the regular expression a line must match to be a comment 34 * @throws NullPointerException 35 * if regex is null 36 * @throws IllegalArgumentException 37 * if regex is empty 38 * @throws PatternSyntaxException 39 * if the regex is invalid 40 */ 41 public CommentMatches(final String regex) { 42 if( regex == null ) { 43 throw new NullPointerException("regex should not be null"); 44 } else if( regex.length() == 0 ) { 45 throw new IllegalArgumentException("regex should not be empty"); 46 } 47 this.pattern = Pattern.compile(regex); 48 } 49 50 /** 51 * {@inheritDoc} 52 */ 53 public boolean isComment(String line) { 54 return pattern.matcher(line).matches(); 55 } 56 57 }