views:

29

answers:

2

Dear All:

I would like to implement a Glazed List that has both an AdvancedTableFormat and WritableTableFormat interface.

I am reading here: http://www.jroller.com/aalmiray/entry/glazedlists_groovy_not_your_regular

and for one interface it seems this is possible in Groovy with the "as" keyword:

# return new EventTableModel(linksList, [  
#       getColumnCount: {columnNames.size()},  
#       getColumnName: {index -> columnNames[index]},  
#       getColumnValue: {object, index ->  
#          object."${columnNames[index].toLowerCase()}"  
#       }] as TableFormat)

Is it somehow possible to do this for two interfaces? If so how?

Thank you!

Misha

A: 

You can create a new interface that extends the two interfaces you are interested in.

interface PersonalizedTableFormat extends AdvancedTableFormat, WriteableTableFormat {
}

You can cast the object you return to the new interface.

return object as PersonalizedTableFormat;
frm
This is neat. Btw can I implement an interface _within_ a class or must I always implement it outside one? Thank you!
Misha Koshelev
The rules for Groovy interfaces are the same for Java interfaces. You can define PersonalizedTableFormat as a private interface in your class and create an anonymous inner type "on the fly" as you do in your example.
frm
A: 

The "as" keyword is just a fancy way of invoking Groovy's asType(Class) method, which takes only a single Class as an argument. Therefore you can't directly use "as" with more than one interface (unless you take frm's approach and combine the interfaces in one super interface).

Christoph Metzendorf