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