blob: b830ba13ed536cfeb75e689c856a75a8e498af95 (
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query;
/**
* An immutable numeric range limit which can be inclusive or exclusive
*
* @author bratseth
*/
public class Limit {
public static final Limit NEGATIVE_INFINITY = new Limit(Double.NEGATIVE_INFINITY, false);
public static final Limit POSITIVE_INFINITY = new Limit(Double.POSITIVE_INFINITY, false);
private final Number number;
private final boolean inclusive;
private final boolean infinite;
public Limit(Number number, boolean inclusive) {
this.number = number;
this.infinite = Double.isInfinite(number.doubleValue());
this.inclusive = inclusive || infinite;
}
public Number number() { return number; }
/** Returns true if this limit includes its number, false if it excludes it */
public boolean isInclusive() { return inclusive; }
String toRangeStart() {
return (inclusive ? "[" : "<" ) + (isInfinite() ? "" : number.toString());
}
String toRangeEnd() {
return (isInfinite() ? "" : number.toString()) + (inclusive ? "]" : ">" );
}
/** Returns the smaller of this and the given limit */
public Limit min(Limit other) {
return this.isSmallerOrEqualTo(other) ? this : other;
}
/** Returns the larger of this and the given limit */
public Limit max(Limit other) {
return this.isLargerOrEqualTo(other) ? this : other;
}
public boolean isSmallerOrEqualTo(Limit other) {
double thisNumber = this.number().doubleValue();
double otherNumber = other.number().doubleValue();
if (thisNumber == otherNumber) {
if ( ! other.isInclusive()) return false;
return true;
}
return thisNumber < otherNumber;
}
public boolean isLargerOrEqualTo(Limit other) {
double thisNumber = this.number().doubleValue();
double otherNumber = other.number().doubleValue();
if (thisNumber == otherNumber) {
if ( ! other.isInclusive()) return false;
return true;
}
return thisNumber > otherNumber;
}
public boolean isInfinite() { return infinite; }
@Override
public String toString() {
return number + " (" + (inclusive ? "inclusive" : "exclusive") + ")";
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if ( ! (o instanceof Limit)) return false;
Limit other = (Limit)o;
if (Boolean.compare(other.inclusive, this.inclusive) != 0) return false;
return this.number.equals(other.number);
}
@Override
public int hashCode() {
return number.hashCode() + (inclusive ? 1 : 0);
}
}
|