aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search-core/src/main/java/com/yahoo/document/predicate/RangeEdgePartition.java
blob: e02926150bd7fbb0bf3503f71cf176a2cf6ce729 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.predicate;

/**
 * @author Magnar Nedland
 */
public class RangeEdgePartition extends RangePartition {

    private final long value;
    private final int lowerBound;
    private final int upperBound;

    public RangeEdgePartition(String label, long value, int lower, int upper) {
        super(label);
        this.value = value;
        this.lowerBound = lower;
        this.upperBound = upper;
    }

    public long getValue() {
        return value;
    }

    public int getLowerBound() {
        return lowerBound;
    }

    public int getUpperBound() {
        return upperBound;
    }

    @Override
    public RangeEdgePartition clone() throws CloneNotSupportedException {
        return (RangeEdgePartition)super.clone();
    }

    @Override
    public int hashCode() {
        return super.hashCode()
                + Long.valueOf(value).hashCode()
                + Integer.valueOf(lowerBound).hashCode()
                + Integer.valueOf(upperBound).hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof RangeEdgePartition)) {
            return false;
        }
        RangeEdgePartition other = (RangeEdgePartition)obj;
        return super.equals(other) &&
                value == other.value &&
                lowerBound == other.lowerBound &&
                upperBound == other.upperBound;
    }

    @Override
    protected void appendTo(StringBuilder out) {
        super.appendTo(out);

        out.append("+[");
        if (lowerBound > 0)
            out.append(lowerBound);
        out.append("..");
        if (upperBound >= 0)
            out.append(upperBound);
        out.append(']');
    }

    public long encodeBounds() {
        if (lowerBound > 0) {
            if (upperBound >= 0) {
                return lowerBound << 16 | (upperBound + 1);
            } else {
                return lowerBound | 0x80000000L;
            }
        } else {
            if (upperBound >= 0) {
                return (upperBound + 1) | 0x40000000;
            } else {
                return 0x80000000L;  // >0
            }
        }
    }

}