diff --git a/spring-core/src/main/java/org/springframework/util/backoff/ExponentialBackOff.java b/spring-core/src/main/java/org/springframework/util/backoff/ExponentialBackOff.java index 5cbee849d59..5cd39685fa2 100644 --- a/spring-core/src/main/java/org/springframework/util/backoff/ExponentialBackOff.java +++ b/spring-core/src/main/java/org/springframework/util/backoff/ExponentialBackOff.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -16,6 +16,8 @@ package org.springframework.util.backoff; +import java.util.StringJoiner; + import org.springframework.util.Assert; /** @@ -212,6 +214,16 @@ public class ExponentialBackOff implements BackOff { "or equal to 1. A multiplier of 1 is equivalent to a fixed interval."); } + @Override + public String toString() { + return new StringJoiner(", ", ExponentialBackOff.class.getSimpleName() + "{", "}") + .add("initialInterval=" + this.initialInterval) + .add("multiplier=" + this.multiplier) + .add("maxInterval=" + this.maxInterval) + .add("maxElapsedTime=" + this.maxElapsedTime) + .add("maxAttempts=" + this.maxAttempts) + .toString(); + } private class ExponentialBackOffExecution implements BackOffExecution { @@ -255,11 +267,12 @@ public class ExponentialBackOff implements BackOff { @Override public String toString() { - StringBuilder sb = new StringBuilder("ExponentialBackOff{"); - sb.append("currentInterval=").append(this.currentInterval < 0 ? "n/a" : this.currentInterval + "ms"); - sb.append(", multiplier=").append(getMultiplier()); - sb.append('}'); - return sb.toString(); + String currentIntervalDescription = this.currentInterval < 0 ? "n/a" : this.currentInterval + "ms"; + return new StringJoiner(", ", ExponentialBackOffExecution.class.getSimpleName() + "{", "}") + .add("currentInterval=" + currentIntervalDescription) + .add("multiplier=" + getMultiplier()) + .add("attempts=" + this.attempts) + .toString(); } } diff --git a/spring-core/src/test/java/org/springframework/util/ExponentialBackOffTests.java b/spring-core/src/test/java/org/springframework/util/ExponentialBackOffTests.java index a539479c77c..71e5c04ef75 100644 --- a/spring-core/src/test/java/org/springframework/util/ExponentialBackOffTests.java +++ b/spring-core/src/test/java/org/springframework/util/ExponentialBackOffTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -127,14 +127,14 @@ class ExponentialBackOffTests { } @Test - void toStringContent() { + void executionToStringContent() { ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0); BackOffExecution execution = backOff.start(); - assertThat(execution.toString()).isEqualTo("ExponentialBackOff{currentInterval=n/a, multiplier=2.0}"); + assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=n/a, multiplier=2.0, attempts=0}"); execution.nextBackOff(); - assertThat(execution.toString()).isEqualTo("ExponentialBackOff{currentInterval=2000ms, multiplier=2.0}"); + assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=2000ms, multiplier=2.0, attempts=1}"); execution.nextBackOff(); - assertThat(execution.toString()).isEqualTo("ExponentialBackOff{currentInterval=4000ms, multiplier=2.0}"); + assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=4000ms, multiplier=2.0, attempts=2}"); } @Test