aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/grouping/request/FixedWidthFunction.java
blob: 04f6d155699c010445876c340d703e1d14e4e913 (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
// 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.Arrays;

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

    /**
     * Constructs a new instance of this class.
     *
     * @param exp        The expression to evaluate.
     * @param width The width of each bucket.
     */
    public FixedWidthFunction(GroupingExpression exp, Number width) {
        this(null, null, exp,
             width instanceof Double ? new DoubleValue(width.doubleValue()) : new LongValue(width.longValue()));
    }

    private FixedWidthFunction(String label, Integer level, GroupingExpression exp, ConstantValue width) {
        super("fixedwidth", label, level, Arrays.asList(exp, width));
    }

    @Override
    public FixedWidthFunction copy() {
        return new FixedWidthFunction(getLabel(), getLevelOrNull(), getArg(0).copy(), (ConstantValue)getArg(1).copy());
    }

    /**
     * Returns the number of buckets to divide the result into.
     *
     * @return The bucket count.
     */
    public Number getWidth() {
        GroupingExpression w = getArg(1);
        return (w instanceof LongValue) ? ((LongValue)w).getValue() : ((DoubleValue)w).getValue();
    }
}