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.manager;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.Reader;
23  import java.util.List;
24  import java.util.zip.ZipFile;
25  import java.util.zip.ZipInputStream;
26  
27  import com.orangesignal.csv.Csv;
28  import com.orangesignal.csv.CsvConfig;
29  import com.orangesignal.csv.LhaEntryFilter;
30  import com.orangesignal.csv.ZipEntryFilter;
31  import com.orangesignal.csv.bean.AbstractCsvBeanTemplate;
32  import com.orangesignal.csv.filters.BeanFilter;
33  import com.orangesignal.csv.handlers.AbstractBeanListHandler;
34  import com.orangesignal.csv.handlers.BeanOrder;
35  import com.orangesignal.jlha.LhaFile;
36  import com.orangesignal.jlha.LhaInputStream;
37  
38  /**
39   * 区切り文字形式データの統合入力インタフェースの実装を容易にするための抽象クラスを提供します。
40   *
41   * @author Koji Sugisawa
42   * @since 1.2.1
43   */
44  public abstract class AbstractCsvLoader<T, O extends AbstractCsvBeanTemplate<T, O>, H extends AbstractBeanListHandler<T, O, H>, L extends AbstractCsvLoader<T, O, H, L>> implements CsvLoader<T> {
45  
46  	/**
47  	 * 区切り文字形式情報を保持します。
48  	 */
49  	private final CsvConfig cfg;
50  
51  	/**
52  	 * データアクセスハンドラを保持します。
53  	 */
54  	private final H handler;
55  
56  	/**
57  	 * コンストラクタです。
58  	 *
59  	 * @param cfg 区切り文字形式情報
60  	 * @param handler 区切り文字形式データリストのデータアクセスハンドラ
61  	 * @throws IllegalArgumentException {@code cfg} または {@code handler} が {@code null} の場合
62  	 * @since 1.3.0
63  	 */
64  	protected AbstractCsvLoader(final CsvConfig cfg, final H handler) {
65  		if (cfg == null) {
66  			throw new IllegalArgumentException("CsvConfig must not be null");
67  		}
68  		if (handler == null) {
69  			throw new IllegalArgumentException("CsvListHandler must not be null");
70  		}
71  		this.cfg = cfg;
72  		this.handler = handler;
73  	}
74  
75  	/**
76  	 * 区切り文字形式データリストのデータアクセスハンドラを返します。
77  	 * 
78  	 * @return 区切り文字形式データリストのデータアクセスハンドラ
79  	 */
80  	protected H getCsvListHandler() { return handler; }
81  
82  	@SuppressWarnings("unchecked")
83  	@Override
84  	public L filter(final BeanFilter filter) {
85  		getCsvListHandler().filter(filter);
86  		return (L) this;
87  	}
88  
89  	@SuppressWarnings("unchecked")
90  	@Override
91  	public L order(final BeanOrder... orders) {
92  		getCsvListHandler().order(orders);
93  		return (L) this;
94  	}
95  
96  	@SuppressWarnings("unchecked")
97  	@Override
98  	public L offset(final int offset) {
99  		getCsvListHandler().setOffset(offset);
100 		return (L) this;
101 	}
102 
103 	@SuppressWarnings("unchecked")
104 	@Override
105 	public L limit(final int limit) {
106 		getCsvListHandler().setLimit(limit);
107 		return (L) this;
108 	}
109 
110 	@Override
111 	public List<T> from(final Reader reader) throws IOException {
112 		return Csv.load(reader, cfg, getCsvListHandler());
113 	}
114 
115 	@Override
116 	public List<T> from(final InputStream in, final String encoding) throws IOException {
117 		return Csv.load(in, encoding, cfg, getCsvListHandler());
118 	}
119 
120 	@Override
121 	public List<T> from(final InputStream in) throws IOException {
122 		return Csv.load(in, cfg, getCsvListHandler());
123 	}
124 
125 	@Override
126 	public List<T> from(final File file, final String encoding) throws IOException {
127 		return Csv.load(file, encoding, cfg, getCsvListHandler());
128 	}
129 
130 	@Override
131 	public List<T> from(final File file) throws IOException {
132 		return Csv.load(file, cfg, getCsvListHandler());
133 	}
134 
135 	@Override
136 	public List<T> from(final LhaInputStream in, final String encoding, final LhaEntryFilter filter) throws IOException {
137 		return Csv.load(in, encoding, cfg, getCsvListHandler(), filter);
138 	}
139 
140 	@Override
141 	public List<T> from(final LhaInputStream in, final String encoding) throws IOException {
142 		return Csv.load(in, encoding, cfg, getCsvListHandler());
143 	}
144 
145 	@Override
146 	public List<T> from(final LhaInputStream in, final LhaEntryFilter filter) throws IOException {
147 		return Csv.load(in, cfg, getCsvListHandler(), filter);
148 	}
149 
150 	@Override
151 	public List<T> from(final LhaInputStream in) throws IOException {
152 		return Csv.load(in, cfg, getCsvListHandler());
153 	}
154 
155 	@Override
156 	public List<T> from(final LhaFile lhaFile, final String encoding, final LhaEntryFilter filter) throws IOException {
157 		return Csv.load(lhaFile, encoding, cfg, getCsvListHandler(), filter);
158 	}
159 
160 	@Override
161 	public List<T> from(final LhaFile lhaFile, final String encoding) throws IOException {
162 		return Csv.load(lhaFile, encoding, cfg, getCsvListHandler());
163 	}
164 
165 	@Override
166 	public List<T> from(final LhaFile lhaFile, final LhaEntryFilter filter) throws IOException {
167 		return Csv.load(lhaFile, cfg, getCsvListHandler(), filter);
168 	}
169 
170 	@Override
171 	public List<T> from(final LhaFile lhaFile) throws IOException {
172 		return Csv.load(lhaFile, cfg, getCsvListHandler());
173 	}
174 
175 	@Override
176 	public List<T> from(final ZipInputStream in, final String encoding, final ZipEntryFilter filter) throws IOException {
177 		return Csv.load(in, encoding, cfg, getCsvListHandler(), filter);
178 	}
179 
180 	@Override
181 	public List<T> from(final ZipInputStream in, final String encoding) throws IOException {
182 		return Csv.load(in, encoding, cfg, getCsvListHandler());
183 	}
184 
185 	@Override
186 	public List<T> from(final ZipInputStream in, final ZipEntryFilter filter) throws IOException {
187 		return Csv.load(in, cfg, getCsvListHandler(), filter);
188 	}
189 
190 	@Override
191 	public List<T> from(final ZipInputStream in) throws IOException {
192 		return Csv.load(in, cfg, getCsvListHandler());
193 	}
194 
195 	@Override
196 	public List<T> from(final ZipFile zipFile, final String encoding, final ZipEntryFilter filter) throws IOException {
197 		return Csv.load(zipFile, encoding, cfg, getCsvListHandler(), filter);
198 	}
199 
200 	@Override
201 	public List<T> from(final ZipFile zipFile, final String encoding) throws IOException {
202 		return Csv.load(zipFile, encoding, cfg, getCsvListHandler());
203 	}
204 
205 	@Override
206 	public List<T> from(final ZipFile zipFile, final ZipEntryFilter filter) throws IOException {
207 		return Csv.load(zipFile, cfg, getCsvListHandler(), filter);
208 	}
209 
210 	@Override
211 	public List<T> from(final ZipFile zipFile) throws IOException {
212 		return Csv.load(zipFile, cfg, getCsvListHandler());
213 	}
214 
215 }