aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/annotation/SpanNode2AnnotationContainer.java
blob: d3f29492a74a7e3c4f050d099e0c5fe000990d2a (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.annotation;


import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;

/**
 * TODO: Should this be removed?
 *
 * @author Einar M R Rosenvinge
 */
class SpanNode2AnnotationContainer extends AnnotationContainer {
    private final Multimap<SpanNode, Annotation> spanNode2Annotation = Multimaps.newMultimap(new IdentityHashMap<>(), ArrayList::new);

    @Override
    void annotateAll(Collection<Annotation> annotations) {
        for (Annotation a : annotations) {
            annotate(a);
        }
    }

    @Override
    void annotate(Annotation a) {
        if (a.getSpanNode() == null) {
            spanNode2Annotation.put(DummySpanNode.INSTANCE, a);
        } else {
            spanNode2Annotation.put(a.getSpanNode(), a);
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    Collection<Annotation> annotations() {
        return spanNode2Annotation.values();
    }

    @Override
    @SuppressWarnings("unchecked")
    Iterator<Annotation> iterator(SpanNode node) {
        Collection<Annotation> annotationsForNode = spanNode2Annotation.get(node);
        if (annotationsForNode == null) {
            return Collections.<Annotation>emptyList().iterator();
        }
        return annotationsForNode.iterator();    }

    @Override
    @SuppressWarnings("unchecked")
    Iterator<Annotation> iteratorRecursive(SpanNode node) {
        IdentityHashMap<SpanNode, SpanNode> nodes = new IdentityHashMap<SpanNode, SpanNode>();
        nodes.put(node, node);
        {
            Iterator<SpanNode> childrenIt = node.childIteratorRecursive();
            while (childrenIt.hasNext()) {
                SpanNode child = childrenIt.next();
                nodes.put(child, child);
            }
        }
        List<Collection<Annotation>> annotationLists = new ArrayList<Collection<Annotation>>(nodes.size());
        for (SpanNode includedNode : nodes.keySet()) {
            Collection<Annotation> includedAnnotations = spanNode2Annotation.get(includedNode);
            if (includedAnnotations != null) {
                annotationLists.add(includedAnnotations);
            }
        }
        return new AnnotationCollectionIterator(annotationLists);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SpanNode2AnnotationContainer)) return false;
        SpanNode2AnnotationContainer that = (SpanNode2AnnotationContainer) o;
        if (!spanNode2Annotation.equals(that.spanNode2Annotation)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return spanNode2Annotation.hashCode();
    }

    private class AnnotationCollectionIterator implements Iterator<Annotation> {
        private final List<Collection<Annotation>> annotationLists;
        private Iterator<Annotation> currentIterator;
        private boolean nextCalled = false;

        AnnotationCollectionIterator(List<Collection<Annotation>> annotationLists) {
            this.annotationLists = annotationLists;
            if (annotationLists.isEmpty()) {
                currentIterator = Collections.<Annotation>emptyList().iterator();
            } else {
                currentIterator = annotationLists.remove(0).iterator();
            }
        }

        @Override
        public boolean hasNext() {
            nextCalled = false;
            if (currentIterator.hasNext()) {
                return true;
            }
            if (annotationLists.isEmpty()) {
                return false;
            }
            currentIterator = annotationLists.remove(0).iterator();
            return hasNext();
        }

        @Override
        public Annotation next() {
            if (hasNext()) {
                nextCalled = true;
                return currentIterator.next();
            }
            return null;
        }

        @Override
        public void remove() {
            if (nextCalled) {
                currentIterator.remove();
            } else {
                throw new IllegalStateException();
            }
        }
    }

}