Avoid String allocations with Assert.isTrue()

This commit is contained in:
Sam Brannen
2022-11-05 14:40:45 +01:00
parent 902cdd1a2f
commit 5f02323b9c
27 changed files with 60 additions and 58 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-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.
@@ -31,8 +31,8 @@ import org.springframework.web.server.ServerWebExchange;
* ensure all conditions match a given request.
*
* <p>When {@code CompositeRequestCondition} instances are combined or compared
* they are expected to (a) contain the same number of conditions and (b) that
* conditions in the respective index are of the same type. It is acceptable to
* is expected that (a) they contain the same number of conditions and (b)
* conditions at the same index are of the same type. It is acceptable to
* provide {@code null} conditions or no conditions at all to the constructor.
*
* @author Rossen Stoyanchev
@@ -105,7 +105,7 @@ public class CompositeRequestCondition extends AbstractRequestCondition<Composit
/**
* If one instance is empty, return the other.
* If both instances have conditions, combine the individual conditions
* <p>If both instances have conditions, combine the individual conditions
* after ensuring they are of the same type and number.
*/
@Override
@@ -131,8 +131,8 @@ public class CompositeRequestCondition extends AbstractRequestCondition<Composit
private void assertNumberOfConditions(CompositeRequestCondition other) {
Assert.isTrue(getLength() == other.getLength(),
"Cannot combine CompositeRequestConditions with a different number of conditions. " +
ObjectUtils.nullSafeToString(this.requestConditions) + " and " +
() -> "Cannot combine CompositeRequestConditions with a different number of conditions. " +
ObjectUtils.nullSafeToString(this.requestConditions) + " and " +
ObjectUtils.nullSafeToString(other.requestConditions));
}

View File

@@ -97,7 +97,7 @@ public class RedirectView extends AbstractUrlBasedView {
* {@link HttpStatus#PERMANENT_REDIRECT}.
*/
public void setStatusCode(HttpStatus statusCode) {
Assert.isTrue(statusCode.is3xxRedirection(), "Not a redirect status code");
Assert.isTrue(statusCode.is3xxRedirection(), () -> "Not a redirect status code: " + statusCode);
this.statusCode = statusCode;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-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.
@@ -154,7 +154,7 @@ public final class CloseStatus {
* @param reason the reason
*/
public CloseStatus(int code, @Nullable String reason) {
Assert.isTrue((code >= 1000 && code < 5000), "Invalid status code");
Assert.isTrue((code >= 1000 && code < 5000), () -> "Invalid status code: " + code);
this.code = code;
this.reason = reason;
}