1 | package org.springframework.batch.item.file.transform; |
2 | |
3 | import org.springframework.util.Assert; |
4 | |
5 | /** |
6 | * A class to represent ranges. A Range can have minimum/maximum values from |
7 | * interval <1,Integer.MAX_VALUE-1> A Range can be unbounded at maximum |
8 | * side. This can be specified by passing {@link Range#UPPER_BORDER_NOT_DEFINED}} as max |
9 | * value or using constructor {@link #Range(int)}. |
10 | * |
11 | * @author peter.zozom |
12 | */ |
13 | public class Range { |
14 | |
15 | public final static int UPPER_BORDER_NOT_DEFINED = Integer.MAX_VALUE; |
16 | |
17 | private int min; |
18 | private int max; |
19 | |
20 | public Range(int min) { |
21 | checkMinMaxValues(min, UPPER_BORDER_NOT_DEFINED); |
22 | this.min = min; |
23 | this.max = UPPER_BORDER_NOT_DEFINED; |
24 | } |
25 | |
26 | public Range(int min, int max) { |
27 | checkMinMaxValues(min, max); |
28 | this.min = min; |
29 | this.max = max; |
30 | } |
31 | |
32 | public int getMax() { |
33 | return max; |
34 | } |
35 | |
36 | public int getMin() { |
37 | return min; |
38 | } |
39 | |
40 | public void setMax(int max) { |
41 | checkMinMaxValues(this.min, max); |
42 | this.max = max; |
43 | } |
44 | |
45 | public void setMin(int min) { |
46 | checkMinMaxValues(min, this.max); |
47 | this.min = min; |
48 | } |
49 | |
50 | public boolean hasMaxValue() { |
51 | return max != UPPER_BORDER_NOT_DEFINED; |
52 | } |
53 | |
54 | public String toString() { |
55 | return hasMaxValue() ? min + "-" + max : String.valueOf(min); |
56 | } |
57 | |
58 | private void checkMinMaxValues(int min, int max) { |
59 | Assert.isTrue(min>0, "Min value must be higher than zero"); |
60 | Assert.isTrue(min<=max, "Min value should be lower or equal to max value"); |
61 | } |
62 | } |