From 95183d7a37ee8522f728fbcf27293da381f70913 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 15 Apr 2022 16:46:55 +0200 Subject: [PATCH] Use modern java constructs --- .../classify/PatternMatcher.java | 2 +- .../RecoverAnnotationRecoveryHandler.java | 5 +---- .../RetryOperationsInterceptor.java | 22 +++++++++---------- .../StatefulRetryOperationsInterceptor.java | 7 ++---- .../retry/backoff/DummySleeper.java | 3 +-- .../ExponentialBackOffPolicyTests.java | 14 ++++++------ .../RetryOperationsInterceptorTests.java | 2 +- ...odInvocationRetryListenerSupportTests.java | 19 +++++++++------- .../support/StatefulRecoveryRetryTests.java | 2 +- 9 files changed, 36 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/springframework/classify/PatternMatcher.java b/src/main/java/org/springframework/classify/PatternMatcher.java index 45f140e..0efe91f 100644 --- a/src/main/java/org/springframework/classify/PatternMatcher.java +++ b/src/main/java/org/springframework/classify/PatternMatcher.java @@ -44,7 +44,7 @@ public class PatternMatcher { this.map = map; // Sort keys to start with the most specific this.sorted = new ArrayList<>(map.keySet()); - Collections.sort(this.sorted, (o1, o2) -> { + this.sorted.sort((o1, o2) -> { String s1 = o1; // .replace('?', '{'); String s2 = o2; // .replace('*', '}'); return s2.compareTo(s1); diff --git a/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java b/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java index 0f0f401..9c45616 100644 --- a/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java +++ b/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java @@ -112,10 +112,7 @@ public class RecoverAnnotationRecoveryHandler implements MethodInvocationReco try { return proxy.getClass().getMethod(method.getName(), method.getParameterTypes()); } - catch (NoSuchMethodException e) { - return null; - } - catch (SecurityException e) { + catch (NoSuchMethodException | SecurityException e) { return null; } } diff --git a/src/main/java/org/springframework/retry/interceptor/RetryOperationsInterceptor.java b/src/main/java/org/springframework/retry/interceptor/RetryOperationsInterceptor.java index 80771bd..3824717 100644 --- a/src/main/java/org/springframework/retry/interceptor/RetryOperationsInterceptor.java +++ b/src/main/java/org/springframework/retry/interceptor/RetryOperationsInterceptor.java @@ -1,14 +1,17 @@ /* - * Copyright 2006-2007 the original author or authors. + * Copyright 2006-2022 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 + * 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 + * 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. + * 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. */ package org.springframework.retry.interceptor; @@ -96,10 +99,7 @@ public class RetryOperationsInterceptor implements MethodInterceptor { try { return ((ProxyMethodInvocation) this.invocation).invocableClone().proceed(); } - catch (Exception e) { - throw e; - } - catch (Error e) { + catch (Exception | Error e) { throw e; } catch (Throwable e) { diff --git a/src/main/java/org/springframework/retry/interceptor/StatefulRetryOperationsInterceptor.java b/src/main/java/org/springframework/retry/interceptor/StatefulRetryOperationsInterceptor.java index f769742..6844eda 100644 --- a/src/main/java/org/springframework/retry/interceptor/StatefulRetryOperationsInterceptor.java +++ b/src/main/java/org/springframework/retry/interceptor/StatefulRetryOperationsInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2019 the original author or authors. + * Copyright 2006-2022 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. @@ -208,10 +208,7 @@ public class StatefulRetryOperationsInterceptor implements MethodInterceptor { try { return this.invocation.proceed(); } - catch (Exception e) { - throw e; - } - catch (Error e) { + catch (Exception | Error e) { throw e; } catch (Throwable e) { diff --git a/src/test/java/org/springframework/retry/backoff/DummySleeper.java b/src/test/java/org/springframework/retry/backoff/DummySleeper.java index e4f645c..0fd7baf 100644 --- a/src/test/java/org/springframework/retry/backoff/DummySleeper.java +++ b/src/test/java/org/springframework/retry/backoff/DummySleeper.java @@ -41,8 +41,7 @@ public class DummySleeper implements Sleeper { public long[] getBackOffs() { long[] result = new long[backOffs.size()]; int i = 0; - for (Iterator iterator = backOffs.iterator(); iterator.hasNext();) { - Long value = iterator.next(); + for (Long value : backOffs) { result[i++] = value; } return result; diff --git a/src/test/java/org/springframework/retry/backoff/ExponentialBackOffPolicyTests.java b/src/test/java/org/springframework/retry/backoff/ExponentialBackOffPolicyTests.java index faba542..7267c52 100644 --- a/src/test/java/org/springframework/retry/backoff/ExponentialBackOffPolicyTests.java +++ b/src/test/java/org/springframework/retry/backoff/ExponentialBackOffPolicyTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2007 the original author or authors. + * Copyright 2006-2022 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. @@ -33,28 +33,28 @@ public class ExponentialBackOffPolicyTests { public void testSetMaxInterval() throws Exception { ExponentialBackOffPolicy strategy = new ExponentialBackOffPolicy(); strategy.setMaxInterval(1000); - assertTrue(strategy.toString().indexOf("maxInterval=1000") >= 0); + assertTrue(strategy.toString().contains("maxInterval=1000")); strategy.setMaxInterval(0); // The minimum value for the max interval is 1 - assertTrue(strategy.toString().indexOf("maxInterval=1") >= 0); + assertTrue(strategy.toString().contains("maxInterval=1")); } @Test public void testSetInitialInterval() throws Exception { ExponentialBackOffPolicy strategy = new ExponentialBackOffPolicy(); strategy.setInitialInterval(10000); - assertTrue(strategy.toString().indexOf("initialInterval=10000,") >= 0); + assertTrue(strategy.toString().contains("initialInterval=10000,")); strategy.setInitialInterval(0); - assertTrue(strategy.toString().indexOf("initialInterval=1,") >= 0); + assertTrue(strategy.toString().contains("initialInterval=1,")); } @Test public void testSetMultiplier() throws Exception { ExponentialBackOffPolicy strategy = new ExponentialBackOffPolicy(); strategy.setMultiplier(3.); - assertTrue(strategy.toString().indexOf("multiplier=3.") >= 0); + assertTrue(strategy.toString().contains("multiplier=3.")); strategy.setMultiplier(.5); - assertTrue(strategy.toString().indexOf("multiplier=1.") >= 0); + assertTrue(strategy.toString().contains("multiplier=1.")); } @Test diff --git a/src/test/java/org/springframework/retry/interceptor/RetryOperationsInterceptorTests.java b/src/test/java/org/springframework/retry/interceptor/RetryOperationsInterceptorTests.java index 1589957..62d6e32 100644 --- a/src/test/java/org/springframework/retry/interceptor/RetryOperationsInterceptorTests.java +++ b/src/test/java/org/springframework/retry/interceptor/RetryOperationsInterceptorTests.java @@ -247,7 +247,7 @@ public class RetryOperationsInterceptorTests { } catch (IllegalStateException e) { assertTrue("Exception message should contain MethodInvocation: " + e.getMessage(), - e.getMessage().indexOf("MethodInvocation") >= 0); + e.getMessage().contains("MethodInvocation")); } } diff --git a/src/test/java/org/springframework/retry/listener/MethodInvocationRetryListenerSupportTests.java b/src/test/java/org/springframework/retry/listener/MethodInvocationRetryListenerSupportTests.java index 8f36e62..18e49aa 100644 --- a/src/test/java/org/springframework/retry/listener/MethodInvocationRetryListenerSupportTests.java +++ b/src/test/java/org/springframework/retry/listener/MethodInvocationRetryListenerSupportTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2019 the original author or authors. + * Copyright 2006-2022 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. @@ -17,7 +17,9 @@ package org.springframework.retry.listener; import java.util.concurrent.atomic.AtomicInteger; + import org.junit.Test; + import org.springframework.retry.RetryCallback; import org.springframework.retry.RetryContext; import org.springframework.retry.interceptor.MethodInvocationRetryCallback; @@ -51,8 +53,7 @@ public class MethodInvocationRetryListenerSupportTests { } }; RetryContext context = mock(RetryContext.class); - MethodInvocationRetryCallback callback = mock(MethodInvocationRetryCallback.class); - support.close(context, callback, null); + support.close(context, mockMethodInvocationRetryCallback(), null); assertEquals(1, callsOnDoCloseMethod.get()); } @@ -68,7 +69,7 @@ public class MethodInvocationRetryListenerSupportTests { } }; RetryContext context = mock(RetryContext.class); - RetryCallback callback = mock(RetryCallback.class); + RetryCallback callback = mock(RetryCallback.class); support.close(context, callback, null); assertEquals(0, callsOnDoCloseMethod.get()); @@ -96,8 +97,7 @@ public class MethodInvocationRetryListenerSupportTests { } }; RetryContext context = mock(RetryContext.class); - MethodInvocationRetryCallback callback = mock(MethodInvocationRetryCallback.class); - support.onError(context, callback, null); + support.onError(context, mockMethodInvocationRetryCallback(), null); assertEquals(1, callsOnDoOnErrorMethod.get()); } @@ -120,10 +120,13 @@ public class MethodInvocationRetryListenerSupportTests { } }; RetryContext context = mock(RetryContext.class); - MethodInvocationRetryCallback callback = mock(MethodInvocationRetryCallback.class); - assertTrue(support.open(context, callback)); + assertTrue(support.open(context, mockMethodInvocationRetryCallback())); assertEquals(1, callsOnDoOpenMethod.get()); } + private MethodInvocationRetryCallback mockMethodInvocationRetryCallback() { + return mock(MethodInvocationRetryCallback.class); + } + } diff --git a/src/test/java/org/springframework/retry/support/StatefulRecoveryRetryTests.java b/src/test/java/org/springframework/retry/support/StatefulRecoveryRetryTests.java index 5cc14eb..7d9e6b9 100644 --- a/src/test/java/org/springframework/retry/support/StatefulRecoveryRetryTests.java +++ b/src/test/java/org/springframework/retry/support/StatefulRecoveryRetryTests.java @@ -234,7 +234,7 @@ public class StatefulRecoveryRetryTests { } catch (RetryException e) { String message = e.getMessage(); - assertTrue("Message does not contain 'capacity': " + message, message.indexOf("capacity") >= 0); + assertTrue("Message does not contain 'capacity': " + message, message.contains("capacity")); } }