aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/grouping/request/PredefinedFunction.java
blob: 0fcc07ead6bd79031d3b49bad6f69e8e7211dd36 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.request;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * This class represents a predefined bucket-function in a {@link GroupingExpression}. It maps the input into one of the
 * given buckets by the result of the argument expression.
 *
 * @author Simon Thoresen Hult
 */
public abstract class PredefinedFunction extends FunctionNode {

    protected PredefinedFunction(String label, Integer level, GroupingExpression exp, List<? extends BucketValue> args) {
        super("predefined", label, level, asList(exp, args));
        Iterator<? extends BucketValue> it = args.iterator();
        BucketValue prev = it.next();
        while (it.hasNext()) {
            BucketValue arg = it.next();
            if (prev.compareTo(arg) >= 0) {
                throw new IllegalArgumentException("Buckets must be monotonically increasing, got " + prev +
                                                   " before " + arg + ".");
            }
            prev = arg;
        }
    }

    /**
     * Returns the number of buckets to divide the result into.
     *
     * @return The bucket count.
     */
    public int getNumBuckets() {
        return getNumArgs() - 1;
    }

    /**
     * Returns the bucket at the given index.
     *
     * @param i The index of the bucket to return.
     * @return The bucket at the given index.
     * @throws IndexOutOfBoundsException If the index is out of range.
     */
    public BucketValue getBucket(int i) {
        return (BucketValue)getArg(i + 1);
    }

    private static List<GroupingExpression> asList(GroupingExpression exp, List<? extends BucketValue> args) {
        List<GroupingExpression> ret = new LinkedList<>();
        ret.add(exp);
        ret.addAll(args);
        return ret;
    }

}