summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/query/NotItem.java
blob: 2dc1d09e1299ec913c057aa86432053f2d4e8cc5 (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
// Copyright Yahoo. 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 which should be excluded from the result.
 *
 * @author bratseth
 */
// TODO: Handle nulls by creating nullItem or checking in encode/toString
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 null 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 tolerate nulls and to append "+"
     * to the first item and "-" to the rest
     */
    protected void appendBodyString(StringBuilder buffer) {
        boolean isFirstItem = true;

        for (Iterator<Item> i = getItemIterator(); i.hasNext();) {
            Item item = i.next();

            if (isFirstItem) {
                buffer.append("+");
            } else {
                buffer.append(" -");
            }
            if (item == null) {
                buffer.append("(null)");
            } else {
                buffer.append(item.toString());
            }
            isFirstItem = false;
        }
    }

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

}