diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java index 293c43fb60..5ab0e9c8d8 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java @@ -128,7 +128,7 @@ public final class BeanFactoryUtilsTests { public void testNoBeansOfType() { StaticListableBeanFactory lbf = new StaticListableBeanFactory(); lbf.addBean("foo", new Object()); - Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false); + Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false); assertTrue(beans.isEmpty()); } @@ -231,7 +231,7 @@ public final class BeanFactoryUtilsTests { Object test3 = this.listableBeanFactory.getBean("test3"); Object test = this.listableBeanFactory.getBean("test"); - Map beans = + Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false); assertEquals(2, beans.size()); assertEquals(test3, beans.get("test3")); diff --git a/spring-core/src/main/java/org/springframework/core/ResolvableType.java b/spring-core/src/main/java/org/springframework/core/ResolvableType.java index 35b4393015..d5407537df 100644 --- a/spring-core/src/main/java/org/springframework/core/ResolvableType.java +++ b/spring-core/src/main/java/org/springframework/core/ResolvableType.java @@ -1152,7 +1152,7 @@ public final class ResolvableType implements Serializable { * @return a {@link ResolvableType} for the specific class and generics * @see #forClassWithGenerics(Class, Class...) */ - public static ResolvableType forClassWithGenerics(final Class sourceClass, final ResolvableType... generics) { + public static ResolvableType forClassWithGenerics(Class sourceClass, ResolvableType... generics) { Assert.notNull(sourceClass, "Source class must not be null"); Assert.notNull(generics, "Generics must not be null"); TypeVariable[] variables = sourceClass.getTypeParameters(); diff --git a/spring-web/src/main/java/org/springframework/http/HttpRange.java b/spring-web/src/main/java/org/springframework/http/HttpRange.java index f10405ab77..4b8e848588 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpRange.java +++ b/spring-web/src/main/java/org/springframework/http/HttpRange.java @@ -30,10 +30,11 @@ import org.springframework.util.StringUtils; * Represents an HTTP (byte) range for use with the HTTP {@code "Range"} header. * * @author Arjen Poutsma + * @author Juergen Hoeller + * @since 4.2 * @see HTTP/1.1: Range Requests * @see HttpHeaders#setRange(List) * @see HttpHeaders#getRange() - * @since 4.2 */ public abstract class HttpRange { @@ -111,7 +112,7 @@ public abstract class HttpRange { } private static HttpRange parseRange(String range) { - Assert.notNull(range); + Assert.hasLength(range, "Range String must not be empty"); int dashIdx = range.indexOf('-'); if (dashIdx > 0) { long firstPos = Long.parseLong(range.substring(0, dashIdx)); @@ -139,11 +140,11 @@ public abstract class HttpRange { * @return the string representation */ public static String toString(Collection ranges) { - Assert.notNull(ranges); + Assert.notEmpty(ranges, "Ranges Collection must not be empty"); StringBuilder builder = new StringBuilder(BYTE_RANGE_PREFIX); for (Iterator iterator = ranges.iterator(); iterator.hasNext(); ) { HttpRange range = iterator.next(); - range.appendTo(builder); + builder.append(range); if (iterator.hasNext()) { builder.append(", "); } @@ -151,15 +152,6 @@ public abstract class HttpRange { return builder.toString(); } - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - appendTo(builder); - return builder.toString(); - } - - abstract void appendTo(StringBuilder builder); - /** * Represents an HTTP/1.1 byte range, with a first and optional last position. @@ -173,8 +165,7 @@ public abstract class HttpRange { private final Long lastPos; - - private ByteRange(long firstPos, Long lastPos) { + public ByteRange(long firstPos, Long lastPos) { assertPositions(firstPos, lastPos); this.firstPos = firstPos; this.lastPos = lastPos; @@ -182,10 +173,10 @@ public abstract class HttpRange { private void assertPositions(long firstBytePos, Long lastBytePos) { if (firstBytePos < 0) { - throw new IllegalArgumentException("Invalid firstPos=" + firstBytePos); + throw new IllegalArgumentException("Invalid first byte position: " + firstBytePos); } if (lastBytePos != null && lastBytePos < firstBytePos) { - throw new IllegalArgumentException("firstPost= " + firstBytePos + + throw new IllegalArgumentException("firstBytePosition=" + firstBytePos + " should be less then or equal to lastBytePosition=" + lastBytePos); } } @@ -206,34 +197,37 @@ public abstract class HttpRange { } @Override - void appendTo(StringBuilder builder) { + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (!(other instanceof ByteRange)) { + return false; + } + ByteRange otherRange = (ByteRange) other; + return (this.firstPos == otherRange.firstPos && + ObjectUtils.nullSafeEquals(this.lastPos, otherRange.lastPos)); + } + + @Override + public int hashCode() { + return (ObjectUtils.nullSafeHashCode(this.firstPos) * 31 + + ObjectUtils.nullSafeHashCode(this.lastPos)); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); builder.append(this.firstPos); builder.append('-'); if (this.lastPos != null) { builder.append(this.lastPos); } - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof ByteRange)) { - return false; - } - ByteRange other = (ByteRange) o; - return this.firstPos == other.firstPos && ObjectUtils.nullSafeEquals(this.lastPos, other.lastPos); - } - - @Override - public int hashCode() { - int hashCode = ObjectUtils.nullSafeHashCode(this.firstPos); - hashCode = 31 * hashCode + ObjectUtils.nullSafeHashCode(this.lastPos); - return hashCode; + return builder.toString(); } } + /** * Represents an HTTP/1.1 suffix byte range, with a number of suffix bytes. * @see Byte Ranges @@ -243,10 +237,9 @@ public abstract class HttpRange { private final long suffixLength; - - private SuffixByteRange(long suffixLength) { + public SuffixByteRange(long suffixLength) { if (suffixLength < 0) { - throw new IllegalArgumentException("Invalid suffixLength=" + suffixLength); + throw new IllegalArgumentException("Invalid suffix length: " + suffixLength); } this.suffixLength = suffixLength; } @@ -268,26 +261,26 @@ public abstract class HttpRange { } @Override - void appendTo(StringBuilder builder) { - builder.append('-'); - builder.append(this.suffixLength); - } - - @Override - public boolean equals(Object o) { - if (this == o) { + public boolean equals(Object other) { + if (this == other) { return true; } - if (!(o instanceof SuffixByteRange)) { + if (!(other instanceof SuffixByteRange)) { return false; } - SuffixByteRange other = (SuffixByteRange) o; - return this.suffixLength == other.suffixLength; + SuffixByteRange otherRange = (SuffixByteRange) other; + return (this.suffixLength == otherRange.suffixLength); } @Override public int hashCode() { return ObjectUtils.hashCode(this.suffixLength); } + + @Override + public String toString() { + return "-" + this.suffixLength; + } } + } diff --git a/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java b/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java index fa71022f2b..85870b3c9b 100644 --- a/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java +++ b/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java @@ -15,20 +15,20 @@ */ package org.springframework.http; -import static org.junit.Assert.*; - import java.util.ArrayList; import java.util.List; import org.junit.Test; +import static org.junit.Assert.*; + /** - * Unit tests for {@link org.springframework.http.HttpRange}. + * Unit tests for {@link HttpRange}. + * * @author Rossen Stoyanchev */ public class HttpRangeTests { - @Test(expected = IllegalArgumentException.class) public void invalidFirstPosition() throws Exception { HttpRange.createByteRange(-1);