aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/annotation/Span.java
blob: 476f93a90cc0216e4c09d45ff7009a88100afca5 (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
// 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 com.yahoo.document.serialization.SpanNodeReader;

import java.util.ListIterator;
import java.util.NoSuchElementException;

/**
 * This class represents a range of characters from a string. This is the leaf node
 * in a Span tree. Its boundaries are defined as inclusive-from and exclusive-to.
 *
 * @author baldersheim
 * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 */
public class Span extends SpanNode {
    public static final byte ID = 1;
    private int from;
    private int length;

    /**
     * This will construct a valid span or throw {@link IllegalArgumentException}
     * if the span is invalid.
     *
     * @param from   Start of the span. Must be &gt;= 0.
     * @param length of the span. Must be &gt;= 0.
     * @throws IllegalArgumentException if illegal span
     */
    public Span(int from, int length) {
        setFrom(from);
        setLength(length);
    }

    /**
     * Creates an empty Span, used mainly for deserialization.
     *
     * @param reader the reader that must populate this Span instance
     */
    public Span(SpanNodeReader reader) {
        reader.read(this);
    }

    /**
     * WARNING!&nbsp;Only to be used by deserializers!&nbsp;Creates an empty Span instance.
     */
    public Span() {
    }

    /**
     * Copies the given Span into a new Span instance.
     *
     * @param spanToCopy the Span to copy.
     */
    public Span(Span spanToCopy) {
        this(spanToCopy.getFrom(), spanToCopy.getLength());
    }

    @Override
    public final int getFrom() {
        return from;
    }

    /**
     * NOTE: DO NOT USE. Should only be used by {@link com.yahoo.document.serialization.SpanNodeReader}.
     * @param from the from value to set
     */
    public void setFrom(int from) {
        if (from < 0) {
            throw new IllegalArgumentException("From cannot be < 0. (Was " + from + ").");
        }
        this.from = from;
    }

    @Override
    public final int getTo() {
        return from + length;
    }

    @Override
    public final int getLength() {
        return length;
    }

    /**
     * NOTE: DO NOT USE. Should only be used by {@link com.yahoo.document.serialization.SpanNodeReader}.
     * @param length the length value to set
     */
    public void setLength(int length) {
        if (length < 0) {
            throw new IllegalArgumentException("Length cannot be < 0. (Was " + length + ").");
        }
        this.length = length;
    }

    public String toString() {
        return new StringBuilder("span [").append(from).append(',').append(getTo()).append('>').toString();
    }

    @Override
    public final CharSequence getText(CharSequence text) {
        return text.subSequence(from, getTo());
    }

    /**
     * Always returns true.
     *
     * @return always true.
     */
    @Override
    public boolean isLeafNode() {
        return true;
    }

    /**
     * Returns a ListIterator that iterates over absolutely nothing.
     *
     * @return a ListIterator that iterates over absolutely nothing.
     */
    @Override
    public ListIterator<SpanNode> childIterator() {
        return new EmptyIterator();
    }

    /**
     * Returns a ListIterator that iterates over absolutely nothing.
     *
     * @return a ListIterator that iterates over absolutely nothing.
     */
    @Override
    public ListIterator<SpanNode> childIteratorRecursive() {
        return childIterator();
    }

    private class EmptyIterator implements ListIterator<SpanNode> {
        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public SpanNode next() {
            throw new NoSuchElementException("A Span has no children");
        }

        @Override
        public boolean hasPrevious() {
            return false;
        }

        @Override
        public SpanNode previous() {
            throw new NoSuchElementException("A Span has no children");
        }

        @Override
        public int nextIndex() {
            return 0;
        }

        @Override
        public int previousIndex() {
            return 0;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("A Span has no children");
        }

        @Override
        public void set(SpanNode spanNode) {
            throw new UnsupportedOperationException("A Span has no children");
        }

        @Override
        public void add(SpanNode spanNode) {
            throw new UnsupportedOperationException("A Span has no children");
        }
    }
}