Polishing
This commit is contained in:
@@ -199,7 +199,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
|
||||
*/
|
||||
@@ -209,15 +209,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;
|
||||
}
|
||||
@@ -228,7 +227,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.
|
||||
@@ -239,9 +238,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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -20,30 +20,24 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test suite for {@link FastByteArrayOutputStream}
|
||||
* Test suite for {@link FastByteArrayOutputStream}.
|
||||
*
|
||||
* @author Craig Andrews
|
||||
*/
|
||||
public class FastByteArrayOutputStreamTests {
|
||||
|
||||
private static final int INITIAL_CAPACITY = 256;
|
||||
|
||||
private FastByteArrayOutputStream os;
|
||||
private final FastByteArrayOutputStream os = new FastByteArrayOutputStream(INITIAL_CAPACITY);;
|
||||
|
||||
private byte[] helloBytes;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.os = new FastByteArrayOutputStream(INITIAL_CAPACITY);
|
||||
this.helloBytes = "Hello World".getBytes("UTF-8");
|
||||
}
|
||||
private final byte[] helloBytes = "Hello World".getBytes(StandardCharsets.UTF_8);;
|
||||
|
||||
|
||||
@Test
|
||||
|
||||
@@ -45,6 +45,7 @@ public class ObjectUtilsTests {
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void isCheckedException() {
|
||||
assertTrue(ObjectUtils.isCheckedException(new Exception()));
|
||||
@@ -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}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user