Browse Source

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.
pull/22597/head
Sam Brannen 6 years ago
parent
commit
9d2e7ced89
  1. 4
      spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java
  2. 5
      spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java
  3. 7
      spring-test/src/main/java/org/springframework/test/annotation/TestAnnotationUtils.java
  4. 4
      spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java
  5. 4
      spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java

4
spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

@ -288,7 +288,7 @@ class ConstructorResolver { @@ -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 { @@ -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) {

5
spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java

@ -1,5 +1,5 @@ @@ -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 @@ -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);
}
}

7
spring-test/src/main/java/org/springframework/test/annotation/TestAnnotationUtils.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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()));
}
/**

4
spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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()));
}
/**

4
spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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);

Loading…
Cancel
Save