emfpath
DESCRIPTION
These are the slides I presented during Eclipse Con 2011http://www.eclipsecon.org/2011/sessions/?page=sessions&id=2164TRANSCRIPT
EMFPath
Even more cool than what I
do with strawsMikaël Barbero
OBEO
public void demo(Library l) { List<Book> books = l.getBooks(); Set<Borrower> allBorrowers = new HashSet<Borrower>(); List<Writer> allWriters = new ArrayList<Writer>(); for (Book book : books) { List<Borrower> borrowers = book.getBorrowers(); allBorrowers.addAll(borrowers); allWriters.add(book.getAuthor()); } Set<Borrower> filteredBorowers = new HashSet<Borrower>(); for (Borrower borrower : allBorrowers) { if (borrower.getLastName().startsWith("B") && borrower.getBorrowed().contains(aSpecificBook)) { filteredBorowers.add(borrower); } } }
Java library used internally @ Google for years
Superset of Google Collections
About Guava
2007 2008 2009 2010 2011
Google Collec
tions
v0.5
Google Collec
tions
v1Guava
r03
Guava r0
8
About Guava
IO
About Guava
IO Networking
About Guava
IO Networking Concurrency
About Guava
IO Networking Concurrency
Primitive types
About Guava
IO Networking Concurrency
Primitive types Collections
About Guava
IO Networking Concurrency
Primitive types Collections
About Guava
in com.google.common.base package
Function<F, T> to transform a collection
Predicate<T> to filter out a collection
public interface Function<F, T> {
T apply(F from);
}
public interface Predicate<T> {boolean apply(T from);
}
in com.google.common.collect package
Iterables.filter()
Iterators.filter()
Collections2.filter()
Sets.filter()
4224 7 12813
4224 7 12813
public static void demo(Collection<Integer> c) { Predicate<Integer> isEven = new Predicate<Integer>() { @Override public boolean apply(Integer input) { return (input.intValue() % 2 == 0); } }; Collections2.filter(c, isEven); }
4224 128
4224 7 12813
public static void demo(Collection<Integer> c) { Predicate<Integer> isEven = new Predicate<Integer>() { @Override public boolean apply(Integer input) { return (input.intValue() % 2 == 0); } }; Collections2.filter(c, isEven); }
in com.google.common.collect package
Iterables.transform()
Iterators.transform()
Collections2.transform()
Lists.transform()
OrangeApple Kiwi PearBanana
OrangeApple Kiwi PearBanana
public void demo(Collection<String> c) { Function<String, String> toUpperCase = new Function<String, String>() { @Override public String apply(String input) { return input.toUpperCase(); } }; Collections2.transform(c, toUpperCase); }
ORANGEAPPLE KIWI PEARBANANA
OrangeApple Kiwi PearBanana
public void demo(Collection<String> c) { Function<String, String> toUpperCase = new Function<String, String>() { @Override public String apply(String input) { return input.toUpperCase(); } }; Collections2.transform(c, toUpperCase); }
Predicates
Functions
composeforPredicate
andornotcompose
Compose and combine
Beware of
Beware of
lazyness
Copy!
Lists.newArrayList() Lists.newLinkedList() Sets.newHashSet() Sets.newLinkedHashSet() Sets.newTreeSet()
ImmutableList.copyOf() ImmutableSet.copyOf()
and make it immutable...
Path
Set of functions and predicates for EMF objects
Code generators for your own Ecore models
and few other utility classes
public interface Function<F, T> {
T apply(F from);
}public interface Predicate<T> {
boolean apply(T from);}
‣ parent (eContainer)
‣ ancestor‣ ancestorOrSelf‣ child (eContents)
‣ descendant (eAllContents)
‣ descendantOrSelf‣ following‣ followingSibling‣ preceding‣ precedingSibling
Lazy EObjects containment tree walking
EObject myObject;Collection<EObject> fs = followingSibling.of(myObject);
HavingIsKind/IsTypeIsAncestor/IsChild
Common predicates
public static Collection<EObject> demoHaving(Collection<EObject> c) {
return Collections2.filter(c, Having.feature(EcorePackage.Literals.ENAMED_ELEMENT__NAME,
StringPredicates.firstUpperCase) ); }
non-EMF functions & predicates
length : Function<String, Integer>toL1Case : Function<String, String>toLowerCase : Function<String, String>toU1Case : Function<String, String>toUpperCase : Function<String, String>trim : Function<String, String>replaceAll(Pattern, String)replaceAll(String, String)replaceFirst(Pattern, String)replaceFirst(String, String)substring(int)substring(int, int)
Strings
Predicates to test ordering:equalless thangreater thanless or equalgreater or equal
Comparable
Ecore API has been Guava-ified
‣ EObject.eResource() is wrapped as a Function in EObjectPath.eResource
‣ EObject.eIsProxy() is wrapped as a Predicate in EObjectPath.eIsProxy
‣ EClass.getESuperTypes() is wrapped as a Function in EClass.eSuperTypes
‣ EReference.isContainment() is wrapped as a Predicate in ERefrencePath.isContainment
Ecore has been Guava-ified through a generator
that is available for your own Ecore model
+
time to play
public void demo(Library l) { List<Book> books = l.getBooks(); Set<Borrower> allBorrowers = new HashSet<Borrower>(); List<Writer> allWriters = new ArrayList<Writer>(); for (Book book : books) { List<Borrower> borrowers = book.getBorrowers(); allBorrowers.addAll(borrowers); allWriters.add(book.getAuthor()); } Set<Borrower> filteredBorowers = new HashSet<Borrower>(); for (Borrower borrower : allBorrowers) { if (borrower.getLastName().startsWith("B") && borrower.getBorrowed().contains(aSpecificBook)) { filteredBorowers.add(borrower); } } }
public void demo2(Library l) { List<Book> books = l.getBooks(); Set<Borrower> allBorrowers = ImmutableSet.copyOf( Iterables.concat( Collections2.transform(books, BookPath.borrowers)));
List<Writer> allWriters = Lists.transform(books, BookPath.author); Predicate<Borrower> predicate = new Predicate<Borrower>() { @Override public boolean apply(Borrower borrower) { return borrower.getLastName().startsWith("B") && borrower.getBorrowed().contains(aSpecificBook); } }; Set<Borrower> filteredBorrowers = Sets.filter(allBorrowers, predicate); }
public void demo2(Library l) { List<Book> books = l.getBooks(); Set<Borrower> allBorrowers = ImmutableSet.copyOf( Iterables.concat( Collections2.transform(books, BookPath.borrowers)));
List<Writer> allWriters = Lists.transform(books, BookPath.author); Predicate<Borrower> predicate = new Predicate<Borrower>() { @Override public boolean apply(Borrower borrower) { return borrower.getLastName().startsWith("B") && borrower.getBorrowed().contains(aSpecificBook); } }; Set<Borrower> filteredBorrowers = Sets.filter(allBorrowers, predicate); }
public void demo2(Library l) { List<Book> books = l.getBooks(); Set<Borrower> allBorrowers = ImmutableSet.copyOf( Iterables.concat( Collections2.transform(books, BookPath.borrowers)));
List<Writer> allWriters = Lists.transform(books, BookPath.author); Set<Borrower> filteredBorrowers = Sets.filter(allBorrowers, predicate); }
1. Function and Predicate2. Lazyness3. Walking EObjects containment tree along XPath-like axes4. Code generators for your own Ecore model
What you should remember
about this presentation
1. Guava is cool! 2. EMF is cool too!3. EMFPath bridges the gap between Guava and EMF4. EMFPath is cool - Q.E.D
What you should REALLY remember
about this presentation
http://code.google.com/a/eclipselabs.org/p/emfpath/
EMFPath v0.4.0 is available NOW!
Q&A