16
16
* The same as {0,N}.</li>
17
17
* <li>{N}: Just retrieve the N-th element. The same as {N,N+1}.</li>
18
18
* </ul>
19
- *
19
+ * <p>
20
20
* You can use the {@link Range} class like this:
21
- *
21
+ *
22
22
* <pre>
23
23
* Range fromAndTo = Range.build().from(1).to(5);
24
24
* Range fromOnly = Range.build().from(3).build();
25
25
* Range toOnly = Range.build().to(5).build();
26
26
* Range only = Range.build().only(3);
27
27
* </pre>
28
- *
29
- * @author Karl Heinz Marbaise
30
28
*
29
+ * @author Karl Heinz Marbaise
31
30
*/
32
31
public final class Range {
33
32
34
- public static final String CURLY_BRACKET_OPEN = "%7B" ; // {
35
- public static final String CURLY_BRACKET_CLOSE = "%7D" ; // }
33
+ /**
34
+ * This represents {@code {} (left curly bracket).
35
+ */
36
+ public static final String CURLY_BRACKET_OPEN = "%7B" ;
37
+ /**
38
+ * This represents {@code }} (right curly bracket).
39
+ */
40
+ public static final String CURLY_BRACKET_CLOSE = "%7D" ;
36
41
37
42
private Integer from ;
38
43
private Integer to ;
39
44
40
45
private Range () {
41
- this .from = null ;
42
- this .to = null ;
46
+ this .from = null ;
47
+ this .to = null ;
43
48
}
44
49
45
50
private Range setFrom (int from ) {
46
- if (from < 0 ) {
47
- throw new IllegalArgumentException ("from value must be greater or equal null." );
48
- }
49
- this .from = new Integer (from );
50
- return this ;
51
+ if (from < 0 ) {
52
+ throw new IllegalArgumentException ("from value must be greater or equal null." );
53
+ }
54
+ this .from = new Integer (from );
55
+ return this ;
51
56
}
52
57
53
58
private Range setTo (int to ) {
54
- if (to < 0 ) {
55
- throw new IllegalArgumentException ("to must be greater or equal null." );
56
- }
57
- this .to = new Integer (to );
58
- return this ;
59
+ if (to < 0 ) {
60
+ throw new IllegalArgumentException ("to must be greater or equal null." );
61
+ }
62
+ this .to = new Integer (to );
63
+ return this ;
59
64
}
60
65
61
66
public String getRangeString () {
62
- StringBuilder sb = new StringBuilder ();
63
- sb .append (CURLY_BRACKET_OPEN );
64
- if (this .from != null ) {
65
- sb .append (String .format ("%d" , this .from ));
66
- }
67
+ StringBuilder sb = new StringBuilder ();
68
+ sb .append (CURLY_BRACKET_OPEN );
69
+ if (this .from != null ) {
70
+ sb .append (String .format ("%d" , this .from ));
71
+ }
67
72
68
- sb .append (',' );
73
+ sb .append (',' );
69
74
70
- if (this .to != null ) {
71
- sb .append (String .format ("%d" , this .to ));
72
- }
75
+ if (this .to != null ) {
76
+ sb .append (String .format ("%d" , this .to ));
77
+ }
73
78
74
- sb .append (CURLY_BRACKET_CLOSE );
75
- return sb .toString ();
79
+ sb .append (CURLY_BRACKET_CLOSE );
80
+ return sb .toString ();
76
81
}
77
82
78
83
public static final class FromBuilder {
79
- private Range range ;
80
-
81
- public FromBuilder (Range range ) {
82
- this .range = range ;
83
- }
84
-
85
- public Range to (int t ) {
86
- this .range .setTo (t );
87
- if (range .to <= range .from ) {
88
- throw new IllegalArgumentException ("to must be greater than from" );
89
- }
90
- return this .range ;
91
- }
92
-
93
- public Range build () {
94
- return this .range ;
95
- }
84
+ private Range range ;
85
+
86
+ public FromBuilder (Range range ) {
87
+ this .range = range ;
88
+ }
89
+
90
+ public Range to (int t ) {
91
+ this .range .setTo (t );
92
+ if (range .to <= range .from ) {
93
+ throw new IllegalArgumentException ("to must be greater than from" );
94
+ }
95
+ return this .range ;
96
+ }
97
+
98
+ public Range build () {
99
+ return this .range ;
100
+ }
96
101
}
97
102
98
103
public static final class ToBuilder {
99
- private Range range ;
104
+ private Range range ;
100
105
101
- public ToBuilder (Range range ) {
102
- this .range = range ;
103
- }
106
+ public ToBuilder (Range range ) {
107
+ this .range = range ;
108
+ }
104
109
105
- public Range build () {
106
- return this .range ;
107
- }
110
+ public Range build () {
111
+ return this .range ;
112
+ }
108
113
}
109
114
110
115
public static final class Builder {
111
- private Range range ;
112
-
113
- protected Builder () {
114
- this .range = new Range ();
115
- }
116
-
117
- public FromBuilder from (int f ) {
118
- this .range .setFrom (f );
119
- return new FromBuilder (this .range );
120
- }
121
-
122
- public ToBuilder to (int t ) {
123
- this .range .setTo (t );
124
- return new ToBuilder (this .range );
125
- }
126
-
127
- public Range only (int only ) {
128
- this .range .from = new Integer (only );
129
- this .range .to = new Integer (only + 1 );
130
- return this .range ;
131
- }
116
+ private Range range ;
117
+
118
+ protected Builder () {
119
+ this .range = new Range ();
120
+ }
121
+
122
+ public FromBuilder from (int f ) {
123
+ this .range .setFrom (f );
124
+ return new FromBuilder (this .range );
125
+ }
126
+
127
+ public ToBuilder to (int t ) {
128
+ this .range .setTo (t );
129
+ return new ToBuilder (this .range );
130
+ }
131
+
132
+ public Range only (int only ) {
133
+ this .range .from = new Integer (only );
134
+ this .range .to = new Integer (only + 1 );
135
+ return this .range ;
136
+ }
132
137
}
133
138
134
139
public static Builder build () {
135
- return new Builder ();
140
+ return new Builder ();
136
141
}
137
142
}
0 commit comments