1616 * The same as {0,N}.</li>
1717 * <li>{N}: Just retrieve the N-th element. The same as {N,N+1}.</li>
1818 * </ul>
19- *
19+ * <p>
2020 * You can use the {@link Range} class like this:
21- *
21+ *
2222 * <pre>
2323 * Range fromAndTo = Range.build().from(1).to(5);
2424 * Range fromOnly = Range.build().from(3).build();
2525 * Range toOnly = Range.build().to(5).build();
2626 * Range only = Range.build().only(3);
2727 * </pre>
28- *
29- * @author Karl Heinz Marbaise
3028 *
29+ * @author Karl Heinz Marbaise
3130 */
3231public final class Range {
3332
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" ;
3641
3742 private Integer from ;
3843 private Integer to ;
3944
4045 private Range () {
41- this .from = null ;
42- this .to = null ;
46+ this .from = null ;
47+ this .to = null ;
4348 }
4449
4550 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 ;
5156 }
5257
5358 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 ;
5964 }
6065
6166 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+ }
6772
68- sb .append (',' );
73+ sb .append (',' );
6974
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+ }
7378
74- sb .append (CURLY_BRACKET_CLOSE );
75- return sb .toString ();
79+ sb .append (CURLY_BRACKET_CLOSE );
80+ return sb .toString ();
7681 }
7782
7883 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+ }
96101 }
97102
98103 public static final class ToBuilder {
99- private Range range ;
104+ private Range range ;
100105
101- public ToBuilder (Range range ) {
102- this .range = range ;
103- }
106+ public ToBuilder (Range range ) {
107+ this .range = range ;
108+ }
104109
105- public Range build () {
106- return this .range ;
107- }
110+ public Range build () {
111+ return this .range ;
112+ }
108113 }
109114
110115 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+ }
132137 }
133138
134139 public static Builder build () {
135- return new Builder ();
140+ return new Builder ();
136141 }
137142}
0 commit comments