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.Collection;
19  
20  import org.supercsv.cellprocessor.CellProcessorAdaptor;
21  import org.supercsv.cellprocessor.ift.BoolCellProcessor;
22  import org.supercsv.cellprocessor.ift.CellProcessor;
23  import org.supercsv.cellprocessor.ift.DateCellProcessor;
24  import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
25  import org.supercsv.cellprocessor.ift.LongCellProcessor;
26  import org.supercsv.cellprocessor.ift.StringCellProcessor;
27  import org.supercsv.exception.SuperCsvConstraintViolationException;
28  import org.supercsv.util.CsvContext;
29  
30  /**
31   * This processor ensures that the input value is an element of a Collection. It differs from {@link IsIncludedIn} as
32   * the Collection is modifiable, so it can be used with other processors such as
33   * {@link org.supercsv.cellprocessor.Collector Collector} to enforce referential integrity.
34   * 
35   * @since 2.1.0
36   * @author James Bassett
37   */
38  public class IsElementOf extends CellProcessorAdaptor implements BoolCellProcessor, DateCellProcessor,
39  	DoubleCellProcessor, LongCellProcessor, StringCellProcessor {
40  	
41  	private final Collection<Object> collection;
42  	
43  	/**
44  	 * Constructs a new <tt>IsElementOf</tt>, which ensures that the input value is an element of a Collection.
45  	 * 
46  	 * @param collection
47  	 *            the collection to check
48  	 * @throws NullPointerException
49  	 *             if collection is null
50  	 */
51  	public IsElementOf(final Collection<Object> collection) {
52  		super();
53  		checkPreconditions(collection);
54  		this.collection = collection;
55  	}
56  	
57  	/**
58  	 * Constructs a new <tt>IsElementOf</tt>, which ensures that the input value is an element of a Collection, then
59  	 * calls the next processor in the chain.
60  	 * 
61  	 * @param collection
62  	 *            the collection to check
63  	 * @param next
64  	 *            the next processor in the chain
65  	 * @throws NullPointerException
66  	 *             if collection or next is null
67  	 */
68  	public IsElementOf(final Collection<Object> collection, final CellProcessor next) {
69  		super(next);
70  		checkPreconditions(collection);
71  		this.collection = collection;
72  	}
73  	
74  	/**
75  	 * Checks the preconditions for creating a new IsElementOf processor.
76  	 * 
77  	 * @param collection
78  	 *            the collection to check
79  	 * @throws NullPointerException
80  	 *             if collection is null
81  	 */
82  	private static void checkPreconditions(final Collection<Object> collection) {
83  		if( collection == null ) {
84  			throw new NullPointerException("collection should not be null");
85  		}
86  	}
87  	
88  	/**
89  	 * {@inheritDoc}
90  	 * 
91  	 * @throws SuperCsvConstraintViolationException
92  	 *             if value isn't in the Collection
93  	 */
94  	public Object execute(final Object value, final CsvContext context) {
95  		
96  		if( !collection.contains(value) ) {
97  			throw new SuperCsvConstraintViolationException(String.format(
98  				"'%s' is not an element of the supplied Collection", value), context, this);
99  		}
100 		
101 		return next.execute(value, context);
102 	}
103 	
104 }