reta commented on a change in pull request #407: [CXF-7716] Reduce
StringBuilders and other performance changes.
URL: https://github.com/apache/cxf/pull/407#discussion_r183578479
##########
File path:
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
##########
@@ -1508,11 +1509,40 @@ public static boolean matchMimeTypes(MediaType
requestContentType,
}
}
}
-
return new ArrayList<>(supportedMimeTypeList);
+ }
+
+ public static boolean doMimeTypesIntersect(List<MediaType>
requiredMediaTypes, List<MediaType> userMediaTypes) {
+ for (MediaType requiredType : requiredMediaTypes) {
Review comment:
Turned out to be quite tricky ... but I have one suggestion for you. Here is
the implementation for `doMimeTypesIntersect` and `intersectMimeTypes`:
```
public static List<MediaType> intersectMimeTypes(List<MediaType>
requiredMediaTypes,
List<MediaType>
userMediaTypes,
boolean
addRequiredParamsIfPossible,
boolean
addDistanceParameter) {
final AccumulatingConsumer consumer = new
AccumulatingConsumer(addRequiredParamsIfPossible, addDistanceParameter);
intersectMimeTypes(requiredMediaTypes, userMediaTypes, consumer);
return new ArrayList<>(consumer.supportedMimeTypeList);
}
public static boolean doMimeTypesIntersect(List<MediaType>
requiredMediaTypes, List<MediaType> userMediaTypes) {
final NonAccumulatingConsumer consumer = new
NonAccumulatingConsumer();
intersectMimeTypes(requiredMediaTypes, userMediaTypes, consumer);
return consumer.doIntersect;
}
```
Here is the initial `intersectMimeTypes` function, refactored to use
consumers, the part which is was accumulating (or returning `true`) is now part
of the consumers:
```
private static void intersectMimeTypes(List<MediaType> requiredMediaTypes,
List<MediaType> userMediaTypes,
BiConsumer<MediaType, MediaType> consumer) {
for (MediaType requiredType : requiredMediaTypes) {
for (MediaType userType : userMediaTypes) {
boolean isCompatible = isMediaTypeCompatible(requiredType,
userType);
if (isCompatible) {
boolean parametersMatched = true;
for (Map.Entry<String, String> entry :
userType.getParameters().entrySet()) {
String value =
requiredType.getParameters().get(entry.getKey());
if (value != null && entry.getValue() != null
&& !(stripDoubleQuotesIfNeeded(value).equals(
stripDoubleQuotesIfNeeded(entry.getValue())))) {
if (HTTP_CHARSET_PARAM.equals(entry.getKey())
&& value.equalsIgnoreCase(entry.getValue()))
{
continue;
}
parametersMatched = false;
break;
}
}
if (!parametersMatched) {
continue;
}
consumer.accept(requiredType, userType);
}
}
}
}
```
And finally, two consumers:
```
private static class NonAccumulatingConsumer implements
BiConsumer<MediaType, MediaType> {
private boolean doIntersect;
@Override
public void accept(MediaType t, MediaType u) {
doIntersect = true;
}
}
```
And
```
private static class AccumulatingConsumer implements
BiConsumer<MediaType, MediaType> {
private final Set<MediaType> supportedMimeTypeList = new
LinkedHashSet<MediaType>();
private final boolean addRequiredParamsIfPossible;
private final boolean addDistanceParameter;
private AccumulatingConsumer(boolean addRequiredParamsIfPossible,
boolean addDistanceParameter) {
this.addRequiredParamsIfPossible = addRequiredParamsIfPossible;
this.addDistanceParameter = addDistanceParameter;
}
@Override
public void accept(MediaType requiredType, MediaType userType) {
boolean requiredTypeWildcard =
requiredType.getType().equals(MediaType.MEDIA_TYPE_WILDCARD);
boolean requiredSubTypeWildcard =
requiredType.getSubtype().contains(MediaType.MEDIA_TYPE_WILDCARD);
String type = requiredTypeWildcard ? userType.getType() :
requiredType.getType();
String subtype = requiredSubTypeWildcard ? userType.getSubtype()
: requiredType.getSubtype();
Map<String, String> parameters = userType.getParameters();
if (addRequiredParamsIfPossible) {
parameters = new LinkedHashMap<String, String>(parameters);
for (Map.Entry<String, String> entry :
requiredType.getParameters().entrySet()) {
if (!parameters.containsKey(entry.getKey())) {
parameters.put(entry.getKey(), entry.getValue());
}
}
}
if (addDistanceParameter) {
int distance = 0;
if (requiredTypeWildcard) {
distance++;
}
if (requiredSubTypeWildcard) {
distance++;
}
parameters.put(MEDIA_TYPE_DISTANCE_PARAM,
Integer.toString(distance));
}
supportedMimeTypeList.add(new MediaType(type, subtype,
parameters));
}
}
```
Ideally, would be good to move to a dedicated utility class, but roughly
this is an idea. What do you think. @WhiteCat22, does it make sense?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services