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.Collections;
19  import java.util.HashSet;
20  import java.util.Set;
21  
22  import org.supercsv.cellprocessor.CellProcessorAdaptor;
23  import org.supercsv.cellprocessor.ift.BoolCellProcessor;
24  import org.supercsv.cellprocessor.ift.CellProcessor;
25  import org.supercsv.cellprocessor.ift.DateCellProcessor;
26  import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
27  import org.supercsv.cellprocessor.ift.LongCellProcessor;
28  import org.supercsv.cellprocessor.ift.StringCellProcessor;
29  import org.supercsv.exception.SuperCsvCellProcessorException;
30  import org.supercsv.exception.SuperCsvConstraintViolationException;
31  import org.supercsv.util.CsvContext;
32  
33  /**
34   * This processor ensures that the input value belongs to a specific set of (unchangeable) values. If you want to check
35   * if the value is an element of a (possibly changing) Collection, then use {@link IsElementOf} instead.
36   * 
37   * @since 1.50
38   * @author Dominique De Vito
39   * @author James Bassett
40   */
41  public class IsIncludedIn extends CellProcessorAdaptor implements BoolCellProcessor, DateCellProcessor,
42  	DoubleCellProcessor, LongCellProcessor, StringCellProcessor {
43  	
44  	private final Set<Object> possibleValues = new HashSet<Object>();
45  	
46  	/**
47  	 * Constructs a new <tt>IsIncludedIn</tt> processor, which ensures that the input value belongs to a specific set of
48  	 * given values.
49  	 * 
50  	 * @param possibleValues
51  	 *            the Set of values
52  	 * @throws NullPointerException
53  	 *             if possibleValues is null
54  	 * @throws IllegalArgumentException
55  	 *             if possibleValues is empty
56  	 */
57  	public IsIncludedIn(final Set<Object> possibleValues) {
58  		super();
59  		checkPreconditions(possibleValues);
60  		this.possibleValues.addAll(possibleValues);
61  	}
62  	
63  	/**
64  	 * Constructs a new <tt>IsIncludedIn</tt> processor, which ensures that the input value belongs to a specific set of
65  	 * given values, then calls the next processor in the chain.
66  	 * 
67  	 * @param possibleValues
68  	 *            the Set of values
69  	 * @param next
70  	 *            the next processor in the chain
71  	 * @throws NullPointerException
72  	 *             if possibleValues or next is null
73  	 * @throws IllegalArgumentException
74  	 *             if possibleValues is empty
75  	 */
76  	public IsIncludedIn(final Set<Object> possibleValues, final CellProcessor next) {
77  		super(next);
78  		checkPreconditions(possibleValues);
79  		this.possibleValues.addAll(possibleValues);
80  	}
81  	
82  	/**
83  	 * Constructs a new <tt>IsIncludedIn</tt> processor, which ensures that the input value belongs to a specific set of
84  	 * given values.
85  	 * 
86  	 * @param possibleValues
87  	 *            the array of values
88  	 * @throws NullPointerException
89  	 *             if possibleValues is null
90  	 * @throws IllegalArgumentException
91  	 *             if possibleValues is empty
92  	 */
93  	public IsIncludedIn(final Object[] possibleValues) {
94  		super();
95  		checkPreconditions(possibleValues);
96  		Collections.addAll(this.possibleValues, possibleValues);
97  	}
98  	
99  	/**
100 	 * Constructs a new <tt>IsIncludedIn</tt> processor, which ensures that the input value belongs to a specific set of
101 	 * given values, then calls the next processor in the chain.
102 	 * 
103 	 * @param possibleValues
104 	 *            the array of values
105 	 * @param next
106 	 *            the next processor in the chain
107 	 * @throws NullPointerException
108 	 *             if possibleValues or next is null
109 	 * @throws IllegalArgumentException
110 	 *             if possibleValues is empty
111 	 */
112 	public IsIncludedIn(final Object[] possibleValues, final CellProcessor next) {
113 		super(next);
114 		checkPreconditions(possibleValues);
115 		Collections.addAll(this.possibleValues, possibleValues);
116 	}
117 	
118 	/**
119 	 * Checks the preconditions for creating a new IsIncludedIn processor with a Set of Objects.
120 	 * 
121 	 * @param possibleValues
122 	 *            the Set of possible values
123 	 * @throws NullPointerException
124 	 *             if possibleValues is null
125 	 * @throws IllegalArgumentException
126 	 *             if possibleValues is empty
127 	 */
128 	private static void checkPreconditions(final Set<Object> possibleValues) {
129 		if( possibleValues == null ) {
130 			throw new NullPointerException("possibleValues Set should not be null");
131 		} else if( possibleValues.isEmpty() ) {
132 			throw new IllegalArgumentException("possibleValues Set should not be empty");
133 		}
134 	}
135 	
136 	/**
137 	 * Checks the preconditions for creating a new IsIncludedIn processor with a array of Objects.
138 	 * 
139 	 * @param possibleValues
140 	 *            the array of possible values
141 	 * @throws NullPointerException
142 	 *             if possibleValues is null
143 	 * @throws IllegalArgumentException
144 	 *             if possibleValues is empty
145 	 */
146 	private static void checkPreconditions(final Object... possibleValues) {
147 		if( possibleValues == null ) {
148 			throw new NullPointerException("possibleValues array should not be null");
149 		} else if( possibleValues.length == 0 ) {
150 			throw new IllegalArgumentException("possibleValues array should not be empty");
151 		}
152 	}
153 	
154 	/**
155 	 * {@inheritDoc}
156 	 * 
157 	 * @throws SuperCsvCellProcessorException
158 	 *             if value is null
159 	 * @throws SuperCsvConstraintViolationException
160 	 *             if value isn't one of the possible values
161 	 */
162 	public Object execute(final Object value, final CsvContext context) {
163 		validateInputNotNull(value, context);
164 		
165 		if( !possibleValues.contains(value) ) {
166 			throw new SuperCsvConstraintViolationException(String.format(
167 				"'%s' is not included in the allowed set of values", value), context, this);
168 		}
169 		
170 		return next.execute(value, context);
171 	}
172 }