aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/textserialize/item/AndNotRestConverter.java
blob: 795a78157c57989109b2dced8a8135a25143295f (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.textserialize.item;

import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.NotItem;

import java.util.List;

import static com.yahoo.search.query.textserialize.item.ListUtil.butFirst;
import static com.yahoo.search.query.textserialize.item.ListUtil.first;

/**
 * @author Tony Vaagenes
 */
public class AndNotRestConverter extends CompositeConverter<NotItem> {

    static final String andNotRest = "AND-NOT-REST";

    public AndNotRestConverter() {
        super(NotItem.class);
    }

    @Override
    protected void addChildren(NotItem item, ItemArguments arguments, ItemContext context) {
        if (firstIsNull(arguments.children)) {
            addNegativeItems(item, arguments.children);
        } else {
            addItems(item, arguments.children);
        }
    }

    private void addNegativeItems(NotItem notItem, List<Object> children) {
        for (Object child: butFirst(children)) {
            TypeCheck.ensureInstanceOf(child, Item.class);
            notItem.addNegativeItem((Item) child);
        }
    }

    private void addItems(NotItem notItem, List<Object> children) {
        for (Object child : children) {
            TypeCheck.ensureInstanceOf(child, Item.class);
            notItem.addItem((Item) child);
        }
    }


    private boolean firstIsNull(List<Object> children) {
        return !children.isEmpty() && first(children) == null;
    }

    @Override
    protected String getFormName(Item item) {
        return andNotRest;
    }

}