Skip to content

Commit

Permalink
Favor Math.[min|max]() over handcrafted code
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sbrannen committed Mar 14, 2019
1 parent 9cf9d0f commit 9d2e7ce
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution())
}

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();
Expand Down Expand Up @@ -933,7 +933,7 @@ public int getTypeDifferenceWeight(Class<?>[] paramTypes) {
// 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -383,8 +383,7 @@ public final int getConcurrentConsumers() {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -400,7 +400,7 @@ else if (junitTimeout > 0) {
*/
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()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -109,7 +109,7 @@ public String getOriginalFilename() {
// 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);
Expand Down

0 comments on commit 9d2e7ce

Please sign in to comment.