1
|
|
|
/* |
2
|
|
|
* This file is part of ArakneUtils. |
3
|
|
|
* |
4
|
|
|
* ArakneUtils is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* ArakneUtils is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with ArakneUtils. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* Copyright (c) 2017-2020 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.arakne.utils.value; |
21
|
|
|
|
22
|
|
|
import java.util.Objects; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Integer interval, min and max included |
26
|
|
|
* min and max can be equals |
27
|
|
|
* |
28
|
|
|
* Note: This is an immutable value object |
29
|
|
|
*/ |
30
|
|
|
final public class Interval { |
31
|
|
|
final private int min; |
32
|
|
|
final private int max; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param min Minimal value of the interval |
36
|
|
|
* @param max Maximal value of the interval |
37
|
|
|
* @throws IllegalArgumentException When max < min |
38
|
|
|
*/ |
39
|
1 |
|
public Interval(int min, int max) { |
40
|
1 |
|
if (max < min) { |
41
|
1 |
|
throw new IllegalArgumentException("max must be higher than min"); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
this.min = min; |
45
|
1 |
|
this.max = max; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The minimal value of the interval |
50
|
|
|
*/ |
51
|
|
|
public int min() { |
52
|
1 |
|
return min; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The maximal value of the interval |
57
|
|
|
*/ |
58
|
|
|
public int max() { |
59
|
1 |
|
return max; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check if the value is contained into the interval |
64
|
|
|
* The interval is inclusive |
65
|
|
|
* |
66
|
|
|
* @param value The value to check |
67
|
|
|
* |
68
|
|
|
* @return true if value is in the interface |
69
|
|
|
*/ |
70
|
|
|
public boolean contains(int value) { |
71
|
1 |
|
return value >= min && value <= max; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Modify the end of the interval |
76
|
|
|
* The returned interval will be [min, max + modifier] |
77
|
|
|
* If the new end is lower than the min (i.e. -modifier > max - min), the interval [min, min] will be returned |
78
|
|
|
* |
79
|
|
|
* @param modifier The modifier value. If positive will increase max, if negative will decrease |
80
|
|
|
* |
81
|
|
|
* @return The new interval |
82
|
|
|
*/ |
83
|
|
|
public Interval modify(int modifier) |
84
|
|
|
{ |
85
|
1 |
|
if (modifier == 0 || (min == max && modifier < 0)) { |
86
|
1 |
|
return this; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
return new Interval(min, Math.max(max + modifier, min)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
@Override |
93
|
|
|
public String toString() { |
94
|
1 |
|
return "[" + min + ", " + max + "]"; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
@Override |
98
|
|
|
public boolean equals(Object obj) { |
99
|
1 |
|
if (!(obj instanceof Interval)) { |
100
|
1 |
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
Interval other = (Interval) obj; |
104
|
|
|
|
105
|
1 |
|
return other.min == min && other.max == max; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
@Override |
109
|
|
|
public int hashCode() { |
110
|
1 |
|
return Objects.hash(min, max); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|