Polishing

This commit is contained in:
Juergen Hoeller
2018-11-22 20:20:36 +01:00
parent ed9afa3686
commit 85b5c5a134
18 changed files with 118 additions and 115 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -170,7 +170,7 @@ public abstract class ObjectUtils {
/**
* Check whether the given array of enum constants contains a constant with the given name,
* ignoring case when determining a match.
* @param enumValues the enum values to check, typically the product of a call to MyEnum.values()
* @param enumValues the enum values to check, typically obtained via {@code MyEnum.values()}
* @param constant the constant name to find (must not be null or empty string)
* @return whether the constant has been found in the given array
*/
@@ -180,15 +180,14 @@ public abstract class ObjectUtils {
/**
* Check whether the given array of enum constants contains a constant with the given name.
* @param enumValues the enum values to check, typically the product of a call to MyEnum.values()
* @param enumValues the enum values to check, typically obtained via {@code MyEnum.values()}
* @param constant the constant name to find (must not be null or empty string)
* @param caseSensitive whether case is significant in determining a match
* @return whether the constant has been found in the given array
*/
public static boolean containsConstant(Enum<?>[] enumValues, String constant, boolean caseSensitive) {
for (Enum<?> candidate : enumValues) {
if (caseSensitive ?
candidate.toString().equals(constant) :
if (caseSensitive ? candidate.toString().equals(constant) :
candidate.toString().equalsIgnoreCase(constant)) {
return true;
}
@@ -199,7 +198,7 @@ public abstract class ObjectUtils {
/**
* Case insensitive alternative to {@link Enum#valueOf(Class, String)}.
* @param <E> the concrete Enum type
* @param enumValues the array of all Enum constants in question, usually per Enum.values()
* @param enumValues the array of all Enum constants in question, usually per {@code Enum.values()}
* @param constant the constant to get the enum value of
* @throws IllegalArgumentException if the given constant is not found in the given array
* of enum values. Use {@link #containsConstant(Enum[], String)} as a guard to avoid this exception.
@@ -210,9 +209,8 @@ public abstract class ObjectUtils {
return candidate;
}
}
throw new IllegalArgumentException(
String.format("constant [%s] does not exist in enum type %s",
constant, enumValues.getClass().getComponentType().getName()));
throw new IllegalArgumentException("Constant [" + constant + "] does not exist in enum type " +
enumValues.getClass().getComponentType().getName());
}
/**

View File

@@ -25,7 +25,6 @@ import java.lang.reflect.UndeclaredThrowableException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -635,7 +634,7 @@ public abstract class ReflectionUtils {
for (Method ifcMethod : ifc.getMethods()) {
if (!Modifier.isAbstract(ifcMethod.getModifiers())) {
if (result == null) {
result = new LinkedList<Method>();
result = new ArrayList<Method>();
}
result.add(ifcMethod);
}

View File

@@ -45,6 +45,7 @@ public class ObjectUtilsTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void isCheckedException() {
assertTrue(ObjectUtils.isCheckedException(new Exception()));
@@ -101,8 +102,8 @@ public class ObjectUtilsTests {
assertTrue(isEmpty(new Object[0]));
assertTrue(isEmpty(new Integer[0]));
assertFalse(isEmpty(new int[] { 42 }));
assertFalse(isEmpty(new Integer[] { new Integer(42) }));
assertFalse(isEmpty(new int[] {42}));
assertFalse(isEmpty(new Integer[] {42}));
}
@Test
@@ -271,7 +272,7 @@ public class ObjectUtilsTests {
@Test
@Deprecated
public void hashCodeWithLong() {
long lng = 883l;
long lng = 883L;
int expected = (new Long(lng)).hashCode();
assertEquals(expected, ObjectUtils.hashCode(lng));
}
@@ -489,12 +490,12 @@ public class ObjectUtilsTests {
@Test
public void nullSafeHashCodeWithLongArray() {
long lng = 7993l;
long lng = 7993L;
int expected = 31 * 7 + (int) (lng ^ (lng >>> 32));
lng = 84320l;
lng = 84320L;
expected = 31 * expected + (int) (lng ^ (lng >>> 32));
long[] array = {7993l, 84320l};
long[] array = {7993L, 84320L};
int actual = ObjectUtils.nullSafeHashCode(array);
assertEquals(expected, actual);
@@ -715,7 +716,7 @@ public class ObjectUtilsTests {
@Test
public void nullSafeToStringWithLongArray() {
long[] array = {434l, 23423l};
long[] array = {434L, 23423L};
assertEquals("{434, 23423}", ObjectUtils.nullSafeToString(array));
}
@@ -737,7 +738,7 @@ public class ObjectUtilsTests {
@Test
public void nullSafeToStringWithObjectArray() {
Object[] array = {"Han", new Long(43)};
Object[] array = {"Han", Long.valueOf(43)};
assertEquals("{Han, 43}", ObjectUtils.nullSafeToString(array));
}
@@ -807,7 +808,8 @@ public class ObjectUtilsTests {
assertThat(ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "BAR"), is(Tropes.BAR));
exception.expect(IllegalArgumentException.class);
exception.expectMessage(is("constant [bogus] does not exist in enum type org.springframework.util.ObjectUtilsTests$Tropes"));
exception.expectMessage(
is("Constant [bogus] does not exist in enum type org.springframework.util.ObjectUtilsTests$Tropes"));
ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "bogus");
}
@@ -818,6 +820,6 @@ public class ObjectUtilsTests {
}
enum Tropes { FOO, BAR, baz }
enum Tropes {FOO, BAR, baz}
}