blob: b8d8cd692d847970187947bbf31c41e349afa335 (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.annotation;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
/**
* @author Einar M R Rosenvinge
*/
public class ListAnnotationContainer extends IteratingAnnotationContainer {
private final List<Annotation> annotations = new LinkedList<>();
@Override
void annotateAll(Collection<Annotation> annotations) {
this.annotations.addAll(annotations);
}
@Override
void annotate(Annotation a) {
annotations.add(a);
}
@Override
Collection<Annotation> annotations() {
return annotations;
}
@Override
Iterator<Annotation> iterator(IdentityHashMap<SpanNode, SpanNode> nodes) {
return new AnnotationIterator(annotations.listIterator(), nodes);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ListAnnotationContainer other)) return false;
if (!annotations.equals(other.annotations)) return false;
return true;
}
@Override
public int hashCode() {
return annotations.hashCode();
}
private class AnnotationIterator implements Iterator<Annotation> {
private final IdentityHashMap<SpanNode, SpanNode> nodes;
private final PeekableListIterator<Annotation> base;
private boolean nextCalled = false;
AnnotationIterator(ListIterator<Annotation> baseIt, IdentityHashMap<SpanNode, SpanNode> nodes) {
this.base = new PeekableListIterator(baseIt);
this.nodes = nodes;
}
@Override
public boolean hasNext() {
nextCalled = false;
while (base.hasNext() && !nodes.containsKey(base.peek().getSpanNode())) {
base.next();
}
//now either, base has no next, or next is the correct node
if (base.hasNext()) {
return true;
}
return false;
}
@Override
public Annotation next() {
if (hasNext()) {
nextCalled = true;
return base.next();
} else {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
if (!nextCalled) {
throw new IllegalStateException();
}
base.remove();
nextCalled = false;
}
}
}
|