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 java.util.Collection;
19
20 import org.supercsv.cellprocessor.ift.BoolCellProcessor;
21 import org.supercsv.cellprocessor.ift.CellProcessor;
22 import org.supercsv.cellprocessor.ift.DateCellProcessor;
23 import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
24 import org.supercsv.cellprocessor.ift.LongCellProcessor;
25 import org.supercsv.cellprocessor.ift.StringCellProcessor;
26 import org.supercsv.util.CsvContext;
27
28 /**
29 * This processor collects each value it encounters and adds it to the supplied Collection. You could supply a Set to
30 * collect all of the unique values for a column, or a List to collect every value in a column in order. Just remember
31 * that the larger your CSV file, the larger this Collection will be, so use with caution!
32 *
33 * @since 2.1.0
34 * @author James Bassett
35 */
36 public class Collector extends CellProcessorAdaptor implements BoolCellProcessor, DateCellProcessor,
37 DoubleCellProcessor, LongCellProcessor, StringCellProcessor {
38
39 private final Collection<Object> collection;
40
41 /**
42 * Constructs a new <tt>Collector</tt>, which collects each value it encounters and adds it to the supplied
43 * Collection.
44 *
45 * @param collection
46 * the collection to add to
47 * @throws NullPointerException
48 * if collection is null
49 */
50 public Collector(final Collection<Object> collection) {
51 super();
52 checkPreconditions(collection);
53 this.collection = collection;
54 }
55
56 /**
57 * Constructs a new <tt>Collector</tt>, which collects each value it encounters, adds it to the supplied Collection,
58 * then calls the next processor in the chain.
59 *
60 * @param collection
61 * the collection to add to
62 * @param next
63 * the next processor in the chain
64 * @throws NullPointerException
65 * if collection or next is null
66 */
67 public Collector(final Collection<Object> collection, final CellProcessor next) {
68 super(next);
69 checkPreconditions(collection);
70 this.collection = collection;
71 }
72
73 /**
74 * Checks the preconditions for creating a new Collector processor.
75 *
76 * @param collection
77 * the collection to add to
78 * @throws NullPointerException
79 * if collection is null
80 */
81 private static void checkPreconditions(final Collection<Object> collection) {
82 if( collection == null ) {
83 throw new NullPointerException("collection should not be null");
84 }
85 }
86
87 /**
88 * {@inheritDoc}
89 */
90 public Object execute(final Object value, final CsvContext context) {
91 collection.add(value);
92 return next.execute(value, context);
93 }
94
95 /**
96 * Gets the collection of collected values.
97 *
98 * @return the collection of collected values
99 */
100 public Collection<Object> getCollection() {
101 return collection;
102 }
103
104 }