-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBook.java
319 lines (290 loc) · 10.9 KB
/
Book.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright 2013 National Bank of Belgium
*
* Licensed under the EUPL, Version 1.1 or – as soon they will be approved
* by the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
package ec.util.spreadsheet;
import nbbrd.service.*;
import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.io.*;
import java.net.URL;
import java.nio.file.*;
import java.util.*;
import java.util.function.ObjIntConsumer;
/**
* Facade that represents <b>a book in a spreadsheet</b>. It is created by a
* factory.
* <br>Note that you should not store directly a sheet since some
* implementations may use the flyweight pattern.
* <br>This facade might also lock some resources. Therefore it is recommended
* to {@link #close() close} it after use.
*
* @author Philippe Charles
* @see Factory
* @see Sheet
* @see Cell
*/
//@FacadePattern
public abstract class Book implements Closeable {
/**
* Returns the number of sheets contained in this book.
*
* @return a sheet count
*/
@NonNegative
public int getSheetCount2() throws IOException {
try {
return getSheetCount();
} catch (UncheckedIOException ex) {
throw ex.getCause();
}
}
/**
* @deprecated use {@link #getSheetCount2()} instead
*/
@Deprecated
abstract public int getSheetCount();
/**
* Returns a sheet based on its index in this book.
*
* @param index a zero-based index
* @return a non-null sheet
* @throws IOException if something goes wrong during loading
* @throws IndexOutOfBoundsException if the index is out of bounds
* (0<=index<sheetCount)
*/
@NonNull
abstract public Sheet getSheet(@NonNegative int index) throws IOException, IndexOutOfBoundsException;
/**
* Returns the name of a sheet based on its index in this book.
*
* @param index a zero-based index
* @return a non-null name
* @throws IOException if something goes wrong during loading
* @throws IndexOutOfBoundsException if the index is out of bounds
*/
@NonNull
public String getSheetName(@NonNegative int index) throws IOException, IndexOutOfBoundsException {
return getSheet(index).getName();
}
/**
* Performs the given action for each sheet of the book until all sheets
* have been processed or an exception has been thrown.
*
* @param action The action to be performed for each sheet
* @throws NullPointerException if the specified action is null
* @throws IOException if something goes wrong during loading
* @implSpec <p>
* The default implementation behaves as if:<pre>{@code
* for (int index = 0; index < getSheetCount(); index++) {
* action.accept(getSheet(index), index);
* }
* }</pre>
* @since 2.2.0
*/
public void forEach(@NonNull ObjIntConsumer<? super Sheet> action) throws IOException {
Objects.requireNonNull(action);
for (int index = 0; index < getSheetCount2(); index++) {
action.accept(getSheet(index), index);
}
}
/**
* Performs in parallel the given action for each sheet of the book until
* all sheets have been processed or an exception has been thrown.
*
* @param action The action to be performed for each sheet
* @throws NullPointerException if the specified action is null
* @throws IOException if something goes wrong during loading
* @implSpec <p>
* The default implementation behaves as the regular foreach.
* @since 2.2.2
*/
public void parallelForEach(@NonNull ObjIntConsumer<? super Sheet> action) throws IOException {
forEach(action);
}
/**
* Closes this book and releases any resources associated with it.
*
* @throws IOException if an I/O error occurs.
*/
@Override
public void close() throws IOException {
}
/**
* Factory used to store/load books in/from spreadsheets.
*/
@ServiceDefinition(
singleton = true,
quantifier = Quantifier.MULTIPLE,
mutability = Mutability.CONCURRENT,
loaderName = "ec.util.spreadsheet.BookFactoryLoader"
)
public abstract static class Factory implements FileFilter, DirectoryStream.Filter<Path> {
/**
* Returns a unique identifier of this factory.
*
* @return a non-null identifier
*/
@ServiceId
@NonNull
abstract public String getName();
@ServiceSorter(reverse = true)
public int getRank() {
return UNKNOWN_RANK;
}
//<editor-fold defaultstate="collapsed" desc="Loading methods">
/**
* Checks if this factory can load a book from a spreadsheet.
*
* @return true if loading is supported; false otherwise
*/
public boolean canLoad() {
return true;
}
/**
* Loads a book from a Path.
*
* @param file a non-null spreadsheet file
* @return a non-null book
* @throws IOException if something goes wrong during the loading.
*/
@NonNull
public Book load(@NonNull Path file) throws IOException {
try {
return load(file.toFile());
} catch (UnsupportedOperationException ex) {
// if this Path is not associated with the default provider
try (InputStream stream = Files.newInputStream(file)) {
return load(stream);
}
}
}
/**
* Loads a book from a File.
*
* @param file a non-null spreadsheet file
* @return a non-null book
* @throws IOException if something goes wrong during the loading.
*/
@NonNull
public Book load(@NonNull File file) throws IOException {
try (InputStream stream = Files.newInputStream(file.toPath())) {
return load(stream);
}
}
/**
* Loads a book from an URL.
*
* @param url a non-null spreadsheet URL
* @return a non-null book
* @throws IOException if something goes wrong during the loading.
*/
@Deprecated
@NonNull
public Book load(@NonNull URL url) throws IOException {
try (InputStream stream = url.openStream()) {
return load(stream);
} catch (FileNotFoundException ex) {
throw translate(ex);
}
}
/**
* Loads a book from an InputStream.<br>This method <u>does not
* close</u> the stream after use.
*
* @param stream a non-null spreadsheet stream
* @return a non-null book
* @throws IOException if something goes wrong during the loading.
*/
@NonNull
abstract public Book load(@NonNull InputStream stream) throws IOException;
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Storing methods">
/**
* Checks if this factory can store a book in a spreadsheet.
*
* @return true if storing is supported; false otherwise
*/
public boolean canStore() {
return true;
}
/**
* Stores a book in a file.
*
* @param file a non-null spreadsheet file
* @param book the data to be stored
* @throws IOException if something goes wrong during the storing.
*/
public void store(@NonNull Path file, @NonNull Book book) throws IOException {
try {
store(file.toFile(), book);
} catch (UnsupportedOperationException ex) {
// if this Path is not associated with the default provider
try (OutputStream stream = Files.newOutputStream(file)) {
store(stream, book);
}
}
}
/**
* Stores a book in a file.
*
* @param file a non-null spreadsheet file
* @param book the data to be stored
* @throws IOException if something goes wrong during the storing.
*/
public void store(@NonNull File file, @NonNull Book book) throws IOException {
try (OutputStream stream = Files.newOutputStream(file.toPath(), StandardOpenOption.CREATE)) {
store(stream, book);
}
}
/**
* Stores a book in an OutputStream.<br>This method <u>does not
* close</u> the stream after use.
*
* @param stream a non-null spreadsheet stream
* @param book the data to be stored
* @throws IOException if something goes wrong during the storing.
*/
abstract public void store(@NonNull OutputStream stream, @NonNull Book book) throws IOException;
//</editor-fold>
public @NonNull Map<String, List<String>> getExtensionsByMediaType() {
return Collections.emptyMap();
}
@Override
public boolean accept(Path entry) throws IOException {
try {
return accept(entry.toFile());
} catch (UnsupportedOperationException ex) {
// if this Path is not associated with the default provider
return false;
}
}
public boolean isSupportedDataType(@NonNull Class<?> type) {
return Date.class.isAssignableFrom(type)
|| Number.class.isAssignableFrom(type)
|| String.class.isAssignableFrom(type);
}
public static final int NATIVE_RANK = Byte.MAX_VALUE;
public static final int WRAPPED_RANK = 0;
public static final int UNKNOWN_RANK = -1;
}
private static IOException translate(FileNotFoundException ex) {
String msg = ex.getMessage();
if (msg != null && !msg.isEmpty()) {
return new NoSuchFileException(msg);
}
return ex;
}
}