[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [p2-dev] Extending classes with generics

On 2010-01-04 22:54, Curtis Windatt wrote:
Recently a number of the query API were changed to use generics, this causes some annoyances in PDE (as we are strictly 1.4), but up until now I can work around them.  However, I have a problem extending a class with generics, specifically MatchQuery

IQuery query = new MatchQuery() {
                        public boolean isMatch(Object candidate) {
                                if (candidate instanceof IInstallableUnit) {
                                        if (P2Utils.isBundle((IInstallableUnit) candidate)) {
                                                return true;
                                return false;

The compiler complains that I need to extend the isMatch(T candidate) instead, but I am unable to use generics in PDE.

I noticed that error too. It's a strange error though. It manifests itself as a marker but the code compiles just fine. IMO, this indicates a problem with the compiler since it shouldn't know about generics when compiling for 1.4.

Perhaps it's related to the 1.5 workspace compilation of the p2 bundles? If so, the marker will go away once we tweak the preference setting to get 1.4 binaries in the workspace.

As a side note, why was size() removed from IQueryResult?  It was quite convenient for me when creating arrays of InstallableUnitDescriptions and other objects that get filled in based on the query result iterator.  Now I have to grab one of the sets and ask it for the size.  Not a big deal, but it felt natural to ask the result for its size, since you can ask it for an iterator.

The reason is that it in some cases it might be very expensive to compute the size. Consider the case when you query a remote repository and get an iterator back. Having the size() method present gives the impression that size() is indeed cheap (one proof of that was a large number of size() == 0 tests now replaced by isEmpty()).

You can create an array using the toArray() method. The IQueryResult implementation will know the most efficient way to create the array. You can also use the toSet() method to obtain a copy of the result in a Set that you can add more things to. Or you can use the unmodifiableSet() to get an immutable representation of the result. If you know that the result indeed stems from something that contains a set, then you can always use unmodifiableSet().size() to get the size if that's really needed.

Thomas Hallgren