aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/query/Substring.java
blob: b3f1ee81b49b36597b72f327af2107e82ca00657 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query;

import java.util.Objects;

/**
 * An substring which also provides access to the full (query) string it is a substring of.
 * This is a value object.
 *
 * @author bratseth
 */
public class Substring {

    /** The start of the substring */
    public final int start;

    /** The end of the substring */
    public final int end;

    /** The string this is a substring of */
    public final String string;

    /** Creates a substring which is identical to the string containing it */
    public Substring(String string) {
        this.start = 0;
        this.end = string.length();
        this.string=string;
    }

    public Substring(int start, int end, String string) {
        this.start = start;
        this.end = end;
        this.string=string;
    }

    public String getValue() {
        return string.substring(start, end);
    }

    /** Returns the entire string this is a substring of. The start and end offsets are into this string. */
    public String getSuperstring() { return string; }

    /**
     * Returns the character n places (0 base) after the end of the value substring into the superstring.
     * For example charAfter(0) returns the first character after the end of the substring
     *
     * @return the char n planes after the end of the substring
     * @throws IndexOutOfBoundsException if the string is not long enough to have a character at this position
     */
    public char charAfter(int n) {
        return string.charAt(end+n);
    }

    /**
     * Returns the character n places (0 base) before the start of the value substring into the superstring.
     * For example charBefore(0) returns the first character before the start of the substring
     *
     * @return the char n planes before the start of the substring
     * @throws IndexOutOfBoundsException if the string does not have a character at this position
     */
    public char charBefore(int n) {
        return string.charAt(start-1-n);
    }

    @Override
    public String toString() {
        return "(" + start + ' ' + end + ')';
    }

    @Override
    public boolean equals(Object o) {
        if ( ! (o instanceof Substring)) return false;
        var other = (Substring)o;
        if (this.start != other.start) return false;
        if (this.end != other.end) return false;
        if (! Objects.equals(this.string, other.string)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(start, end, string);
    }

}