Refine compareTo for param and header conditions

Issue: SPR-16674
This commit is contained in:
Rossen Stoyanchev
2018-05-08 17:36:20 -04:00
parent c238fb441b
commit 1aadf2c3a6
8 changed files with 185 additions and 71 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -122,19 +122,27 @@ public final class HeadersRequestCondition extends AbstractRequestCondition<Head
}
/**
* Returns:
* <ul>
* <li>0 if the two conditions have the same number of header expressions
* <li>Less than 0 if "this" instance has more header expressions
* <li>Greater than 0 if the "other" instance has more header expressions
* </ul>
* Compare to another condition based on header expressions. A condition
* is considered to be a more specific match, if it has:
* <ol>
* <li>A greater number of expressions.
* <li>A greater number of non-negated expressions with a concrete value.
* </ol>
* <p>It is assumed that both instances have been obtained via
* {@link #getMatchingCondition(HttpServletRequest)} and each instance
* contains the matching header expression only or is otherwise empty.
*/
@Override
public int compareTo(HeadersRequestCondition other, HttpServletRequest request) {
return other.expressions.size() - this.expressions.size();
int result = other.expressions.size() - this.expressions.size();
if (result != 0) {
return result;
}
return (int) (getValueMatchCount(other.expressions) - getValueMatchCount(this.expressions));
}
private long getValueMatchCount(Set<HeaderExpression> expressions) {
return expressions.stream().filter(e -> e.getValue() != null && !e.isNegated()).count();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -107,19 +107,27 @@ public final class ParamsRequestCondition extends AbstractRequestCondition<Param
}
/**
* Returns:
* <ul>
* <li>0 if the two conditions have the same number of parameter expressions
* <li>Less than 0 if "this" instance has more parameter expressions
* <li>Greater than 0 if the "other" instance has more parameter expressions
* </ul>
* Compare to another condition based on parameter expressions. A condition
* is considered to be a more specific match, if it has:
* <ol>
* <li>A greater number of expressions.
* <li>A greater number of non-negated expressions with a concrete value.
* </ol>
* <p>It is assumed that both instances have been obtained via
* {@link #getMatchingCondition(HttpServletRequest)} and each instance
* contains the matching parameter expressions only or is otherwise empty.
*/
@Override
public int compareTo(ParamsRequestCondition other, HttpServletRequest request) {
return (other.expressions.size() - this.expressions.size());
int result = other.expressions.size() - this.expressions.size();
if (result != 0) {
return result;
}
return (int) (getValueMatchCount(other.expressions) - getValueMatchCount(this.expressions));
}
private long getValueMatchCount(Set<ParamExpression> expressions) {
return expressions.stream().filter(e -> e.getValue() != null && !e.isNegated()).count();
}