[
https://issues.apache.org/jira/browse/WINK-167?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12758302#action_12758302
]
Mike Rheinheimer edited comment on WINK-167 at 9/22/09 10:05 AM:
-----------------------------------------------------------------
I think perhaps the easy and fast way to do this is to just sort the result
list at the end of the getProvidersByMediaType method. I think trying to sort
inline would be too expensive since the sort currently implemented there is
sorting Entry<MediaType, Set<ObjectFactory<T>>> and the list we need sorted is
the value of that Entry object. It's probably easier and faster to go ahead
and sort based on MediaTypes then sort the result list, which will hopefully be
short.
Here is a proposed fix; insert this code just before the 'return list;'
statement at the bottom of getProvidersByMediaType. I'm posting the new code
inline here because a patch would overlap with WINK-193
[code]
// now we should sort by declared priority on the ObjectFactory
Collections.sort(list, new Comparator<ObjectFactory<T>>() {
public int compare(ObjectFactory<T> object1, ObjectFactory<T>
object2) {
PriorityObjectFactory<T> factory1 =
(PriorityObjectFactory<T>)object1;
PriorityObjectFactory<T> factory2 =
(PriorityObjectFactory<T>)object2;
if (factory1.priority > factory2.priority) {
return -1;
} else if (factory1.priority < factory2.priority) {
return 1;
}
return 0;
}
});
[/code]
Also add the following test to
org.apache.wink.common.internal.providers.ProvidersContextResolverTest
public void testContextResolverPrioritySort() {
ProvidersRegistry providers = createProvidersRegistryImpl();
// note: the order these are added is important to the test
assertTrue(providers.addProvider(new StringContextResolver4(), 0));
assertTrue(providers.addProvider(new StringContextResolver3(), 0.2));
assertTrue(providers.addProvider(new StringContextResolver2(), 0.1));
// StringContextResolver3 has the highest priority (0.2) even though
StringContextResolver2
// more closely matches based on the media type in @Produces
assertSame(STRING3, providers.getContextResolver(String.class,
null,
null).getContext(String.class));
}
was (Author: rott):
I think perhaps the easy and fast way to do this is to just sort the result
list at the end of the getProvidersByMediaType method. I think trying to sort
inline would be too expensive since the sort currently implemented there is
sorting Entry<MediaType, Set<ObjectFactory<T>>> and the list we need sorted is
the value of that Entry object. It's probably easier and faster to go ahead
and sort based on MediaTypes then sort the result list, which will hopefully be
short.
Here is a proposed fix; insert this code just before the 'return list;'
statement at the bottom of getProvidersByMediaType. I'm posting the new code
inline here because a patch would overlap with WINK-193
// now we should sort by declared priority on the ObjectFactory
Collections.sort(list, new Comparator<ObjectFactory<T>>() {
public int compare(ObjectFactory<T> object1, ObjectFactory<T>
object2) {
PriorityObjectFactory<T> factory1 =
(PriorityObjectFactory<T>)object1;
PriorityObjectFactory<T> factory2 =
(PriorityObjectFactory<T>)object2;
if (factory1.priority > factory2.priority) {
return -1;
} else if (factory1.priority < factory2.priority) {
return 1;
}
return 0;
}
});
Also add the following test to
org.apache.wink.common.internal.providers.ProvidersContextResolverTest
public void testContextResolverPrioritySort() {
ProvidersRegistry providers = createProvidersRegistryImpl();
// note: the order these are added is important to the test
assertTrue(providers.addProvider(new StringContextResolver4(), 0));
assertTrue(providers.addProvider(new StringContextResolver3(), 0.2));
assertTrue(providers.addProvider(new StringContextResolver2(), 0.1));
// StringContextResolver3 has the highest priority (0.2) even though
StringContextResolver2
// more closely matches based on the media type in @Produces
assertSame(STRING3, providers.getContextResolver(String.class,
null,
null).getContext(String.class));
}
> The sort algorithm in ProvidersRegistry.getProvidersByWildcardMediaType
> ignores priority
> ----------------------------------------------------------------------------------------
>
> Key: WINK-167
> URL: https://issues.apache.org/jira/browse/WINK-167
> Project: Wink
> Issue Type: Bug
> Components: Common
> Affects Versions: 0.1
> Reporter: Michael Elman
>
> The sort algorithm in ProvidersRegistry.getProvidersByWildcardMediaType
> ignores priority property of the provider.
> It should be latest key in the sort algorithm as it is used in
> getProvidersByMediaType method.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.