3030 * Represents an HTTP (byte) range for use with the HTTP {@code "Range"} header.
3131 *
3232 * @author Arjen Poutsma
33+ * @author Juergen Hoeller
34+ * @since 4.2
3335 * @see <a href="http://tools.ietf.org/html/rfc7233">HTTP/1.1: Range Requests</a>
3436 * @see HttpHeaders#setRange(List)
3537 * @see HttpHeaders#getRange()
36- * @since 4.2
3738 */
3839public abstract class HttpRange {
3940
@@ -111,7 +112,7 @@ public static List<HttpRange> parseRanges(String ranges) {
111112 }
112113
113114 private static HttpRange parseRange (String range ) {
114- Assert .notNull (range );
115+ Assert .hasLength (range , "Range String must not be empty" );
115116 int dashIdx = range .indexOf ('-' );
116117 if (dashIdx > 0 ) {
117118 long firstPos = Long .parseLong (range .substring (0 , dashIdx ));
@@ -139,27 +140,18 @@ else if (dashIdx == 0) {
139140 * @return the string representation
140141 */
141142 public static String toString (Collection <HttpRange > ranges ) {
142- Assert .notNull (ranges );
143+ Assert .notEmpty (ranges , "Ranges Collection must not be empty" );
143144 StringBuilder builder = new StringBuilder (BYTE_RANGE_PREFIX );
144145 for (Iterator <HttpRange > iterator = ranges .iterator (); iterator .hasNext (); ) {
145146 HttpRange range = iterator .next ();
146- range . appendTo ( builder );
147+ builder . append ( range );
147148 if (iterator .hasNext ()) {
148149 builder .append (", " );
149150 }
150151 }
151152 return builder .toString ();
152153 }
153154
154- @ Override
155- public String toString () {
156- StringBuilder builder = new StringBuilder ();
157- appendTo (builder );
158- return builder .toString ();
159- }
160-
161- abstract void appendTo (StringBuilder builder );
162-
163155
164156 /**
165157 * Represents an HTTP/1.1 byte range, with a first and optional last position.
@@ -173,19 +165,18 @@ private static class ByteRange extends HttpRange {
173165
174166 private final Long lastPos ;
175167
176-
177- private ByteRange (long firstPos , Long lastPos ) {
168+ public ByteRange (long firstPos , Long lastPos ) {
178169 assertPositions (firstPos , lastPos );
179170 this .firstPos = firstPos ;
180171 this .lastPos = lastPos ;
181172 }
182173
183174 private void assertPositions (long firstBytePos , Long lastBytePos ) {
184175 if (firstBytePos < 0 ) {
185- throw new IllegalArgumentException ("Invalid firstPos= " + firstBytePos );
176+ throw new IllegalArgumentException ("Invalid first byte position: " + firstBytePos );
186177 }
187178 if (lastBytePos != null && lastBytePos < firstBytePos ) {
188- throw new IllegalArgumentException ("firstPost= " + firstBytePos +
179+ throw new IllegalArgumentException ("firstBytePosition= " + firstBytePos +
189180 " should be less then or equal to lastBytePosition=" + lastBytePos );
190181 }
191182 }
@@ -206,34 +197,37 @@ public long getRangeEnd(long length) {
206197 }
207198
208199 @ Override
209- void appendTo (StringBuilder builder ) {
210- builder .append (this .firstPos );
211- builder .append ('-' );
212- if (this .lastPos != null ) {
213- builder .append (this .lastPos );
214- }
215- }
216-
217- @ Override
218- public boolean equals (Object o ) {
219- if (this == o ) {
200+ public boolean equals (Object other ) {
201+ if (this == other ) {
220202 return true ;
221203 }
222- if (!(o instanceof ByteRange )) {
204+ if (!(other instanceof ByteRange )) {
223205 return false ;
224206 }
225- ByteRange other = (ByteRange ) o ;
226- return this .firstPos == other .firstPos && ObjectUtils .nullSafeEquals (this .lastPos , other .lastPos );
207+ ByteRange otherRange = (ByteRange ) other ;
208+ return (this .firstPos == otherRange .firstPos &&
209+ ObjectUtils .nullSafeEquals (this .lastPos , otherRange .lastPos ));
227210 }
228211
229212 @ Override
230213 public int hashCode () {
231- int hashCode = ObjectUtils .nullSafeHashCode (this .firstPos );
232- hashCode = 31 * hashCode + ObjectUtils .nullSafeHashCode (this .lastPos );
233- return hashCode ;
214+ return (ObjectUtils .nullSafeHashCode (this .firstPos ) * 31 +
215+ ObjectUtils .nullSafeHashCode (this .lastPos ));
216+ }
217+
218+ @ Override
219+ public String toString () {
220+ StringBuilder builder = new StringBuilder ();
221+ builder .append (this .firstPos );
222+ builder .append ('-' );
223+ if (this .lastPos != null ) {
224+ builder .append (this .lastPos );
225+ }
226+ return builder .toString ();
234227 }
235228 }
236229
230+
237231 /**
238232 * Represents an HTTP/1.1 suffix byte range, with a number of suffix bytes.
239233 * @see <a href="http://tools.ietf.org/html/rfc7233#section-2.1">Byte Ranges</a>
@@ -243,10 +237,9 @@ private static class SuffixByteRange extends HttpRange {
243237
244238 private final long suffixLength ;
245239
246-
247- private SuffixByteRange (long suffixLength ) {
240+ public SuffixByteRange (long suffixLength ) {
248241 if (suffixLength < 0 ) {
249- throw new IllegalArgumentException ("Invalid suffixLength= " + suffixLength );
242+ throw new IllegalArgumentException ("Invalid suffix length: " + suffixLength );
250243 }
251244 this .suffixLength = suffixLength ;
252245 }
@@ -268,26 +261,26 @@ public long getRangeEnd(long length) {
268261 }
269262
270263 @ Override
271- void appendTo (StringBuilder builder ) {
272- builder .append ('-' );
273- builder .append (this .suffixLength );
274- }
275-
276- @ Override
277- public boolean equals (Object o ) {
278- if (this == o ) {
264+ public boolean equals (Object other ) {
265+ if (this == other ) {
279266 return true ;
280267 }
281- if (!(o instanceof SuffixByteRange )) {
268+ if (!(other instanceof SuffixByteRange )) {
282269 return false ;
283270 }
284- SuffixByteRange other = (SuffixByteRange ) o ;
285- return this .suffixLength == other .suffixLength ;
271+ SuffixByteRange otherRange = (SuffixByteRange ) other ;
272+ return ( this .suffixLength == otherRange .suffixLength ) ;
286273 }
287274
288275 @ Override
289276 public int hashCode () {
290277 return ObjectUtils .hashCode (this .suffixLength );
291278 }
279+
280+ @ Override
281+ public String toString () {
282+ return "-" + this .suffixLength ;
283+ }
292284 }
285+
293286}
0 commit comments