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.io;
18  
19  import java.io.Closeable;
20  import java.io.Flushable;
21  import java.io.IOException;
22  import java.util.Arrays;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.SortedMap;
26  import java.util.TreeMap;
27  
28  import com.orangesignal.csv.CsvWriter;
29  import com.orangesignal.csv.filters.CsvValueFilter;
30  
31  /**
32   * 項目位置と項目値のマップで区切り文字形式データアクセスを行う区切り文字形式出力ストリームを提供します。
33   * 
34   * @author Koji Sugisawa
35   * @since 1.4.0
36   */
37  public class CsvColumnPositionMapWriter implements Closeable, Flushable {
38  
39  	/**
40  	 * 区切り文字形式出力ストリームを保持します。
41  	 */
42  	private CsvWriter writer;
43  
44  	/**
45  	 * 区切り文字形式データフィルタを保持します。
46  	 */
47  	private CsvValueFilter filter;
48  
49  	// ------------------------------------------------------------------------
50  	// コンストラクタ
51  
52  	/**
53  	 * 指定された区切り文字形式出力ストリームを使用して、このクラスを構築するコンストラクタです。
54  	 * 
55  	 * @param writer 区切り文字形式出力ストリーム
56  	 * @throws IllegalArgumentException {@code writer} が {@code null} の場合。
57  	 */
58  	public CsvColumnPositionMapWriter(final CsvWriter writer) {
59  		if (writer == null) {
60  			throw new IllegalArgumentException("CsvWriter must not be null");
61  		}
62  		this.writer = writer;
63  	}
64  
65  	// ------------------------------------------------------------------------
66  	// プライベート メソッド
67  
68  	/**
69  	 * Checks to make sure that the stream has not been closed
70  	 */
71  	private void ensureOpen() throws IOException {
72  		if (writer == null) {
73  			throw new IOException("CsvWriter closed");
74  		}
75  	}
76  
77  	// ------------------------------------------------------------------------
78  	// オーバーライド メソッド
79  
80  	@Override
81  	public void flush() throws IOException {
82  		synchronized (this) {
83  			ensureOpen();
84  			writer.flush();
85  		}
86  	}
87  
88  	@Override
89  	public void close() throws IOException {
90  		synchronized (this) {
91  			ensureOpen();
92  			writer.close();
93  			writer = null;
94  		}
95  	}
96  
97  	// ------------------------------------------------------------------------
98  	// パブリック メソッド
99  
100 	/**
101 	 * 指定された項目名と項目値のマップを書き込みます。項目名の書き込みが必要な場合は自動的に書き込みが行われます。
102 	 * 
103 	 * @param map 項目名と項目値のマップ
104 	 * @return 区切り文字形式データフィルタによって書き込みが行われなかった場合は {@code false} それ以外の場合は {@code true}
105 	 * @throws IOException 入出力エラーが発生した場合
106 	 */
107 	public boolean write(final Map<Integer, String> map) throws IOException {
108 		synchronized (this) {
109 			ensureOpen();
110 
111 			// 要素が null の場合は null 出力します。
112 			if (map == null) {
113 				writer.writeValues(null);
114 				return true;
115 			}
116 
117 			final List<String> values = convert(map);
118 			if (filter != null && !filter.accept(values)) {
119 				return false;
120 			}
121 			writer.writeValues(values);
122 			return true;
123 		}
124 	}
125 
126 	private List<String> convert(final Map<Integer, String> map) {
127 		final String[] values = new String[getMaxColumnPosition(map) + 1];
128 		for (final Map.Entry<Integer, String> e : map.entrySet()) {
129 			final int pos = e.getKey();
130 			if (pos >= 0) {
131 				values[pos] = e.getValue();
132 			}
133 		}
134 		return Arrays.asList(values);
135 	}
136 
137 	private static int getMaxColumnPosition(final Map<Integer, String> map) {
138 		final SortedMap<Integer, String> sortedMap;
139 		if (map instanceof SortedMap) {
140 			sortedMap = (SortedMap<Integer, String>) map;
141 		} else {
142 			sortedMap = new TreeMap<Integer, String>(map);
143 		}
144 		return Math.max(sortedMap.lastKey().intValue(), 0);
145 	}
146 
147 	// ------------------------------------------------------------------------
148 	// セッター / ゲッター
149 
150 	/**
151 	 * 区切り文字形式データフィルタを返します。
152 	 * 
153 	 * @return 区切り文字形式データフィルタ。または {@code null}
154 	 */
155 	public CsvValueFilter getFilter() {
156 		return filter;
157 	}
158 
159 	/**
160 	 * 区切り文字形式データフィルタを設定します。
161 	 * 
162 	 * @param filter 区切り文字形式データフィルタ
163 	 */
164 	public void setFilter(final CsvValueFilter filter) {
165 		synchronized (this) {
166 			this.filter = filter;
167 		}
168 	}
169 
170 }