Property for an OptionGroup with MultiSelect - Best way?

I want to ask if the following fragment is the right way to create a Property for an OptionGroup with MultiSelect on.

This fragment works, but with @SuppressWarnings(“unchecked”):

class KeyDTO {
private String key;
private String value;
// + getter and setter
...
}

// my bean item container:
BeanItemContainer<KeyDTO> bic = new BeanItemContainer<KeyDTO>(KeyDTO.class);
bic.addAll(...); // adding some keys

// my Property:
Property selectedItems = new ObjectProperty<Set>(null, Set.class);


//
cr = new OptionGroup();
cr.setContainerDataSource(bic);
cr.setMultiSelect(true);
cr.setItemCaptionMode(OptionGroup.ITEM_CAPTION_MODE_PROPERTY);
cr.setItemCaptionPropertyId("value");
cr.setPropertyDataSource(selectedItems);

Before i tried something like this, but i don’t know how to declare the Second parameter (which should match Class<Set>):

Property selectedItems = new ObjectProperty<Set<KeyDTO>(null, ???);

I would like to hear about your experiences/ways concerning this.
Is there a better way? :slight_smile:

This is really a Java question, and someone can correct me if I am wrong, but I’m afraid there is no way to make this as specific as you would like without producing some warning.

The compiler would want Set.class in order not to produce a warning about the more specific type parameters for the wrapping ObjectProperty. However, due to type erasure (at runtime, the type parameters get converted e.g. to Object*), there is no such thing as Set.class . There is only one instance of Class at runtime**, which is Set.class and is the same for Set, Set etc. This is also why you cannot do e.g. “if (x instanceof Set)” - the information about the type parameter Integer does not exist at runtime when the test is executed. You can only test “if (x instanceof Set)”.

So, both approaches produce (different) warnings or errors. The compiler could (theoretically) suppress the warning/error in one of the cases, but then it would hide even further what is going on behind the scenes and that the operations are not necessarily fully safe in the general case.

Java Generics are quite limited, nowhere near what you can do with C++ templates, but still quite complicated when you go to the details.

  • Object or sometimes a more specific type, e.g. when using the “? extends Xyz” syntax for type parameters. See the
    Java Language Specification
    for some details.

** possibly per classloader, but now this is getting really complicated…