aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/query/NotItem.java
blob: 3c1186a381c6de6933111ff088ed17a4d2121f70 (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
// 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.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * A composite item where the first item is positive and the following
 * items are negative items where matches should exclude the document should from the result.
 * The default positive item, if only negatives are added, is TrueItem: Meaning that all documents are matched
 * except those matching the negative terms added.
 *
 * @author bratseth
 */
public class NotItem extends CompositeItem {

    @Override
    public ItemType getItemType() {
        return ItemType.NOT;
    }

    @Override
    public String getName() {
        return "NOT";
    }

    /** Adds an item. The first item is the positive, the rest are negative */
    @Override
    public void addItem(Item item) {
        super.addItem(item);
    }

    /**
     * Adds a negative item. Like addItem but skips the first position
     * (position 0) if it is not already set.
     */
    public void addNegativeItem(Item negative) {
        if (getItemCount() == 0)
            insertTrueFirstItem();
        addItem(negative);
    }

    /** Returns the negative items of this: All child items except the first */
    public List<Item> negativeItems() { return items().subList(1, getItemCount()); }

    /** Returns the positive item (the first subitem), or TrueItem if no positive items has been added. */
    public Item getPositiveItem() {
        if (getItemCount() == 0)
            return new TrueItem();
        return getItem(0);
    }

    /**
     * Sets the positive item (the first item)
     *
     * @return the old positive item, or TrueItem if there was none
     */
    public Item setPositiveItem(Item item) {
        Objects.requireNonNull(item, () -> "Positive item of " + this);
        if (getItemCount() == 0) {
            addItem(item);
            return null;
        } else {
            return setItem(0, item);
        }
    }

    /**
     * Convenience method for adding a positive item.
     * If a positive item is already present
     * the positive item becomes an AndItem with the items added
     */
    public void addPositiveItem(Item item) {
        if (getPositiveItem() instanceof TrueItem) {
            setPositiveItem(item);
        } else if (getPositiveItem() instanceof AndItem) {
            ((AndItem) getPositiveItem()).addItem(item);
        } else {
            AndItem positives = new AndItem();

            positives.addItem(getPositiveItem());
            positives.addItem(item);
            setPositiveItem(positives);
        }
    }

    public boolean removeItem(Item item) {
        int removedIndex = getItemIndex(item);
        boolean removed = super.removeItem(item);

        if (removed && removedIndex == 0) {
            insertTrueFirstItem();
        }
        return removed;
    }

    public Item removeItem(int index) {
        Item removed = super.removeItem(index);

        if (index == 0) { // Don't make the first negative the positive
            insertTrueFirstItem();
        }
        return removed;
    }

    private void insertTrueFirstItem() {
        addItem(0, new TrueItem());
    }

    /** Not items uses a empty heading instead of "NOT " */
    protected void appendHeadingString(StringBuilder buffer) {}

    /**
     * Overridden to skip the positive TrueItem and (otherwise) append "+"
     * to the first item and "-" to the rest
     */
    @Override
    protected void appendBodyString(StringBuilder buffer) {
        if (items().isEmpty()) return;
        if (items().size() == 1) {
            buffer.append(items().get(0));
            return;
        }
        for (int i = 0; i < items().size(); i++) {
            if (i == 0 && items().get(i) instanceof TrueItem) continue; // skip positive true

            buffer.append(i == 0 ? "+" : "-").append(items().get(i));
            if ( i < items().size() - 1)
                buffer.append(" ");
        }
    }

    /** Returns the number of actual *positive* terms in this */
    @Override
    public int getTermCount() {
        Item positive = getPositiveItem();
        return positive == null ? 0 : positive.getTermCount();
    }

}