Skip to content

Commit

Permalink
Polish contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jul 24, 2019
1 parent 1d6e44a commit a3c7ae2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -40,12 +40,13 @@
*/
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";


Expand Down Expand Up @@ -83,7 +84,7 @@ else if (value.getClass().isArray()) {

private <K, V> String style(Map<K, V> value) {
if (value.isEmpty()) {
return MAP + '[' + EMPTY + ']';
return EMPTY_MAP;
}

StringJoiner result = new StringJoiner(", ", "[", "]");
Expand All @@ -101,7 +102,7 @@ private String style(Collection<?> value) {
String collectionType = getCollectionTypeString(value);

if (value.isEmpty()) {
return collectionType + '[' + EMPTY + ']';
return collectionType + EMPTY;
}

StringJoiner result = new StringJoiner(", ", "[", "]");
Expand All @@ -125,7 +126,7 @@ else if (value instanceof Set) {

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(", ", "[", "]");
Expand Down
54 changes: 27 additions & 27 deletions spring-core/src/main/java/org/springframework/util/ObjectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,11 @@ public static String nullSafeToString(@Nullable Object[] array) {
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();
}

/**
Expand All @@ -734,11 +734,11 @@ public static String nullSafeToString(@Nullable boolean[] array) {
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();
}

/**
Expand All @@ -758,11 +758,11 @@ public static String nullSafeToString(@Nullable byte[] array) {
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();
}

/**
Expand All @@ -782,11 +782,11 @@ public static String nullSafeToString(@Nullable char[] array) {
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();
}

/**
Expand All @@ -806,11 +806,11 @@ public static String nullSafeToString(@Nullable double[] array) {
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();
}

/**
Expand All @@ -830,11 +830,11 @@ public static String nullSafeToString(@Nullable float[] array) {
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();
}

/**
Expand All @@ -854,11 +854,11 @@ public static String nullSafeToString(@Nullable int[] array) {
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();
}

/**
Expand All @@ -878,11 +878,11 @@ public static String nullSafeToString(@Nullable long[] array) {
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();
}

/**
Expand All @@ -902,11 +902,11 @@ public static String nullSafeToString(@Nullable short[] array) {
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();
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,32 +24,37 @@
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]]");

Expand All @@ -49,19 +68,19 @@ public void style_map() {
}

@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]]");

Expand All @@ -73,27 +92,27 @@ public void style_collection() {
}

@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']");
}

}
}

0 comments on commit a3c7ae2

Please sign in to comment.