aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/engine/Match.java
blob: 691aeb33d26314048c506be5b795ef8aedd02b79 (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
// Copyright 2017 Yahoo Holdings. 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;

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

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

    private TermItem item;

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

    /** The parent of the matched item */
    private CompositeItem parent=null;

    /**
     * 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; }

    public int hashCode() {
        return
                17*item.getIndexedString().hashCode()+
                33*item.getIndexName().hashCode();
    }

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

    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;
    }

}