View Javadoc
1   /*
2    * Copyright 2013 the original author or authors.
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  
17  package com.orangesignal.csv.handlers;
18  
19  import java.io.IOException;
20  import java.text.Format;
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  
25  import com.orangesignal.csv.CsvReader;
26  import com.orangesignal.csv.CsvWriter;
27  import com.orangesignal.csv.bean.CsvColumnNameMappingBeanOperation;
28  import com.orangesignal.csv.bean.CsvColumnNameMappingBeanTemplate;
29  import com.orangesignal.csv.filters.CsvNamedValueFilter;
30  import com.orangesignal.csv.io.CsvColumnNameMappingBeanReader;
31  import com.orangesignal.csv.io.CsvColumnNameMappingBeanWriter;
32  
33  /**
34   * 区切り文字形式データの項目名を基準として Java プログラム要素のリストと区切り文字形式データアクセスを行うハンドラを提供します。
35   * 
36   * @author Koji Sugisawa
37   * @see com.orangesignal.csv.manager.CsvColumnNameMappingBeanManager
38   */
39  public class ColumnNameMappingBeanListHandler<T> extends AbstractBeanListHandler<T, CsvColumnNameMappingBeanTemplate<T>, ColumnNameMappingBeanListHandler<T>> implements CsvColumnNameMappingBeanOperation<ColumnNameMappingBeanListHandler<T>> {
40  
41  	/**
42  	 * 区切り文字形式データの列見出し (ヘッダ) 行を出力するかどうかを保持します。
43  	 * 
44  	 * @since 2.1
45  	 */
46  	private boolean header = true;
47  
48  	// ------------------------------------------------------------------------
49  
50  	/**
51  	 * コンストラクタです。
52  	 * 
53  	 * @param type Java プログラム要素の型
54  	 * @throws IllegalArgumentException {@code type} が {@code null} の場合
55  	 */
56  	public ColumnNameMappingBeanListHandler(final Class<T> type) {
57  		super(CsvColumnNameMappingBeanTemplate.newInstance(type));
58  	}
59  
60  	// ------------------------------------------------------------------------
61  
62  	@Override
63  	public ColumnNameMappingBeanListHandler<T> column(final String column, final String field) {
64  		return column(column, field, null);
65  	}
66  
67  	@Override
68  	public ColumnNameMappingBeanListHandler<T> column(final String column, final String field, final Format format) {
69  		template.column(column, field, format);
70  		return this;
71  	}
72  
73  	@Override
74  	public void setColumnMapping(final Map<String, String> columnMapping) {
75  		template.setColumnMapping(columnMapping);
76  	}
77  
78  	@Override
79  	public ColumnNameMappingBeanListHandler<T> columnMapping(final Map<String, String> columnMapping) {
80  		setColumnMapping(columnMapping);
81  		return this;
82  	}
83  
84  	@Override
85  	public ColumnNameMappingBeanListHandler<T> filter(final CsvNamedValueFilter filter) {
86  		template.filter(filter);
87  		return this;
88  	}
89  
90  	/**
91  	 * 区切り文字形式データの列見出し (ヘッダ) 行を出力するかどうかを設定します。
92  	 * 
93  	 * @param header 区切り文字形式データの列見出し (ヘッダ) 行を出力するかどうか
94  	 * @return このオブジェクトへの参照
95  	 * @since 2.1
96  	 */
97  	public ColumnNameMappingBeanListHandler<T> header(final boolean header) {
98  		this.header = header;
99  		return this;
100 	}
101 
102 	// ------------------------------------------------------------------------
103 
104 	@Override
105 	public List<T> load(final CsvReader reader, final boolean ignoreScalar) throws IOException {
106 		@SuppressWarnings("resource")
107 		final CsvColumnNameMappingBeanReader<T> r = new CsvColumnNameMappingBeanReader<T>(reader, template);
108 
109 		// データ部を処理します。
110 		final List<T> results = new ArrayList<T>();
111 		final boolean order = ignoreScalar || orders != null && !orders.isEmpty();
112 		int offset = 0;
113 
114 		List<String> values;
115 		while ((values = r.readValues()) != null && (order || limit <= 0 || results.size() < limit)) {
116 			if (beanFilter == null && !order && offset < this.offset) {
117 				offset++;
118 				continue;
119 			}
120 			final T bean = r.toBean(values);
121 			if (beanFilter != null) {
122 				if (!beanFilter.accept(bean)) {
123 					continue;
124 				}
125 				if (!order && offset < this.offset) {
126 					offset++;
127 					continue;
128 				}
129 			}
130 			results.add(bean);
131 		}
132 
133 		if (ignoreScalar || !order) {
134 			return results;
135 		}
136 		return processScalar(results);
137 	}
138 
139 	@Override
140 	public void save(final List<T> list, final CsvWriter writer) throws IOException {
141 		@SuppressWarnings("resource")
142 		final CsvColumnNameMappingBeanWriter<T> w = new CsvColumnNameMappingBeanWriter<T>(writer, template, header);
143 
144 		// データ部を処理します。
145 		for (final T bean : list) {
146 			// 要素が null の場合は null 出力します。
147 			if (bean == null) {
148 				w.write(null);
149 				continue;
150 			} else if (beanFilter != null && !beanFilter.accept(bean)) {
151 				continue;
152 			}
153 
154 			w.write(bean);
155 		}
156 	}
157 
158 }