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 org.supercsv.cellprocessor.CellProcessorAdaptor;
19  import org.supercsv.cellprocessor.ift.BoolCellProcessor;
20  import org.supercsv.cellprocessor.ift.CellProcessor;
21  import org.supercsv.cellprocessor.ift.DateCellProcessor;
22  import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
23  import org.supercsv.cellprocessor.ift.LongCellProcessor;
24  import org.supercsv.cellprocessor.ift.StringCellProcessor;
25  import org.supercsv.exception.SuperCsvConstraintViolationException;
26  import org.supercsv.util.CsvContext;
27  
28  /**
29   * This constraint ensures that all input data is equal (to each other, or to a supplied constant value).
30   * 
31   * @author Dominique De Vito
32   * @author James Bassett
33   * @since 1.50
34   */
35  public class Equals extends CellProcessorAdaptor implements BoolCellProcessor, DateCellProcessor, DoubleCellProcessor,
36  	LongCellProcessor, StringCellProcessor {
37  	
38  	private static final Object UNKNOWN = new Object();
39  	
40  	private Object constantValue;
41  	private boolean constantSupplied;
42  	
43  	/**
44  	 * Constructs a new <tt>Equals</tt> processor, which ensures all input data is equal.
45  	 */
46  	public Equals() {
47  		super();
48  		this.constantValue = UNKNOWN;
49  		this.constantSupplied = false;
50  	}
51  	
52  	/**
53  	 * Constructs a new <tt>Equals</tt> processor, which ensures all input data is equal to the supplied constant value.
54  	 * 
55  	 * @param constantValue
56  	 *            the constant value that all input must equal
57  	 */
58  	public Equals(Object constantValue) {
59  		super();
60  		this.constantValue = constantValue;
61  		this.constantSupplied = true;
62  	}
63  	
64  	/**
65  	 * Constructs a new <tt>Equals</tt> processor, which ensures all input data is equal, then calls the the next
66  	 * processor in the chain.
67  	 * 
68  	 * @param next
69  	 *            the next processor in the chain
70  	 * @throws NullPointerException
71  	 *             if next is null
72  	 */
73  	public Equals(final CellProcessor next) {
74  		super(next);
75  		this.constantValue = UNKNOWN;
76  		this.constantSupplied = false;
77  	}
78  	
79  	/**
80  	 * Constructs a new <tt>Equals</tt> processor, which ensures all input data is equal to the supplied constant value,
81  	 * then calls the the next processor in the chain.
82  	 * 
83  	 * @param constantValue
84  	 *            the constant value that all input must equal
85  	 * @param next
86  	 *            the next processor in the chain
87  	 * @throws NullPointerException
88  	 *             if next is null
89  	 */
90  	public Equals(final Object constantValue, final CellProcessor next) {
91  		super(next);
92  		this.constantValue = constantValue;
93  		this.constantSupplied = true;
94  	}
95  	
96  	/**
97  	 * {@inheritDoc}
98  	 * 
99  	 * @throws SuperCsvConstraintViolationException
100 	 *             if value isn't equal to the constant value (or previously encountered value if a constant wasn't
101 	 *             supplied)
102 	 */
103 	public Object execute(final Object value, final CsvContext context) {
104 		if( UNKNOWN.equals(constantValue) ) {
105 			constantValue = value; // no constant supplied, so remember the first value encountered
106 		} else {
107 			if( !equals(constantValue, value) ) {
108 				if( constantSupplied ) {
109 					throw new SuperCsvConstraintViolationException(String.format("'%s' is not equal to the supplied constant '%s'", value,
110 						constantValue), context, this);
111 				} else {
112 					throw new SuperCsvConstraintViolationException(String.format("'%s' is not equal to the previous value(s) of '%s'",
113 						value, constantValue), context, this);
114 				}
115 			}
116 		}
117 		return next.execute(value, context);
118 	}
119 	
120 	/**
121 	 * Returns true if both objects are null or equal, otherwise false.
122 	 * 
123 	 * @param o1
124 	 *            the first object
125 	 * @param o2
126 	 *            the second object
127 	 * @return true if both objects are null or equal, otherwise false
128 	 */
129 	private static boolean equals(Object o1, Object o2) {
130 		return (o1 == null) ? (o2 == null) : o1.equals(o2);
131 	}
132 	
133 }