01: package test.it.unimi.dsi.mg4j.document;
02:
03: import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
04: import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps;
05: import it.unimi.dsi.mg4j.document.AbstractDocumentCollection;
06: import it.unimi.dsi.mg4j.document.Document;
07: import it.unimi.dsi.mg4j.document.DocumentCollection;
08: import it.unimi.dsi.mg4j.document.DocumentFactory;
09: import it.unimi.dsi.mg4j.document.IdentityDocumentFactory;
10: import it.unimi.dsi.fastutil.io.FastByteArrayInputStream;
11:
12: import java.io.IOException;
13: import java.io.InputStream;
14:
15: import org.apache.commons.configuration.ConfigurationException;
16:
17: /** A document collection explicitly defined by a sequence of strings (mainly useful for testing).
18: *
19: * <p>Every string in the provided sequence is considered a document,
20: * and the stream returned by the collection is the UTF-8 encoding of the string. By default,
21: * the collection uses an {@link IdentityDocumentFactory}, but you can also
22: * {@linkplain #StringArrayDocumentCollection(DocumentFactory, String[]) specify your own factory}.
23: */
24:
25: public class StringArrayDocumentCollection extends
26: AbstractDocumentCollection {
27: final public String[] document;
28: final DocumentFactory factory;
29:
30: public StringArrayDocumentCollection(final String... document)
31: throws ConfigurationException {
32: factory = new IdentityDocumentFactory(
33: new String[] { "encoding=UTF-8" });
34: this .document = document;
35: }
36:
37: public StringArrayDocumentCollection(final DocumentFactory factory,
38: final String... document) {
39: this .factory = factory;
40: this .document = document;
41: }
42:
43: public int size() {
44: return document.length;
45: }
46:
47: public Document document(final int index) throws IOException {
48: return factory.getDocument(stream(index), metadata(index));
49: }
50:
51: public InputStream stream(final int index) throws IOException {
52: return new FastByteArrayInputStream(document[index]
53: .getBytes("UTF-8"));
54: }
55:
56: @SuppressWarnings("unchecked")
57: public Reference2ObjectMap<Enum<?>, Object> metadata(int index)
58: throws IOException {
59: return Reference2ObjectMaps.EMPTY_MAP;
60: }
61:
62: public DocumentCollection copy() {
63: return this ;
64: }
65:
66: public DocumentFactory factory() {
67: return factory;
68: };
69: }
|