summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/querytransform/QueryTreeUtil.java
blob: 6a7e0d69db4cc6cbfffe049c412ad17fbd61c63f (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.querytransform;

import com.yahoo.prelude.query.AndItem;
import com.yahoo.prelude.query.Item;
import com.yahoo.search.Query;
import com.yahoo.search.query.QueryTree;

/**
 * Utility class for manipulating a QueryTree.
 *
 * @author geirst
 */
public class QueryTreeUtil {

    static public void andQueryItemWithRoot(Query query, Item item) {
        andQueryItemWithRoot(query.getModel().getQueryTree(), item);
    }

    static public void andQueryItemWithRoot(QueryTree tree, Item item) {
        if (tree.isEmpty()) {
            tree.setRoot(item);
        } else {
            Item oldRoot = tree.getRoot();
            if (oldRoot.getClass() == AndItem.class) {
                ((AndItem) oldRoot).addItem(item);
            } else {
                AndItem newRoot = new AndItem();
                newRoot.addItem(oldRoot);
                newRoot.addItem(item);
                tree.setRoot(newRoot);
            }
        }
    }
}