Skip to content

Commit 870b02b

Browse files
committed
Polish
1 parent 3da45aa commit 870b02b

File tree

7 files changed

+30
-36
lines changed

7 files changed

+30
-36
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.function.Consumer;
2727

2828
import org.springframework.boot.json.JsonWriter.WritableJson;
29+
import org.springframework.util.Assert;
2930
import org.springframework.util.ObjectUtils;
3031
import org.springframework.util.function.ThrowingConsumer;
3132

@@ -57,7 +58,6 @@ class JsonValueWriter {
5758
* @param name the name of the pair or {@code null} if only the value should be
5859
* written
5960
* @param value the value
60-
* @on IO error
6161
*/
6262
<N, V> void write(N name, V value) {
6363
if (name != null) {
@@ -81,7 +81,6 @@ <N, V> void write(N name, V value) {
8181
* All other values are written as JSON strings.
8282
* @param <V> the value type
8383
* @param value the value to write
84-
* @on IO error
8584
*/
8685
<V> void write(V value) {
8786
if (value == null) {
@@ -118,7 +117,6 @@ else if (value instanceof Boolean) {
118117
/**
119118
* Start a new {@link Series} (JSON object or array).
120119
* @param series the series to start
121-
* @on IO error
122120
* @see #end(Series)
123121
* @see #writePairs(Consumer)
124122
* @see #writeElements(Consumer)
@@ -133,7 +131,6 @@ void start(Series series) {
133131
/**
134132
* End an active {@link Series} (JSON object or array).
135133
* @param series the series type being ended (must match {@link #start(Series)})
136-
* @on IO error
137134
* @see #start(Series)
138135
*/
139136
void end(Series series) {
@@ -148,7 +145,6 @@ void end(Series series) {
148145
* @param <E> the element type
149146
* @param elements a callback that will be used to provide each element. Typically a
150147
* {@code forEach} method reference.
151-
* @on IO error
152148
* @see #writeElements(Consumer)
153149
*/
154150
<E> void writeArray(Consumer<Consumer<E>> elements) {
@@ -171,6 +167,7 @@ <E> void writeElements(Consumer<Consumer<E>> elements) {
171167

172168
<E> void writeElement(E element) {
173169
ActiveSeries activeSeries = this.activeSeries.peek();
170+
Assert.notNull(activeSeries, "No series has been started");
174171
activeSeries.appendCommaIfRequired();
175172
write(element);
176173
}
@@ -181,7 +178,6 @@ <E> void writeElement(E element) {
181178
* @param <V> the value type in the pair
182179
* @param pairs a callback that will be used to provide each pair. Typically a
183180
* {@code forEach} method reference.
184-
* @on IO error
185181
* @see #writePairs(Consumer)
186182
*/
187183
<N, V> void writeObject(Consumer<BiConsumer<N, V>> pairs) {
@@ -205,6 +201,7 @@ <N, V> void writePairs(Consumer<BiConsumer<N, V>> pairs) {
205201

206202
private <N, V> void writePair(N name, V value) {
207203
ActiveSeries activeSeries = this.activeSeries.peek();
204+
Assert.notNull(activeSeries, "No series has been started");
208205
activeSeries.appendCommaIfRequired();
209206
writeString(name);
210207
append(":");

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonWriter.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444

4545
/**
4646
* Interface that can be used to write JSON output. Typically used to generate JSON when a
47-
* a dependency on a fully marshalling library (such as Jackson or Gson) cannot be
48-
* assumed.
47+
* dependency on a fully marshalling library (such as Jackson or Gson) cannot be assumed.
4948
* <p>
5049
* For standard Java types, the {@link #standard()} factory method may be used to obtain
5150
* an instance of this interface. It supports {@link String}, {@link Number} and
@@ -143,21 +142,21 @@ default JsonWriter<T> withSuffix(String suffix) {
143142
* @return a {@link JsonWriter} instance
144143
*/
145144
static <T> JsonWriter<T> standard() {
146-
return of((members) -> members.addSelf());
145+
return of(Members::addSelf);
147146
}
148147

149148
/**
150149
* Factory method to return a {@link JsonWriter} with specific {@link Members member
151150
* mapping}. See {@link JsonValueWriter class-level javadoc} and {@link Members} for
152151
* details.
153152
* @param <T> the type to write
154-
* @param members a consumer which should configure the members
153+
* @param members a consumer, which should configure the members
155154
* @return a {@link JsonWriter} instance
156155
* @see Members
157156
*/
158157
static <T> JsonWriter<T> of(Consumer<Members<T>> members) {
159-
Members<T> initiaizedMembers = new Members<>(members, false); // Don't inline
160-
return (instance, out) -> initiaizedMembers.write(instance, new JsonValueWriter(out));
158+
Members<T> initializedMembers = new Members<>(members, false); // Don't inline
159+
return (instance, out) -> initializedMembers.write(instance, new JsonValueWriter(out));
161160
}
162161

163162
/**
@@ -202,6 +201,7 @@ default byte[] toByteArray() {
202201
* @return the JSON bytes
203202
*/
204203
default byte[] toByteArray(Charset charset) {
204+
Assert.notNull(charset, "'charset' must not be null");
205205
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
206206
toWriter(new OutputStreamWriter(out, charset));
207207
return out.toByteArray();
@@ -303,7 +303,7 @@ public String toString() {
303303

304304
/**
305305
* Callback used to configure JSON members. Individual members can be declared using
306-
* the various {@code add(...)} methods. Typically members are declared with a
306+
* the various {@code add(...)} methods. Typically, members are declared with a
307307
* {@code "name"} and a {@link Function} that will extract the value from the
308308
* instance. Members can also be declared using a static value or a {@link Supplier}.
309309
* The {@link #addSelf(String)} and {@link #addSelf()} methods may be used to access
@@ -473,7 +473,7 @@ boolean contributesPair() {
473473
}
474474

475475
/**
476-
* A member that contributes JSON. Typically a member will contribute a single
476+
* A member that contributes JSON. Typically, a member will contribute a single
477477
* name/value pair based on an extracted value. They may also contribute more complex
478478
* JSON structures when configured with one of the {@code using(...)} methods.
479479
* <p>
@@ -520,7 +520,7 @@ public Member<T> whenNotNull(Function<T, ?> extractor) {
520520
}
521521

522522
/**
523-
* Only include this member when its not {@code null} and has a
523+
* Only include this member when it is not {@code null} and has a
524524
* {@link Object#toString() toString()} that is not zero length.
525525
* @return a {@link Member} which may be configured further
526526
* @see StringUtils#hasLength(CharSequence)
@@ -530,7 +530,7 @@ public Member<T> whenHasLength() {
530530
}
531531

532532
/**
533-
* Only include this member when its not empty (See
533+
* Only include this member when it is not empty (See
534534
* {@link ObjectUtils#isEmpty(Object)} for details).
535535
* @return a {@link Member} which may be configured further
536536
*/
@@ -603,16 +603,13 @@ public <R> Member<R> as(Function<T, R> adapter) {
603603
* }
604604
* </pre>
605605
* @param <E> the element type
606-
* @param <N> the name type
607-
* @param <V> the value type
608606
* @param elements callback used to provide the elements
609607
* @param extractor a {@link PairExtractor} used to extract the name/value pair
610608
* @return a {@link Member} which may be configured further
611609
* @see #usingExtractedPairs(BiConsumer, Function, Function)
612610
* @see #usingPairs(BiConsumer)
613611
*/
614-
public <E, N, V> Member<T> usingExtractedPairs(BiConsumer<T, Consumer<E>> elements,
615-
PairExtractor<E> extractor) {
612+
public <E> Member<T> usingExtractedPairs(BiConsumer<T, Consumer<E>> elements, PairExtractor<E> extractor) {
616613
Assert.notNull(elements, "'elements' must not be null");
617614
Assert.notNull(extractor, "'extractor' must not be null");
618615
return usingExtractedPairs(elements, extractor::getName, extractor::getValue);
@@ -784,8 +781,8 @@ private Object getValueToWrite(T extracted, JsonValueWriter valueWriter) {
784781
}
785782

786783
/**
787-
* Return if this members contributes one or more name/value pairs to the JSON.
788-
* @return if a name/value pair is contributed
784+
* Whether this contributes one or more name/value pairs to the JSON.
785+
* @return whether a name/value pair is contributed
789786
*/
790787
boolean contributesPair() {
791788
return this.name != null || this.pairs != null || (this.members != null && this.members.contributesPair());

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ElasticCommonSchemaStructuredLogFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static void jsonMembers(ApplicationPid pid, ElasticCommonSchemaService s
6262
members.addSelf().whenNotNull(ILoggingEvent::getThrowableProxy).usingMembers((throwableMembers) -> {
6363
throwableMembers.add("error.type", ILoggingEvent::getThrowableProxy).as(IThrowableProxy::getClassName);
6464
throwableMembers.add("error.message", ILoggingEvent::getThrowableProxy).as(IThrowableProxy::getMessage);
65-
throwableMembers.add("error.stack_trace", (event) -> throwableProxyConverter.convert(event));
65+
throwableMembers.add("error.stack_trace", throwableProxyConverter::convert);
6666
});
6767
members.add("ecs.version", "8.11");
6868
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/JsonWriterStructuredLogFormatter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public abstract class JsonWriterStructuredLogFormatter<E> implements StructuredL
3737
/**
3838
* Create a new {@link JsonWriterStructuredLogFormatter} instance with the given
3939
* members.
40-
* @param members a consumer which should configure the members
40+
* @param members a consumer, which should configure the members
4141
*/
4242
protected JsonWriterStructuredLogFormatter(Consumer<Members<E>> members) {
4343
this(JsonWriter.of(members).withNewLineAtEnd());
@@ -59,7 +59,7 @@ public String format(E event) {
5959

6060
@Override
6161
public byte[] formatAsBytes(E event, Charset charset) {
62-
return this.jsonWriter.write(event).toByteArray();
62+
return this.jsonWriter.write(event).toByteArray(charset);
6363
}
6464

6565
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLogFormatterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*/
4343
public class StructuredLogFormatterFactory<E> {
4444

45-
private static FailureHandler failureHandler = (type, implementationName, failure) -> {
45+
private static final FailureHandler failureHandler = (type, implementationName, failure) -> {
4646
if (!(failure instanceof ClassNotFoundException)) {
4747
throw new IllegalArgumentException(
4848
"Unable to instantiate " + implementationName + " [" + type.getName() + "]", failure);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPid.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,7 @@ protected ApplicationPid(Long pid) {
5050

5151
private Long currentProcessPid() {
5252
try {
53-
return Long.valueOf(ProcessHandle.current().pid());
53+
return ProcessHandle.current().pid();
5454
}
5555
catch (Throwable ex) {
5656
return null;

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/JsonWriterTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,25 @@ void ofAddingNamedSupplier() {
109109
}
110110

111111
@Test
112-
void ofAddingUnamedSelf() {
112+
void ofAddingUnnamedSelf() {
113113
JsonWriter<Person> writer = JsonWriter.of((members) -> members.addSelf());
114114
assertThat(writer.writeToString(PERSON)).isEqualTo(quoted("Spring Boot (10)"));
115115
}
116116

117117
@Test
118-
void ofAddingUnamedValue() {
118+
void ofAddingUnnamedValue() {
119119
JsonWriter<Person> writer = JsonWriter.of((members) -> members.add("Boot"));
120120
assertThat(writer.writeToString(PERSON)).isEqualTo(quoted("Boot"));
121121
}
122122

123123
@Test
124-
void ofAddingUnamedSupplier() {
124+
void ofAddingUnnamedSupplier() {
125125
JsonWriter<Person> writer = JsonWriter.of((members) -> members.add(() -> "Boot"));
126126
assertThat(writer.writeToString(PERSON)).isEqualTo(quoted("Boot"));
127127
}
128128

129129
@Test
130-
void ofAddingUnamedExtractor() {
130+
void ofAddingUnnamedExtractor() {
131131
JsonWriter<Person> writer = JsonWriter.of((members) -> members.add(Person::lastName));
132132
assertThat(writer.writeToString(PERSON)).isEqualTo(quoted("Boot"));
133133
}
@@ -162,7 +162,7 @@ void ofWhenNoMembersAddedThrowsException() {
162162
}
163163

164164
@Test
165-
void ofWhenOneContibutesPairByNameAndOneHasNoNameThrowsException() {
165+
void ofWhenOneContributesPairByNameAndOneHasNoNameThrowsException() {
166166
assertThatIllegalStateException().isThrownBy(() -> JsonWriter.of((members) -> {
167167
members.add("Spring", "Boot");
168168
members.add("alone");
@@ -172,7 +172,7 @@ void ofWhenOneContibutesPairByNameAndOneHasNoNameThrowsException() {
172172
}
173173

174174
@Test
175-
void ofWhenOneContibutesPairByUsingPairsAndOneHasNoNameThrowsException() {
175+
void ofWhenOneContributesPairByUsingPairsAndOneHasNoNameThrowsException() {
176176
assertThatIllegalStateException().isThrownBy(() -> JsonWriter.of((members) -> {
177177
members.add(Map.of("Spring", "Boot")).usingPairs(Map::forEach);
178178
members.add("alone");
@@ -182,7 +182,7 @@ void ofWhenOneContibutesPairByUsingPairsAndOneHasNoNameThrowsException() {
182182
}
183183

184184
@Test
185-
void ofWhenOneContibutesPairByUsingMembersAndOneHasNoNameThrowsException() {
185+
void ofWhenOneContributesPairByUsingMembersAndOneHasNoNameThrowsException() {
186186
assertThatIllegalStateException().isThrownBy(() -> JsonWriter.of((members) -> {
187187
members.add(PERSON).usingMembers((personMembers) -> {
188188
personMembers.add("first", Person::firstName);
@@ -260,7 +260,7 @@ void whenHasLength() {
260260
void whenHasLengthOnNonString() {
261261
JsonWriter<StringBuilder> writer = JsonWriter.of((members) -> members.addSelf().whenHasLength());
262262
assertThat(writer.writeToString(new StringBuilder("test"))).isEqualTo(quoted("test"));
263-
assertThat(writer.writeToString(new StringBuilder(""))).isEmpty();
263+
assertThat(writer.writeToString(new StringBuilder())).isEmpty();
264264
assertThat(writer.writeToString(null)).isEmpty();
265265
}
266266

0 commit comments

Comments
 (0)