From 9d2e7ced89588ab05863f123e1b2b8de7dc21057 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 14 Mar 2019 16:48:44 +0100 Subject: [PATCH] Favor Math.[min|max]() over handcrafted code In line with the general trend toward favoring core JDK APIs for common tasks in Spring Framework 5.2, this commit replaces handcrafted statements with Math.min() and Math.max() were applicable. --- .../beans/factory/support/ConstructorResolver.java | 4 ++-- .../jms/listener/DefaultMessageListenerContainer.java | 5 ++--- .../test/annotation/TestAnnotationUtils.java | 7 ++----- .../test/context/junit4/SpringJUnit4ClassRunner.java | 4 ++-- .../web/multipart/commons/CommonsMultipartFile.java | 4 ++-- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java index 4665615a188..ed9c16bebd8 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java @@ -288,7 +288,7 @@ class ConstructorResolver { } private Object instantiate( - String beanName, RootBeanDefinition mbd, Constructor constructorToUse, Object[] argsToUse) { + String beanName, RootBeanDefinition mbd, Constructor constructorToUse, Object[] argsToUse) { try { InstantiationStrategy strategy = this.beanFactory.getInstantiationStrategy(); @@ -933,7 +933,7 @@ class ConstructorResolver { // Decrease raw weight by 1024 to prefer it over equal converted weight. int typeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.arguments); int rawTypeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.rawArguments) - 1024; - return (rawTypeDiffWeight < typeDiffWeight ? rawTypeDiffWeight : typeDiffWeight); + return Math.min(rawTypeDiffWeight, typeDiffWeight); } public int getAssignabilityWeight(Class[] paramTypes) { diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java b/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java index 8b58ceac819..b9532b04a8e 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 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. @@ -383,8 +383,7 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe public void setMaxConcurrentConsumers(int maxConcurrentConsumers) { Assert.isTrue(maxConcurrentConsumers > 0, "'maxConcurrentConsumers' value must be at least 1 (one)"); synchronized (this.lifecycleMonitor) { - this.maxConcurrentConsumers = - (maxConcurrentConsumers > this.concurrentConsumers ? maxConcurrentConsumers : this.concurrentConsumers); + this.maxConcurrentConsumers = Math.max(maxConcurrentConsumers, this.concurrentConsumers); } } diff --git a/spring-test/src/main/java/org/springframework/test/annotation/TestAnnotationUtils.java b/spring-test/src/main/java/org/springframework/test/annotation/TestAnnotationUtils.java index f15b06cb3bc..dd0c41f59be 100644 --- a/spring-test/src/main/java/org/springframework/test/annotation/TestAnnotationUtils.java +++ b/spring-test/src/main/java/org/springframework/test/annotation/TestAnnotationUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 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. @@ -37,10 +37,7 @@ public abstract class TestAnnotationUtils { */ public static long getTimeout(Method method) { Timed timed = AnnotatedElementUtils.findMergedAnnotation(method, Timed.class); - if (timed == null) { - return 0; - } - return Math.max(0, timed.millis()); + return (timed == null ? 0 : Math.max(0, timed.millis())); } /** diff --git a/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java b/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java index 0294700ac24..f7031e9e5c1 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 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. @@ -400,7 +400,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { */ protected long getJUnitTimeout(FrameworkMethod frameworkMethod) { Test test = frameworkMethod.getAnnotation(Test.class); - return (test != null && test.timeout() > 0 ? test.timeout() : 0); + return (test == null ? 0 : Math.max(0, test.timeout())); } /** diff --git a/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java b/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java index 69a1923832e..05bfeddcbc2 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 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. @@ -109,7 +109,7 @@ public class CommonsMultipartFile implements MultipartFile, Serializable { // Check for Windows-style path int winSep = filename.lastIndexOf('\\'); // Cut off at latest possible point - int pos = (winSep > unixSep ? winSep : unixSep); + int pos = Math.max(winSep, unixSep); if (pos != -1) { // Any sort of path separator found... return filename.substring(pos + 1);