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.
This commit is contained in:
Sam Brannen 2019-03-14 16:48:44 +01:00
parent 9cf9d0fa21
commit 9d2e7ced89
5 changed files with 10 additions and 14 deletions

View File

@ -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) {

View File

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

View File

@ -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()));
}
/**

View File

@ -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()));
}
/**

View File

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