aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/annotation/SpanNode.java
blob: 3cf37083c5783c5471b9e3682c924e598b2aed96 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// 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.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;

import java.util.ListIterator;

/**
 * Base class of nodes in a Span tree.
 *
 * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 */
public abstract class SpanNode implements Comparable<SpanNode>, SpanNodeParent {

    private boolean valid = true;
    /**
     * This scratch id is used to avoid using IdentityHashMaps as they are very costly.
     */
    private int scratchId = -1;
    private SpanNodeParent parent;

    protected SpanNode() {
    }

    /**
     * Returns whether this node is valid or not.&nbsp;When a child node from a SpanList, the child
     * is marked as invalid, and the reference to it is removed from the parent SpanList. However,
     * Annotations in the global list kept in SpanTree may still have references to the removed SpanNode.
     * Removing these references is costly, and is only done when calling {@link com.yahoo.document.annotation.SpanTree#cleanup()}.
     *
     * @return true if this node is valid, false otherwise.
     */
    public boolean isValid() {
        return valid;
    }

    void setInvalid() {
        valid = false;
    }

    public void setScratchId(int id) {
        scratchId = id;
    }

    public int getScratchId() {
        return scratchId;
    }
    /**
     * Returns the parent node of this SpanNode, if any.
     *
     * @return the parent node, or null if this is not yet added to a parent SpanList
     */
    public SpanNodeParent getParent() {
        return parent;
    }

    void setParent(SpanNodeParent parent) {
        this.parent = parent;
    }

    /**
     * Returns the SpanTree that this node belongs to, if any.
     *
     * @return the SpanTree that this node belongs to, or null if it is not yet added to a SpanTree.
     */
    @Override
    public SpanTree getSpanTree() {
        if (parent == null) {
            return null;
        }
        return parent.getSpanTree();
    }

    /** Returns the SpanTree this belongs to and throws a nice NullPointerException if none */
    private SpanTree getNonNullSpanTree() {
        SpanTree spanTree=getSpanTree();
        if (spanTree==null)
            throw new NullPointerException(this + " is not attached to a SpanTree through its parent yet");
        return spanTree;
    }


    /**
     * Convenience method for adding an annotation to this span, same as
     * <code>getSpanTree().{@link SpanTree#annotate(SpanNode,Annotation) spanTree.annotate(this,annotation)}</code>
     *
     * @param annotation the annotation to add
     * @return this for chaining
     * @throws NullPointerException if this span is not attached to a tree
     */
    public SpanNode annotate(Annotation annotation) {
        getNonNullSpanTree().annotate(this, annotation);
        return this;
    }

    /**
     * Convenience method for adding an annotation to this span, same as
     * <code>getSpanTree().{@link SpanTree#annotate(SpanNode,AnnotationType,FieldValue) spanTree.annotate(this,type,value)}</code>
     *
     * @param type the type of the annotation to add
     * @param value the value of the annotation to add
     * @return this for chaining
     * @throws NullPointerException if this span is not attached to a tree
     */
    public SpanNode annotate(AnnotationType type,FieldValue value) {
        getNonNullSpanTree().annotate(this,type,value);
        return this;
    }

    /**
     * Convenience method for adding an annotation to this span, same as
     * <code>getSpanTree().{@link SpanTree#annotate(SpanNode,AnnotationType,FieldValue) spanTree.annotate(this,type,new StringFieldValue(value))}</code>
     *
     * @param type the type of the annotation to add
     * @param value the string value of the annotation to add
     * @return this for chaining
     * @throws NullPointerException if this span is not attached to a tree
     */
    public SpanNode annotate(AnnotationType type,String value) {
        getNonNullSpanTree().annotate(this, type, new StringFieldValue(value));
        return this;
    }

    /**
     * Convenience method for adding an annotation to this span, same as
     * <code>getSpanTree().{@link SpanTree#annotate(SpanNode,AnnotationType,FieldValue) spanTree.annotate(this,type,new IntegerFieldValue(value))}</code>
     *
     * @param type the type of the annotation to add
     * @param value the integer value of the annotation to add
     * @return this for chaining
     * @throws NullPointerException if this span is not attached to a tree
     */
    public SpanNode annotate(AnnotationType type,Integer value) {
        getNonNullSpanTree().annotate(this, type, new IntegerFieldValue(value));
        return this;
    }

    /**
     * Convenience method for adding an annotation with no value to this span, same as
     * <code>getSpanTree().{@link SpanTree#annotate(SpanNode,AnnotationType) spanTree.annotate(this,type)}</code>
     *
     * @param type the type of the annotation to add
     * @return this for chaining
     * @throws NullPointerException if this span is not attached to a tree
     */
    public SpanNode annotate(AnnotationType type) {
        getNonNullSpanTree().annotate(this,type);
        return this;
    }

    /**
     * Returns the StringFieldValue that this node belongs to, if any.
     *
     * @return the StringFieldValue that this node belongs to, if any, otherwise null.
     */
    @Override
    public StringFieldValue getStringFieldValue() {
        if (parent == null) {
            return null;
        }
        return parent.getStringFieldValue();
    }

    /**
     * Returns true if this node is a leaf node in the tree.
     *
     * @return true if this node is a leaf node in the tree.
     */
    public abstract boolean isLeafNode();

    /**
     * Traverses all immediate children of this SpanNode.
     *
     * @return a ListIterator which traverses all immediate children of this SpanNode
     */
    public abstract ListIterator<SpanNode> childIterator();

    /**
     * Recursively traverses all possible children (not only leaf nodes) of this SpanNode, in a depth-first fashion.
     *
     * @return a ListIterator which recursively traverses all children and their children etc. of this SpanNode.
     */
    public abstract ListIterator<SpanNode> childIteratorRecursive();

    /**
     * Returns the character index where this SpanNode starts (inclusive).
     *
     * @return the character index where this SpanNode starts (inclusive).
     */
    public abstract int getFrom();

    /**
     * Returns the character index where this SpanNode ends (exclusive).
     *
     * @return the character index where this SpanNode ends (exclusive).
     */
    public abstract int getTo();

    /**
     * Returns the length of this span, i.e.&nbsp;getFrom() - getTo().
     *
     * @return the length of this span
     */
    public abstract int getLength();

    /**
     * Returns the text that is covered by this SpanNode.
     *
     * @param text the input text
     * @return the text that is covered by this SpanNode.
     */
    public abstract CharSequence getText(CharSequence text);

    /**
     * Checks if the text covered by this span overlaps with the text covered by another span.
     *
     * @param o the other SpanNode to check
     * @return true if spans are overlapping, false otherwise
     */
    public boolean overlaps(SpanNode o) {
        int from = getFrom();
        int otherFrom = o.getFrom();
        int to = getTo();
        int otherTo = o.getTo();

        //is other from within our range, or vice versa?
        if ((otherFrom >= from && otherFrom < to)
            || (from >= otherFrom && from < otherTo)) {
            return true;
        }

        //is other to within our range, or vice versa?
        if ((otherTo > from && otherTo <= to)
            || (to > otherFrom && to <= otherTo)) {
            return true;
        }
        return false;
    }

    /**
     * Checks if the text covered by another span is within the text covered by this span.
     *
     * @param o the other SpanNode to check.
     * @return true if the text covered by another span is within the text covered by this span, false otherwise.
     */
    public boolean contains(SpanNode o) {
        int from = getFrom();
        int otherFrom = o.getFrom();
        int to = getTo();
        int otherTo = o.getTo();

        if (otherFrom >= from && otherTo <= to) {
            //other span node is within our range:
            return true;
        }
        return false;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SpanNode)) return false;

        SpanNode spanNode = (SpanNode) o;

        if (getFrom() != spanNode.getFrom()) return false;
        if (getTo() != spanNode.getTo()) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = getFrom();
        result = 31 * result + getTo();
        return result;
    }

    /**
     * Compares two SpanNodes.&nbsp;Note: this class has a natural ordering that <strong>might be</strong> inconsistent with equals.
     * <p>
     * First, getFrom() is compared, and -1 or 1 is return if our getFrom() is smaller or greater that o.getFrom(), respectively.
     * If and only if getFrom() is equal, getTo() is compared, and  -1 or 1 is return if our getTo() is smaller or greater that o.getTo(), respectively.
     * In all other cases, the two SpanNodes are equal both for getFrom() and getTo(), and 0 is returned.
     * <p>
     * Note that if a subclass has overridden equals(), which is very likely, but has not overridden compareTo(), then that subclass
     * will have a natural ordering that is inconsistent with equals.
     *
     * @param o the SpanNode to compare to
     * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object
     */
    @Override
    public int compareTo(SpanNode o) {
        int from = getFrom();
        int otherFrom = o.getFrom();

        if (from < otherFrom) {
            return -1;
        }
        if (from > otherFrom) {
            return 1;
        }

        //so from is equal. Check to:
        int to = getTo();
        int otherTo = o.getTo();

        if (to < otherTo) {
            return -1;
        }
        if (to > otherTo) {
            return 1;
        }

        //both from and to are equal
        return 0;
    }
}