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