generics and anonymous adapter
Hello everyone,
I have the following code:
PaginatorAdapter.java
publicabstractclass PaginatorAdapter
{
publicabstractvoid doPage( java.util.List resultsPage );
}
And the following anonymous adapter
privatevoid method()
{
new DSSUtilities().query( Query01ComuneView.class, null, 500,new PaginatorAdapter()
{
publicvoid doPage( List<Query01ComuneView> results )
{
for( Query01ComuneView row: results )
{
row.getValue();
}
}
}, featureTypeStyle );
}
}
I'm new to generics and I understand this:
1) in PaginatorAdapter I do not know the exactly type so I declare simple java.util.List.
2) in anonymous adapter I know the exact type of objects stored in List (Query01ComuneView more exactly)
When I compile the code, I get the following error:
Error(105): <anonymous TemplateQ01$1 is not abstract and does not override abstract method doPage(java.util.List) in PaginatorAdapter.
This tells me that it cannot interpret java.util.List><Query01ComuneView> as java.util.List.
What am I missing here?
Strangely, I don't understand why this fails under javac 1.5 compiler and works fine under oracle java compiler (oracle).
Using java version 1.5.0_09-b01
# 2
Hi,
First, thanks for the response!
You are reffering to doPage() from AbstractPaginator.java or that from the anonymous adapter?
If I put it in AbstractPaginator.java as such:
public abstract <T> void doPage( Object eventData, java.util.List<T> resultsPage );
still doesn't work writing generic list inside anonymous adapter.
I don't know if I get my point clear.
What I understand is that using generics I can use inside anonymous adapter the definition:
public void doPage( List<Query01ComuneView> results )
so then I can easily iterate with:
for( Query01ComuneView row: results )
{
row.getValue();
}
On the other hand I could simply use:
public void doPage( List results )
and iterate via:
for( Object row: results )
{
((Query01ComuneView)row).getValue();
}
but then there is no longer point of using generic, isn't it?
I read the generics tutorial and some docs, but still didn't fixed this thing. for the moment I do an explicit cast on the object iterated which works fine, but the problem still bugs me.
# 3
How about this? (Note: I've put your method inside the class for convenience, changed the list data type to String - also for convenience - and removed a bunch of parameters to the query method because they weren't relevant to the code.)
public abstract class PaginatorAdapter<T> {
public abstract void doPage(List<T> resultsPage);
private void method() {
new DSSUtilities().query(String.class,
new PaginatorAdapter<String>() {
public void doPage(List<String> results) {
for (String row : results) {
// row.getValue();
}
}
}
);
}
}
Also, the query method is defined as:
public <T> void query(Class<T> clazz, PaginatorAdapter<T> adapter)
# 4
Yes, that exactly what I wanted! Thank you very much. I awarded you the dukes.
I tried myself such combination on putting <T> in different places. but I didn't specify new PaginatorAdapter<String> as in ...
... new PaginatorAdapter<String>() { ... } ...
By writing this means that "PaginatorAdapter" is a generic class and compiler gets a hint in concrete implementation about content of list (String) ?
That's pretty interesting. More interesting is that oracle java compiler didn't complain at all (as i specified in the first post ) but when I switched to javac from ant build script it didn't work as in ojc.
Thanks again very much dannyyates :)