Mikaël Barbero        OBEO
public    void demo(Library l) {	 	   List<Book> books = l.getBooks();	 		 	   Set<Borrower> allBorrowers = new HashSet<Bo...
About GuavaJava library used internally               Superset of Google   @ Google for years                         Coll...
About Guava
About GuavaIO
About GuavaIO   Networking
About GuavaIO   Networking   Concurrency
About GuavaIO               Networking   Concurrency     Primitive types
About GuavaIO               Networking       Concurrency     Primitive types     Collections
About GuavaIO               Networking       Concurrency     Primitive types     Collections
in com.google.common.base package                                     public interface Predicate<T> { Predicate<T> to filte...
in com.google.common.collect package                        Iterables.filter()                        Iterators.filter()    ...
24   42   13   7   128
24                  42               13                7        128public     static void demo(Collection<Integer> c) {	 	...
24                  42               13                7        128public     static void demo(Collection<Integer> c) {	 	...
in com.google.common.collect package Iterables.transform() Iterators.transform()Collections2.transform()   Lists.transform()
Apple   Orange   Banana   Kiwi   Pear
Apple              Orange            Banana             Kiwi             Pearpublic     void demo(Collection<String> c) {	...
Apple              Orange            Banana             Kiwi             Pearpublic     void demo(Collection<String> c) {	...
Compose and combineFunctions    compose    forPredicatePredicates     and     or     not     compose
Bewareof
Bewareoflazyness
Copy!Lists.newArrayList()Lists.newLinkedList()Sets.newHashSet()Sets.newLinkedHashSet()Sets.newTreeSet()and make it immutab...
Path
{                                 public interface Function<F, T>   Set of functions and          }                       ...
Lazy EObjects containment tree walking   ‣ parent     (eContainer)   ‣ ancestor   ‣ ancestorOrSelf   ‣ child   (eContents)...
Common predicates       Having       IsKind/IsType       IsAncestor/IsChild public     static   Collection<EObject> demoHa...
non-EMF functions & predicates            Stringslength : Function<String, Integer>toL1Case : Function<String, String>    ...
Ecore API has been Guava-ified‣ EObject.eResource() is wrapped as a Function inEObjectPath.eResource‣ EObject.eIsProxy() is...
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<Bo...
public      void demo2(Library l) {	 	     List<Book> books = l.getBooks();	 		 	     Set<Borrower> allBorrowers =	 	     ...
public      void demo2(Library l) {	 	     List<Book> books = l.getBooks();	 		 	     Set<Borrower> allBorrowers =	 	     ...
public      void demo2(Library l) {	 	     List<Book> books = l.getBooks();	 		 	     Set<Borrower> allBorrowers =	 	     ...
Wha t you should rememberabout this presentation1. Function and Predicate2. Lazyness3. Walking EObjects containment treeal...
What you  should REALLY remember about this presentation    1. Guava is cool!    2. EMF is cool too!    3. EMFPath bridges...
Q&A
EMFPath
EMFPath
Upcoming SlideShare
Loading in …5
×

EMFPath

1,913 views
1,813 views

Published on

These are the slides I presented during Eclipse Con 2011

http://www.eclipsecon.org/2011/sessions/?page=sessions&id=2164

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,913
On SlideShare
0
From Embeds
0
Number of Embeds
694
Actions
Shares
0
Downloads
33
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

EMFPath

  1. 1. Mikaël Barbero OBEO
  2. 2. 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); } } }
  3. 3. About GuavaJava library used internally Superset of Google @ Google for years Collections s s ti on ti on c lle llec Co 5 Co a r03 a r08 gle v0. gle v1 uav uav G oo G oo G G2007 2008 2009 2010 2011
  4. 4. About Guava
  5. 5. About GuavaIO
  6. 6. About GuavaIO Networking
  7. 7. About GuavaIO Networking Concurrency
  8. 8. About GuavaIO Networking Concurrency Primitive types
  9. 9. About GuavaIO Networking Concurrency Primitive types Collections
  10. 10. About GuavaIO Networking Concurrency Primitive types Collections
  11. 11. in com.google.common.base package public interface Predicate<T> { Predicate<T> to filter boolean apply(T from); out a collection }pu blic interface Function<F, T> { T apply(F from); Function<F, T> to} transform a collection
  12. 12. in com.google.common.collect package Iterables.filter() Iterators.filter() Collections2.filter() Sets.filter()
  13. 13. 24 42 13 7 128
  14. 14. 24 42 13 7 128public 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); }
  15. 15. 24 42 13 7 128public 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); } 24 42 128
  16. 16. in com.google.common.collect package Iterables.transform() Iterators.transform()Collections2.transform() Lists.transform()
  17. 17. Apple Orange Banana Kiwi Pear
  18. 18. Apple Orange Banana Kiwi Pearpublic 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); }
  19. 19. Apple Orange Banana Kiwi Pearpublic 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); }APPLE ORANGE BANANA KIWI PEAR
  20. 20. Compose and combineFunctions compose forPredicatePredicates and or not compose
  21. 21. Bewareof
  22. 22. Bewareoflazyness
  23. 23. Copy!Lists.newArrayList()Lists.newLinkedList()Sets.newHashSet()Sets.newLinkedHashSet()Sets.newTreeSet()and make it immutable... ImmutableList.copyOf() ImmutableSet.copyOf()
  24. 24. Path
  25. 25. { public interface Function<F, T> Set of functions and } T apply(F from); public interface Predicate<T> {predicates for EMF objects } boolean apply(T from); Code generators for your own Ecore modelsand few other utility classes
  26. 26. Lazy EObjects containment tree walking ‣ parent (eContainer) ‣ ancestor ‣ ancestorOrSelf ‣ child (eContents) ‣ descendant (eAllContents) ‣ descendantOrSelf ‣ following ‣ followingSibling ‣ preceding ‣ precedingSibling EObject myObject; Collection<EObject> fs = followingSibling.of(myObject);
  27. 27. Common predicates Having IsKind/IsType IsAncestor/IsChild public static Collection<EObject> demoHaving(Collection<EObject> c) { return Collections2.filter(c, Having.feature(EcorePackage.Literals.ENAMED_ELEMENT__NAME, StringPredicates.firstUpperCase) ); }
  28. 28. non-EMF functions & predicates Stringslength : Function<String, Integer>toL1Case : Function<String, String> ComparabletoLowerCase : Function<String, String>toU1Case : Function<String, String> Predicates totoUpperCase : Function<String, String> test ordering:trim : Function<String, String> equal replaceAll(Pattern, String) less than replaceAll(String, String) greater than replaceFirst(Pattern, String) less or equal replaceFirst(String, String) greater or equal substring(int) substring(int, int)
  29. 29. Ecore API has been Guava-ified‣ EObject.eResource() is wrapped as a Function inEObjectPath.eResource‣ EObject.eIsProxy() is wrapped as a Predicate inEObjectPath.eIsProxy‣ EClass.getESuperTypes() is wrapped as aFunction in EClass.eSuperTypes‣ EReference.isContainment() is wrapped as aPredicate in ERefrencePath.isContainment
  30. 30. Ecore has been Guava-ified through a generator + that is available for your own Ecore model
  31. 31. time to play
  32. 32. 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); } } }
  33. 33. 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); }
  34. 34. 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); }
  35. 35. 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); }
  36. 36. Wha t you should rememberabout this presentation1. Function and Predicate2. Lazyness3. Walking EObjects containment treealong XPath-like axes4. Code generators for your own Ecoremodel
  37. 37. What you should REALLY remember about this presentation 1. Guava is cool! 2. EMF is cool too! 3. EMFPath bridges the gap between Guava and EMF 4. EMFPath is cool - Q.E.DEMFPath v0.4.0 is available NOW!http://code.google.com/a/eclipselabs.org/p/emfpath/
  38. 38. Q&A

×