You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Oliver Heger <ol...@oliver-heger.de> on 2013/10/16 21:49:42 UTC

[BeanUtils] Generics for BeanComparator

There is another complicated one:

BeanComparator implements Comparator in a way that it compares a given
property of a specific bean class. The actual comparison is handled by
another Comparator stored in a member field.

So I am tempted to transform it to BeanComparator<T, P> where T is the
bean class and P is the type of the property. The internal Comparator
would then be of type <P>.

However, this does not really work out for the following reasons:
- The internal comparator can be passed to the constructor, but this is
optional. If it is not defined, a helper class from [collections] is
used providing a generic Comparator on Comparable objects. How can this
be aligned to the type parameter?
- The value of the property to be compared is obtained using
PropertyUtils which returns an Object. So there is no chance for the
compiler to do type checks. Perhaps we can check manually whether the
value of the property is of the expected type, but this implies that we
have to determine the class of the type parameter at runtime. AFAIK this
is not always possible, e.g. if a subclass is created which does not
have type parameters.

Any ideas?

Thanks
Oliver


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org