aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/grouping/request/BucketValue.java
blob: ad3fa4d02368601c9ad20cbe803f19c7847ac59b (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 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.Objects;

/**
 * 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(String label, Integer level, ConstantValue<?> inclusiveFrom, ConstantValue<?> exclusiveTo) {
        super("bucket[" + asImage(inclusiveFrom) + ", " + asImage(exclusiveTo) + ">", label, level);
        if (comparator.compare(exclusiveTo, inclusiveFrom) < 0) {
            throw new IllegalArgumentException("Bucket to-value can not be less than from-value.");
        }
        from = inclusiveFrom;
        to = exclusiveTo;
    }

    @Override
    public BucketValue copy() {
        return new BucketValue(getLabel(), getLevelOrNull(), getFrom().copy(), getTo().copy());
    }

    /**
     * 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;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof BucketValue)) return false;

        BucketValue other = (BucketValue)o;
        if ( ! Objects.equals(this.from, other.from)) return false;
        if ( ! Objects.equals(this.to, other.to)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(from, to);
    }

}