Generics in Eclipse Modelling Framework

We want to enforce that F is a type of Fruit. In the Ecore editor, select the EType Parameter child of the Basket element. Choose “New Child::EGeneric Bound Type”. In the properties view, set the EClassifier attribute to Fruit. Now the name of the class should display “Basket<F extends Fruit>”.

Figure 4: Setting the generic bound typeFigure 4: Setting the generic bound type

Now we need to inherit this power in our basket objects. Expand the children of the  AppleBasket class. You will see an inheritance of Basket. Right click on theinheritance child, Basket, and choose from the context menu “New Child::EGeneric Type Argument”.  If you select on AppleBasket you will not see this menu selection.

Figure 5: Selecting the EGeneric Type Argument menuFigure 5: Selecting the EGeneric Type Argument menu

In the properties view, set the EClassifier to Apple. Repeat for OrangeBasket. You should have what appears in Figure 6.

Figure 6: Generic type argument on inheritanceFigure 6: Generic type argument on inheritance

When you generate the model code, you should see interfaces that look like Listing 4.

public interface Basket<F extends Fruit> extends EObject {
  public EList<F> getContents();
}

public interface AppleBasket extends Basket<Apple> {
}

Listing 4

With the generics in place, you can now be as specific or as general in your model programming as you like. We have enforced that AppleBasket objects can only contain Apple objects. At the same time, we have enabled the access the contents of all FruitBasket objects as Fruit. Listing 5 shows an example of using the model.

AppleBasket appleBasket = FruitFactory.eINSTANCE.createAppleBasket();
…
for (Apple apple : appleBasket.getContents()){
…
}
List<FruitBasket> baskets;
…
baskets.add(appleBasket);
for (FruitBasket basket: baskets){
  for (Fruit fruit: basket.getContents){
    …
  }
}

Listing 5


 

Article Type: 
How-to
0
Average: 5 (1 vote)

Jeffrey Ricker is an experienced technology development executive with 15 years of leadership in defense and the private sector. He is the principal of Jeffrey Ricker LLC, providing expertise in Eclipse, RCP and OSGi to Fortune 500 and start-up clients. Previously, he was the founder of Distributed Instruments and XML Solutions Corp. Jeffrey is a DZone MVB and is not an employee of DZone and has posted 5 posts at DZone. You can read more from them at their website.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

crazzy replied on Wed, 2009/03/11 - 11:52am

But anyway the EclipseLink looks great, StoredProc and Converter are what I want to see in the next JPA

regards,

Term Paper

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.