Polish contribution

See gh-23300
master
Sam Brannen 5 years ago
parent 1d6e44ab04
commit a3c7ae2c58
  1. 11
      spring-core/src/main/java/org/springframework/core/style/DefaultValueStyler.java
  2. 54
      spring-core/src/main/java/org/springframework/util/ObjectUtils.java
  3. 63
      spring-core/src/test/java/org/springframework/core/style/DefaultValueStylerTests.java

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2019 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.
@ -40,12 +40,13 @@ import org.springframework.util.ObjectUtils;
*/
public class DefaultValueStyler implements ValueStyler {
private static final String EMPTY = "[empty]";
private static final String EMPTY = "[[empty]]";
private static final String NULL = "[null]";
private static final String COLLECTION = "collection";
private static final String SET = "set";
private static final String LIST = "list";
private static final String MAP = "map";
private static final String EMPTY_MAP = MAP + EMPTY;
private static final String ARRAY = "array";
@ -83,7 +84,7 @@ public class DefaultValueStyler implements ValueStyler {
private <K, V> String style(Map<K, V> value) {
if (value.isEmpty()) {
return MAP + '[' + EMPTY + ']';
return EMPTY_MAP;
}
StringJoiner result = new StringJoiner(", ", "[", "]");
@ -101,7 +102,7 @@ public class DefaultValueStyler implements ValueStyler {
String collectionType = getCollectionTypeString(value);
if (value.isEmpty()) {
return collectionType + '[' + EMPTY + ']';
return collectionType + EMPTY;
}
StringJoiner result = new StringJoiner(", ", "[", "]");
@ -125,7 +126,7 @@ public class DefaultValueStyler implements ValueStyler {
private String styleArray(Object[] array) {
if (array.length == 0) {
return ARRAY + '<' + ClassUtils.getShortName(array.getClass().getComponentType()) + '>' + '[' + EMPTY + ']';
return ARRAY + '<' + ClassUtils.getShortName(array.getClass().getComponentType()) + '>' + EMPTY;
}
StringJoiner result = new StringJoiner(", ", "[", "]");

@ -710,11 +710,11 @@ public abstract class ObjectUtils {
if (length == 0) {
return EMPTY_ARRAY;
}
StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
for (Object o : array) {
sj.add(String.valueOf(o));
stringJoiner.add(String.valueOf(o));
}
return sj.toString();
return stringJoiner.toString();
}
/**
@ -734,11 +734,11 @@ public abstract class ObjectUtils {
if (length == 0) {
return EMPTY_ARRAY;
}
StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
for (boolean b : array) {
sj.add(String.valueOf(b));
stringJoiner.add(String.valueOf(b));
}
return sj.toString();
return stringJoiner.toString();
}
/**
@ -758,11 +758,11 @@ public abstract class ObjectUtils {
if (length == 0) {
return EMPTY_ARRAY;
}
StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
for (byte b : array) {
sj.add(String.valueOf(b));
stringJoiner.add(String.valueOf(b));
}
return sj.toString();
return stringJoiner.toString();
}
/**
@ -782,11 +782,11 @@ public abstract class ObjectUtils {
if (length == 0) {
return EMPTY_ARRAY;
}
StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
for (char c : array) {
sj.add('\'' + String.valueOf(c) + '\'');
stringJoiner.add('\'' + String.valueOf(c) + '\'');
}
return sj.toString();
return stringJoiner.toString();
}
/**
@ -806,11 +806,11 @@ public abstract class ObjectUtils {
if (length == 0) {
return EMPTY_ARRAY;
}
StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
for (double d : array) {
sj.add(String.valueOf(d));
stringJoiner.add(String.valueOf(d));
}
return sj.toString();
return stringJoiner.toString();
}
/**
@ -830,11 +830,11 @@ public abstract class ObjectUtils {
if (length == 0) {
return EMPTY_ARRAY;
}
StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
for (float f : array) {
sj.add(String.valueOf(f));
stringJoiner.add(String.valueOf(f));
}
return sj.toString();
return stringJoiner.toString();
}
/**
@ -854,11 +854,11 @@ public abstract class ObjectUtils {
if (length == 0) {
return EMPTY_ARRAY;
}
StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
for (int i : array) {
sj.add(String.valueOf(i));
stringJoiner.add(String.valueOf(i));
}
return sj.toString();
return stringJoiner.toString();
}
/**
@ -878,11 +878,11 @@ public abstract class ObjectUtils {
if (length == 0) {
return EMPTY_ARRAY;
}
StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
for (long l : array) {
sj.add(String.valueOf(l));
stringJoiner.add(String.valueOf(l));
}
return sj.toString();
return stringJoiner.toString();
}
/**
@ -902,11 +902,11 @@ public abstract class ObjectUtils {
if (length == 0) {
return EMPTY_ARRAY;
}
StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
for (short s : array) {
sj.add(String.valueOf(s));
stringJoiner.add(String.valueOf(s));
}
return sj.toString();
return stringJoiner.toString();
}
}

@ -1,6 +1,20 @@
package org.springframework.core.style;
/*
* Copyright 2002-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.junit.Test;
package org.springframework.core.style;
import java.util.Arrays;
import java.util.Collections;
@ -10,32 +24,37 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class DefaultValueStylerTest {
/**
* Unit tests for {@link DefaultValueStyler}.
*
* @since 5.2
*/
public class DefaultValueStylerTests {
private final DefaultValueStyler styler = new DefaultValueStyler();
private DefaultValueStyler styler = new DefaultValueStyler();
@Test
public void style() throws NoSuchMethodException {
public void styleBasics() throws NoSuchMethodException {
assertThat(styler.style(null)).isEqualTo("[null]");
assertThat(styler.style("str")).isEqualTo("'str'");
assertThat(styler.style(String.class)).isEqualTo("String");
assertThat(styler.style(String.class.getMethod("toString"))).isEqualTo("toString@String");
}
@Test
public void style_plainObject() {
final Object obj = new Object();
public void stylePlainObject() {
Object obj = new Object();
assertThat(styler.style(obj)).isEqualTo(String.valueOf(obj));
}
@Test
public void style_map() {
public void styleMaps() {
Map<String, Integer> map = Collections.emptyMap();
assertThat(styler.style(map)).isEqualTo("map[[empty]]");
@ -49,19 +68,19 @@ public class DefaultValueStylerTest {
}
@Test
public void style_entry() {
final Map<String, Integer> map = new LinkedHashMap<>();
public void styleMapEntries() {
Map<String, Integer> map = new LinkedHashMap<>();
map.put("key1", 1);
map.put("key2", 2);
final Iterator<Map.Entry<String, Integer>> entries = map.entrySet().iterator();
Iterator<Map.Entry<String, Integer>> entries = map.entrySet().iterator();
assertThat(styler.style(entries.next())).isEqualTo("'key1' -> 1");
assertThat(styler.style(entries.next())).isEqualTo("'key2' -> 2");
}
@Test
public void style_collection() {
public void styleCollections() {
List<Integer> list = Collections.emptyList();
assertThat(styler.style(list)).isEqualTo("list[[empty]]");
@ -73,27 +92,27 @@ public class DefaultValueStylerTest {
}
@Test
public void style_primitiveArray() {
public void stylePrimitiveArrays() {
int[] array = new int[0];
assertThat(styler.style(array)).isEqualTo("array<Object>[[empty]]");
array = new int[]{1};
array = new int[] { 1 };
assertThat(styler.style(array)).isEqualTo("array<Integer>[1]");
array = new int[]{1, 2};
array = new int[] { 1, 2 };
assertThat(styler.style(array)).isEqualTo("array<Integer>[1, 2]");
}
@Test
public void style_objectArray() {
public void styleObjectArrays() {
String[] array = new String[0];
assertThat(styler.style(array)).isEqualTo("array<String>[[empty]]");
array = new String[]{"str1"};
array = new String[] { "str1" };
assertThat(styler.style(array)).isEqualTo("array<String>['str1']");
array = new String[]{"str1", "str2"};
array = new String[] { "str1", "str2" };
assertThat(styler.style(array)).isEqualTo("array<String>['str1', 'str2']");
}
}
}
Loading…
Cancel
Save