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;
17
18 import org.supercsv.cellprocessor.ift.BoolCellProcessor;
19 import org.supercsv.cellprocessor.ift.CellProcessor;
20 import org.supercsv.cellprocessor.ift.DateCellProcessor;
21 import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
22 import org.supercsv.cellprocessor.ift.LongCellProcessor;
23 import org.supercsv.cellprocessor.ift.StringCellProcessor;
24 import org.supercsv.exception.SuperCsvCellProcessorException;
25 import org.supercsv.util.CsvContext;
26
27 /**
28 * Abstract super class containing shared behaviour of all cell processors. Processors are linked together in a linked
29 * list. The end element of this list should always be an instance of <tt>NullObjectPattern</tt>.
30 *
31 * @author Kasper B. Graversen
32 * @author James Bassett
33 */
34 public abstract class CellProcessorAdaptor implements CellProcessor {
35
36 /** the next processor in the chain */
37 protected final CellProcessor next;
38
39 /**
40 * Constructor used by CellProcessors to indicate that they are the last processor in the chain.
41 */
42 protected CellProcessorAdaptor() {
43 super();
44 this.next = NullObjectPattern.INSTANCE;
45 }
46
47 /**
48 * Constructor used by CellProcessors that require <tt>CellProcessor</tt> chaining (further processing is required).
49 *
50 * @param next
51 * the next <tt>CellProcessor</tt> in the chain
52 * @throws NullPointerException
53 * if next is null
54 */
55 protected CellProcessorAdaptor(final CellProcessor next) {
56 super();
57 if( next == null ) {
58 throw new NullPointerException("next CellProcessor should not be null");
59 }
60 this.next = next;
61 }
62
63 /**
64 * Checks that the input value is not <tt>null</tt>, throwing a <tt>NullInputException</tt> if it is. This method
65 * should be called by all processors that need to ensure the input is not <tt>null</tt>.
66 *
67 * @param value
68 * the input value
69 * @param context
70 * the CSV context
71 * @throws SuperCsvCellProcessorException
72 * if value is null
73 * @since 2.0.0
74 */
75 protected void validateInputNotNull(final Object value, final CsvContext context) {
76 if( value == null ) {
77 throw new SuperCsvCellProcessorException(
78 "this processor does not accept null input - if the column is optional then chain an Optional() processor before this one",
79 context, this);
80 }
81 }
82
83 /**
84 * Returns the CellProccessor's fully qualified class name.
85 */
86 @Override
87 public String toString() {
88 return getClass().getName();
89 }
90
91 /**
92 * This is an implementation-specific processor and should only be used by the <tt>CellProcessorAdaptor</tt> class.
93 * It is the implementation of the null object pattern (it does nothing - just returns the value!) and should always
94 * be the last <tt>CellProcessor</tt> in the chain. It is implemented as a reusable singleton to avoid unnecessary
95 * object creation.
96 *
97 * @author Kasper B. Graversen
98 * @author James Bassett
99 */
100 private static final class NullObjectPattern implements BoolCellProcessor, DateCellProcessor, DoubleCellProcessor,
101 LongCellProcessor, StringCellProcessor {
102
103 private static final NullObjectPattern INSTANCE = new NullObjectPattern();
104
105 /*
106 * This processor must not be instantiated outside of CellProcessorAdaptor.
107 */
108 private NullObjectPattern() {
109 super();
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 public Object execute(final Object value, final CsvContext context) {
116 return value;
117 }
118 }
119
120 }