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.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import com.orangesignal.csv.CsvReader;
25  import com.orangesignal.csv.CsvWriter;
26  import com.orangesignal.csv.filters.CsvValueFilter;
27  import com.orangesignal.csv.io.CsvColumnPositionMapReader;
28  import com.orangesignal.csv.io.CsvColumnPositionMapWriter;
29  
30  /**
31   * 項目位置と項目値のマップのリストで区切り文字形式データアクセスを行うハンドラを提供します。
32   *
33   * @author Koji Sugisawa
34   * @since 1.1
35   */
36  public class ColumnPositionMapListHandler extends AbstractCsvListHandler<Map<Integer, String>, ColumnPositionMapListHandler> {
37  
38  	/**
39  	 * 区切り文字形式データフィルタを保持します。
40  	 */
41  	private CsvValueFilter valueFilter;
42  
43  	/**
44  	 * デフォルトコンストラクタです。
45  	 */
46  	public ColumnPositionMapListHandler() {}
47  
48  	/**
49  	 * 区切り文字形式データフィルタを設定します。
50  	 * 
51  	 * @param filter 区切り文字形式データフィルタ
52  	 * @return このオブジェクトへの参照
53  	 * @since 1.2.3
54  	 */
55  	public ColumnPositionMapListHandler filter(final CsvValueFilter filter) {
56  		this.valueFilter = filter;
57  		return this;
58  	}
59  
60  	@Override
61  	public List<Map<Integer, String>> load(final CsvReader reader, final boolean ignoreScalar) throws IOException {
62  		@SuppressWarnings("resource")
63  		final CsvColumnPositionMapReader r = new CsvColumnPositionMapReader(reader);
64  		r.setFilter(valueFilter);
65  
66  		// データ部を処理します。
67  		final List<Map<Integer, String>> results = new ArrayList<Map<Integer, String>>();
68  		int offset = 0;
69  
70  		List<String> values;
71  		while ((values = r.readValues()) != null && (ignoreScalar || limit <= 0 || results.size() < limit)) {
72  			if (!ignoreScalar && offset < this.offset) {
73  				offset++;
74  				continue;
75  			}
76  			results.add(r.toMap(values));
77  		}
78  		return results;
79  	}
80  
81  	@Override
82  	public void save(final List<Map<Integer, String>> list, final CsvWriter writer) throws IOException {
83  		@SuppressWarnings("resource")
84  		final CsvColumnPositionMapWriter w = new CsvColumnPositionMapWriter(writer);
85  		w.setFilter(valueFilter);
86  
87  		// データ部を処理します。
88  		for (final Map<Integer, String> map : list) {
89  			w.write(map);
90  		}
91  	}
92  
93  }