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

/**
 * This class represents a bucket in a {@link PredefinedFunction}. The generic T is the data type of the range values
 * 'from' and 'to'. The range is inclusive-from and exclusive-to. All supported data types are represented as subclasses
 * of this.
 *
 * @author Simon Thoresen Hult
 */
public class BucketValue extends GroupingExpression implements Comparable<BucketValue> {

    private final ConstantValue<?> from;
    private final ConstantValue<?> to;
    private final ConstantValueComparator comparator = new ConstantValueComparator();

    protected BucketValue(ConstantValue<?> inclusiveFrom, ConstantValue<?> exclusiveTo) {
        super("bucket[" + asImage(inclusiveFrom) + ", " + asImage(exclusiveTo) + ">");
        if (comparator.compare(exclusiveTo, inclusiveFrom) < 0) {
            throw new IllegalArgumentException("Bucket to-value can not be less than from-value.");
        }
        from = inclusiveFrom;
        to = exclusiveTo;
    }

    /**
     * Returns the inclusive-from value of this bucket.
     *
     * @return The from-value.
     */
    public ConstantValue<?> getFrom() {
        return from;
    }

    /**
     * Returns the exclusive-to value of this bucket.
     *
     * @return The to-value.
     */
    public ConstantValue<?> getTo() {
        return to;
    }

    @Override
    public int compareTo(BucketValue rhs) {
        if (comparator.compare(to, rhs.from) <= 0) {
            return -1;
        }
        if (comparator.compare(from, rhs.to) >= 0) {
            return 1;
        }
        return 0;
    }
}