Skip to content

Commit

Permalink
Pre-calculated RequestMappingInfo hashcode
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Jun 3, 2020
1 parent 1e25556 commit 8dc8d88
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping

private final RequestConditionHolder customConditionHolder;

private final int hashCode;


public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondition patterns,
@Nullable RequestMethodsRequestCondition methods, @Nullable ParamsRequestCondition params,
Expand All @@ -101,6 +103,10 @@ public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondit
this.consumesCondition = (consumes != null ? consumes : EMPTY_CONSUMES);
this.producesCondition = (produces != null ? produces : EMPTY_PRODUCES);
this.customConditionHolder = (custom != null ? new RequestConditionHolder(custom) : EMPTY_CUSTOM);

this.hashCode = calculateHashCode(
this.patternsCondition, this.methodsCondition, this.paramsCondition, this.headersCondition,
this.consumesCondition, this.producesCondition, this.customConditionHolder);
}

/**
Expand Down Expand Up @@ -323,10 +329,17 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
return (this.patternsCondition.hashCode() * 31 + // primary differentiation
this.methodsCondition.hashCode() + this.paramsCondition.hashCode() +
this.headersCondition.hashCode() + this.consumesCondition.hashCode() +
this.producesCondition.hashCode() + this.customConditionHolder.hashCode());
return this.hashCode;
}

private static int calculateHashCode(
PatternsRequestCondition patterns, RequestMethodsRequestCondition methods,
ParamsRequestCondition params, HeadersRequestCondition headers,
ConsumesRequestCondition consumes, ProducesRequestCondition produces,
RequestConditionHolder custom) {

return patterns.hashCode() * 31 + methods.hashCode() + params.hashCode() +
headers.hashCode() + consumes.hashCode() + produces.hashCode() + custom.hashCode();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping
private static final RequestConditionHolder EMPTY_CUSTOM = new RequestConditionHolder(null);



@Nullable
private final String name;

Expand All @@ -90,6 +89,8 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping

private final RequestConditionHolder customConditionHolder;

private final int hashCode;


public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondition patterns,
@Nullable RequestMethodsRequestCondition methods, @Nullable ParamsRequestCondition params,
Expand All @@ -104,6 +105,10 @@ public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondit
this.consumesCondition = (consumes != null ? consumes : EMPTY_CONSUMES);
this.producesCondition = (produces != null ? produces : EMPTY_PRODUCES);
this.customConditionHolder = (custom != null ? new RequestConditionHolder(custom) : EMPTY_CUSTOM);

this.hashCode = calculateHashCode(
this.patternsCondition, this.methodsCondition, this.paramsCondition, this.headersCondition,
this.consumesCondition, this.producesCondition, this.customConditionHolder);
}

/**
Expand All @@ -125,7 +130,6 @@ public RequestMappingInfo(RequestMappingInfo info, @Nullable RequestCondition<?>
info.consumesCondition, info.producesCondition, customRequestCondition);
}


/**
* Return the name for this mapping, or {@code null}.
*/
Expand Down Expand Up @@ -336,10 +340,17 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
return (this.patternsCondition.hashCode() * 31 + // primary differentiation
this.methodsCondition.hashCode() + this.paramsCondition.hashCode() +
this.headersCondition.hashCode() + this.consumesCondition.hashCode() +
this.producesCondition.hashCode() + this.customConditionHolder.hashCode());
return this.hashCode;
}

private static int calculateHashCode(
PatternsRequestCondition patterns, RequestMethodsRequestCondition methods,
ParamsRequestCondition params, HeadersRequestCondition headers,
ConsumesRequestCondition consumes, ProducesRequestCondition produces,
RequestConditionHolder custom) {

return patterns.hashCode() * 31 + methods.hashCode() + params.hashCode() +
headers.hashCode() + consumes.hashCode() + produces.hashCode() + custom.hashCode();
}

@Override
Expand Down

0 comments on commit 8dc8d88

Please sign in to comment.