001: package test.it.unimi.dsi.mg4j.document;
002:
003: import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
004: import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps;
005: import it.unimi.dsi.mg4j.document.AbstractDocumentCollection;
006: import it.unimi.dsi.mg4j.document.AbstractDocumentFactory;
007: import it.unimi.dsi.mg4j.document.Document;
008: import it.unimi.dsi.mg4j.document.DocumentCollection;
009: import it.unimi.dsi.mg4j.document.DocumentFactory;
010: import it.unimi.dsi.fastutil.io.FastByteArrayInputStream;
011: import it.unimi.dsi.io.WordReader;
012:
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.util.Date;
016:
017: /** A document collection explicitly defined by a sequence of {@linkplain Date dates} (mainly useful for testing).
018: *
019: * <p>Every integer in the provided sequence is considered a document, and the only
020: * field is of type integer. The factory is built-in.
021: */
022:
023: public class DateArrayDocumentCollection extends
024: AbstractDocumentCollection {
025: public final static class DateArrayDocumentFactory extends
026: AbstractDocumentFactory {
027: private static final long serialVersionUID = 1L;
028:
029: public DocumentFactory copy() {
030: return this ;
031: }
032:
033: public int fieldIndex(String fieldName) {
034: if ("date".equals(fieldName))
035: return 0;
036: return -1;
037: }
038:
039: public String fieldName(int field) {
040: ensureFieldIndex(field);
041: return "date";
042: }
043:
044: public FieldType fieldType(int field) {
045: ensureFieldIndex(field);
046: return FieldType.DATE;
047: }
048:
049: public Document getDocument(InputStream rawContent,
050: Reference2ObjectMap<Enum<?>, Object> metadata)
051: throws IOException {
052: throw new UnsupportedOperationException();
053: }
054:
055: public int numberOfFields() {
056: return 1;
057: }
058: }
059:
060: final public Date[] date;
061: final DocumentFactory factory;
062:
063: public DateArrayDocumentCollection(final Date... date) {
064: factory = new DateArrayDocumentFactory();
065:
066: this .date = date;
067: }
068:
069: public int size() {
070: return date.length;
071: }
072:
073: public Document document(final int index) {
074: return new Document() {
075: public void close() {
076: }
077:
078: public Object content(int field) throws IOException {
079: ensureDocumentIndex(index);
080: return date[index];
081: }
082:
083: public CharSequence title() {
084: return null;
085: }
086:
087: public CharSequence uri() {
088: return null;
089: }
090:
091: public WordReader wordReader(int field) {
092: throw new UnsupportedOperationException();
093: }
094: };
095: }
096:
097: public InputStream stream(final int index) throws IOException {
098: return new FastByteArrayInputStream(date[index].toString()
099: .getBytes("ASCII"));
100: }
101:
102: @SuppressWarnings("unchecked")
103: public Reference2ObjectMap<Enum<?>, Object> metadata(int index)
104: throws IOException {
105: return Reference2ObjectMaps.EMPTY_MAP;
106: }
107:
108: public DocumentCollection copy() {
109: return this ;
110: }
111:
112: public DocumentFactory factory() {
113: return factory;
114: };
115: }
|