1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
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
45
46 public Equals() {
47 super();
48 this.constantValue = UNKNOWN;
49 this.constantSupplied = false;
50 }
51
52
53
54
55
56
57
58 public Equals(Object constantValue) {
59 super();
60 this.constantValue = constantValue;
61 this.constantSupplied = true;
62 }
63
64
65
66
67
68
69
70
71
72
73 public Equals(final CellProcessor next) {
74 super(next);
75 this.constantValue = UNKNOWN;
76 this.constantSupplied = false;
77 }
78
79
80
81
82
83
84
85
86
87
88
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
98
99
100
101
102
103 public Object execute(final Object value, final CsvContext context) {
104 if( UNKNOWN.equals(constantValue) ) {
105 constantValue = value;
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
122
123
124
125
126
127
128
129 private static boolean equals(Object o1, Object o2) {
130 return (o1 == null) ? (o2 == null) : o1.equals(o2);
131 }
132
133 }