001: package test.it.unimi.dsi.mg4j.document;
002:
003: import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
004: import it.unimi.dsi.fastutil.objects.ObjectArrayList;
005: import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
006: import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps;
007: import it.unimi.dsi.io.FastBufferedReader;
008: import it.unimi.dsi.io.WordReader;
009: import it.unimi.dsi.lang.MutableString;
010: import it.unimi.dsi.mg4j.document.AbstractDocumentCollection;
011: import it.unimi.dsi.mg4j.document.AbstractDocumentFactory;
012: import it.unimi.dsi.mg4j.document.Document;
013: import it.unimi.dsi.mg4j.document.DocumentCollection;
014: import it.unimi.dsi.mg4j.document.DocumentFactory;
015: import it.unimi.dsi.mg4j.tool.VirtualDocumentResolver;
016: import it.unimi.dsi.mg4j.util.parser.callback.AnchorExtractor.Anchor;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.util.Map;
021:
022: /** A virtual document collection explicitly defined by an array of maps and a trivial resolver (mainly useful for testing).
023: *
024: * <p>Every map in the provided sequence is considered a virtual document, and the only
025: * field virtual. The factory is built-in.
026: *
027: * <p>For simple testing, it is suggested that the provided document specifiers are just integers (as strings),
028: * and that an instance of {@link MapVirtualDocumentCollection.TrivialVirtualDocumentResolver} is used to resolve them.
029: */
030:
031: public class MapVirtualDocumentCollection extends
032: AbstractDocumentCollection {
033: /** A trivial resolver that just parses document specifier as integers. */
034: public final static class TrivialVirtualDocumentResolver implements
035: VirtualDocumentResolver {
036: private static final long serialVersionUID = 1L;
037: private int numberOfDocuments;
038:
039: public int numberOfDocuments() {
040: return numberOfDocuments;
041: }
042:
043: public void context(Document document) {
044: }
045:
046: public int resolve(CharSequence virtualDocumentSpec) {
047: final int d = Integer.parseInt(virtualDocumentSpec
048: .toString());
049: return d < 0 || d >= numberOfDocuments ? -1 : d;
050: }
051:
052: public TrivialVirtualDocumentResolver(
053: final int numberOfDocuments) {
054: this .numberOfDocuments = numberOfDocuments;
055: }
056: };
057:
058: public final static class MapVirtualDocumentFactory extends
059: AbstractDocumentFactory {
060: private static final long serialVersionUID = 1L;
061:
062: public DocumentFactory copy() {
063: return this ;
064: }
065:
066: public int fieldIndex(String fieldName) {
067: if ("virtual".equals(fieldName))
068: return 0;
069: return -1;
070: }
071:
072: public String fieldName(int field) {
073: ensureFieldIndex(field);
074: return "virtual";
075: }
076:
077: public FieldType fieldType(int field) {
078: ensureFieldIndex(field);
079: return FieldType.VIRTUAL;
080: }
081:
082: public Document getDocument(InputStream rawContent,
083: Reference2ObjectMap<Enum<?>, Object> metadata)
084: throws IOException {
085: throw new UnsupportedOperationException();
086: }
087:
088: public int numberOfFields() {
089: return 1;
090: }
091: }
092:
093: final public Int2ObjectMap<? extends CharSequence>[] virtual;
094: final DocumentFactory factory;
095:
096: public MapVirtualDocumentCollection(
097: final Int2ObjectMap<? extends CharSequence>... virtual) {
098: factory = new MapVirtualDocumentFactory();
099: this .virtual = virtual;
100: }
101:
102: public int size() {
103: return virtual.length;
104: }
105:
106: public Document document(final int index) {
107: return new Document() {
108: public void close() {
109: }
110:
111: public Object content(int field) throws IOException {
112: ensureDocumentIndex(index);
113: ObjectArrayList<Anchor> result = new ObjectArrayList<Anchor>();
114: for (Map.Entry<Integer, ? extends CharSequence> entry : virtual[index]
115: .entrySet())
116: result.add(new Anchor(new MutableString(entry
117: .getKey().toString()), new MutableString(
118: entry.getValue())));
119: return result;
120: }
121:
122: public CharSequence title() {
123: return null;
124: }
125:
126: public CharSequence uri() {
127: return null;
128: }
129:
130: public WordReader wordReader(int field) {
131: return new FastBufferedReader();
132: }
133: };
134: }
135:
136: public InputStream stream(final int index) throws IOException {
137: throw new UnsupportedOperationException();
138: }
139:
140: @SuppressWarnings("unchecked")
141: public Reference2ObjectMap<Enum<?>, Object> metadata(int index)
142: throws IOException {
143: return Reference2ObjectMaps.EMPTY_MAP;
144: }
145:
146: public DocumentCollection copy() {
147: return this ;
148: }
149:
150: public DocumentFactory factory() {
151: return factory;
152: };
153: }
|