View Javadoc
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.cellprocessor.constraint;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.supercsv.cellprocessor.CellProcessorAdaptor;
23  import org.supercsv.cellprocessor.ift.CellProcessor;
24  import org.supercsv.cellprocessor.ift.StringCellProcessor;
25  import org.supercsv.exception.SuperCsvCellProcessorException;
26  import org.supercsv.exception.SuperCsvConstraintViolationException;
27  import org.supercsv.util.CsvContext;
28  
29  /**
30   * Converts the input to a String and ensures that the input contains at least one of the specified substrings.
31   * 
32   * @since 1.10
33   * @author Kasper B. Graversen
34   * @author James Bassett
35   */
36  public class RequireSubStr extends CellProcessorAdaptor implements StringCellProcessor {
37  	
38  	private final List<String> requiredSubStrings = new ArrayList<String>();
39  	
40  	/**
41  	 * Converts the input to a String and ensures that the input contains at least one of the specified substrings.
42  	 * 
43  	 * @param requiredSubStrings
44  	 *            the required substrings
45  	 * @throws NullPointerException
46  	 *             if requiredSubStrings or one of its elements is null
47  	 * @throws IllegalArgumentException
48  	 *             if requiredSubStrings is empty
49  	 */
50  	public RequireSubStr(final String... requiredSubStrings) {
51  		super();
52  		checkPreconditions(requiredSubStrings);
53  		checkAndAddRequiredSubStrings(requiredSubStrings);
54  	}
55  	
56  	/**
57  	 * Converts the input to a String, ensures that the input contains at least one of the specified substrings, then
58  	 * calls the next processor in the chain.
59  	 * 
60  	 * @param requiredSubStrings
61  	 *            the List of required substrings
62  	 * @param next
63  	 *            the next processor in the chain
64  	 * @throws NullPointerException
65  	 *             if requiredSubStrings, one of its elements or next is null
66  	 * @throws IllegalArgumentException
67  	 *             if requiredSubStrings is empty
68  	 */
69  	public RequireSubStr(final List<String> requiredSubStrings, final CellProcessor next) {
70  		super(next);
71  		checkPreconditions(requiredSubStrings);
72  		checkAndAddRequiredSubStrings(requiredSubStrings);
73  	}
74  	
75  	/**
76  	 * Converts the input to a String, ensures that the input contains the specified substring, then calls the next
77  	 * processor in the chain.
78  	 * 
79  	 * @param requiredSubString
80  	 *            the required substring
81  	 * @param next
82  	 *            the next processor in the chain
83  	 * @throws NullPointerException
84  	 *             if requiredSubString or next is null
85  	 */
86  	public RequireSubStr(final String requiredSubString, final CellProcessor next) {
87  		super(next);
88  		checkPreconditions(requiredSubString);
89  		checkAndAddRequiredSubStrings(requiredSubString);
90  	}
91  	
92  	/**
93  	 * Converts the input to a String, ensures that the input contains at least one of the specified substrings, then
94  	 * calls the next processor in the chain.
95  	 * 
96  	 * @param requiredSubStrings
97  	 *            the List of required substrings
98  	 * @param next
99  	 *            the next processor in the chain
100 	 * @throws NullPointerException
101 	 *             if requiredSubStrings, one of its elements or next is null
102 	 * @throws IllegalArgumentException
103 	 *             if requiredSubStrings is empty
104 	 */
105 	public RequireSubStr(final String[] requiredSubStrings, final CellProcessor next) {
106 		super(next);
107 		checkPreconditions(requiredSubStrings);
108 		checkAndAddRequiredSubStrings(requiredSubStrings);
109 	}
110 	
111 	/**
112 	 * Checks the preconditions for creating a new RequireSubStr processor with an array of Strings.
113 	 * 
114 	 * @param requiredSubStrings
115 	 *            the required substrings
116 	 * @throws NullPointerException
117 	 *             if requiredSubStrings or one of its elements is null
118 	 * @throws IllegalArgumentException
119 	 *             if requiredSubStrings is empty
120 	 */
121 	private static void checkPreconditions(String... requiredSubStrings) {
122 		if( requiredSubStrings == null ) {
123 			throw new NullPointerException("requiredSubStrings array should not be null");
124 		} else if( requiredSubStrings.length == 0 ) {
125 			throw new IllegalArgumentException("requiredSubStrings array should not be empty");
126 		}
127 	}
128 	
129 	/**
130 	 * Checks the preconditions for creating a new RequireSubStr processor with a List of Strings.
131 	 * 
132 	 * @param requiredSubStrings
133 	 *            the required substrings
134 	 * @throws NullPointerException
135 	 *             if requiredSubStrings or one of its elements is null
136 	 * @throws IllegalArgumentException
137 	 *             if requiredSubStrings is empty
138 	 */
139 	private static void checkPreconditions(List<String> requiredSubStrings) {
140 		if( requiredSubStrings == null ) {
141 			throw new NullPointerException("requiredSubStrings List should not be null");
142 		} else if( requiredSubStrings.isEmpty() ) {
143 			throw new IllegalArgumentException("requiredSubStrings List should not be empty");
144 		}
145 	}
146 	
147 	/**
148 	 * Adds each required substring, checking that it's not null.
149 	 * 
150 	 * @param requiredSubStrings
151 	 *            the required substrings
152 	 * @throws NullPointerException
153 	 *             if a required substring is null
154 	 */
155 	private void checkAndAddRequiredSubStrings(final List<String> requiredSubStrings) {
156 		for( String required : requiredSubStrings ) {
157 			if( required == null ) {
158 				throw new NullPointerException("required substring should not be null");
159 			}
160 			this.requiredSubStrings.add(required);
161 		}
162 	}
163 	
164 	/**
165 	 * Adds each required substring, checking that it's not null.
166 	 * 
167 	 * @param requiredSubStrings
168 	 *            the required substrings
169 	 * @throws NullPointerException
170 	 *             if a required substring is null
171 	 */
172 	private void checkAndAddRequiredSubStrings(final String... requiredSubStrings) {
173 		checkAndAddRequiredSubStrings(Arrays.asList(requiredSubStrings));
174 	}
175 	
176 	/**
177 	 * {@inheritDoc}
178 	 * 
179 	 * @throws SuperCsvCellProcessorException
180 	 *             if value is null
181 	 * @throws SuperCsvConstraintViolationException
182 	 *             if value doesn't contain any of the required substrings
183 	 */
184 	public Object execute(final Object value, final CsvContext context) {
185 		validateInputNotNull(value, context);
186 		
187 		final String stringValue = value.toString();
188 		
189 		for( final String required : requiredSubStrings ) {
190 			if( stringValue.contains(required) ) {
191 				return next.execute(value, context); // just need to match a single substring
192 			}
193 		}
194 		
195 		throw new SuperCsvConstraintViolationException(String.format("'%s' does not contain any of the required substrings", value),
196 			context, this);
197 	}
198 }