aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/engine/Match.java
blob: 6ed09926ccbbd3d9e3a6467a58f682a6d8585d6f (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.semantics.engine;

import com.yahoo.prelude.query.CompositeItem;
import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.TermItem;
import com.yahoo.prelude.query.WordItem;

import java.util.Objects;

/**
 * A match
 *
 * @author bratseth
 */
public class Match {

    /** The start position of this match */
    private final int position;

    private final TermItem item;

    /** The string to replace the match by, usually item.getIndexedString() */
    private final String replaceValue;

    /** The parent of the matched item */
    private final CompositeItem parent;

    /**
     * Creates a match
     *
     * @param item the match to add
     * @param replaceValue the string to replace this match by, usually the item.getIndexedString()
     *        which is what the replace value will be if it is passed as null here
     */
    public Match(FlattenedItem item, String replaceValue) {
        this.item = item.getItem();
        if (replaceValue == null)
            this.replaceValue = item.getItem().getIndexedString();
        else
            this.replaceValue = replaceValue;
        this.parent = this.item.getParent();
        this.position = item.getPosition();
    }

    public int getPosition() { return position; }

    public TermItem getItem() { return item; }

    public String getReplaceValue() {
        return replaceValue;
    }

    /**
     * Returns the parent in which the item was matched, or null if the item was root.
     * Note that the item may subsequently have been removed, so it does not necessarily
     * have this parent
     */
    public CompositeItem getParent() { return parent; }

    /** Returns a new item representing this match */
    public Item toItem(String label) {
        return toItem(label, getReplaceValue());
    }

    /** Returns a new item representing this match */
    public Item toItem(String label, String term) {
        var newItem = new WordItem(term, label);
        newItem.setWeight(item.getWeight());
        return newItem;
    }

    @Override
    public int hashCode() {
        return Objects.hash(item.getIndexedString(), item.getIndexName());
    }

    @Override
    public boolean equals(Object o) {
        if (! (o instanceof Match)) return false;

        Match other = (Match)o;
        if (other.position != position) return false;
        if ( ! other.item.equals(item)) return false;

        return true;
    }

    @Override
    public String toString() {
        return "match of " + item + " at " + position + " to be replaced by " + replaceValue;
    }

}