Mikaë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);
	

 	

   	

 }
	

 	

   }
	

 }
About Guava

Java 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              G

2007                      2008   2009             2010                      2011
About Guava
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
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
in com.google.common.collect package




                        Iterables.filter()
                        Iterators.filter()
                      Collections2.filter()
                          Sets.filter()
24   42   13   7   128
24                  42               13                7        128



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);
	

 }
24                  42               13                7        128



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);
	

 }




                       24               42              128
in com.google.common.collect package




 Iterables.transform()
 Iterators.transform()
Collections2.transform()
   Lists.transform()
Apple   Orange   Banana   Kiwi   Pear
Apple              Orange            Banana             Kiwi             Pear



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);
	

 }
Apple              Orange            Banana             Kiwi             Pear



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);
	

 }




APPLE             ORANGE           BANANA               KIWI             PEAR
Compose and combine
Functions
    compose
    forPredicate

Predicates
     and
     or
     not
     compose
Beware
of
Beware
of


lazyness
Copy!
Lists.newArrayList()
Lists.newLinkedList()
Sets.newHashSet()
Sets.newLinkedHashSet()
Sets.newTreeSet()



and make it immutable...

        ImmutableList.copyOf()
        ImmutableSet.copyOf()
Path
{
                                 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 models



and few other utility classes
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);
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)
 	

 	

   	

 );
 	

 }
non-EMF functions & predicates
            Strings
length : Function<String, Integer>
toL1Case : Function<String, String>        Comparable
toLowerCase : Function<String, String>
toU1Case : Function<String, String>      Predicates to
toUpperCase : 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)
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);
	

   }
Wha t you should remember
about this presentation

1. Function and Predicate
2. Lazyness
3. Walking EObjects containment tree
along XPath-like axes
4. Code generators for your own Ecore
model
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.D


EMFPath v0.4.0 is available NOW!

http://code.google.com/a/eclipselabs.org/p/emfpath/
Q&A

EMFPath

  • 1.
  • 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.
    About Guava Java libraryused 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 G 2007 2008 2009 2010 2011
  • 6.
  • 7.
  • 8.
    About Guava IO Networking
  • 9.
    About Guava IO Networking Concurrency
  • 10.
    About Guava IO Networking Concurrency Primitive types
  • 11.
    About Guava IO Networking Concurrency Primitive types Collections
  • 12.
    About Guava IO Networking Concurrency Primitive types Collections
  • 13.
    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
  • 14.
    in com.google.common.collect package Iterables.filter() Iterators.filter() Collections2.filter() Sets.filter()
  • 15.
    24 42 13 7 128
  • 16.
    24 42 13 7 128 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); }
  • 17.
    24 42 13 7 128 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); } 24 42 128
  • 18.
    in com.google.common.collect package Iterables.transform() Iterators.transform() Collections2.transform() Lists.transform()
  • 19.
    Apple Orange Banana Kiwi Pear
  • 20.
    Apple Orange Banana Kiwi Pear 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); }
  • 21.
    Apple Orange Banana Kiwi Pear 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); } APPLE ORANGE BANANA KIWI PEAR
  • 22.
    Compose and combine Functions compose forPredicate Predicates and or not compose
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
    { 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 models and few other utility classes
  • 28.
    Lazy EObjects containmenttree walking ‣ parent (eContainer) ‣ ancestor ‣ ancestorOrSelf ‣ child (eContents) ‣ descendant (eAllContents) ‣ descendantOrSelf ‣ following ‣ followingSibling ‣ preceding ‣ precedingSibling EObject myObject; Collection<EObject> fs = followingSibling.of(myObject);
  • 29.
    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) ); }
  • 30.
    non-EMF functions &predicates Strings length : Function<String, Integer> toL1Case : Function<String, String> Comparable toLowerCase : Function<String, String> toU1Case : Function<String, String> Predicates to toUpperCase : 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)
  • 31.
    Ecore API hasbeen 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
  • 32.
    Ecore has beenGuava-ified through a generator + that is available for your own Ecore model
  • 33.
  • 34.
    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); } } }
  • 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); 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); }
  • 36.
    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); }
  • 37.
    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); }
  • 38.
    Wha t youshould remember about this presentation 1. Function and Predicate 2. Lazyness 3. Walking EObjects containment tree along XPath-like axes 4. Code generators for your own Ecore model
  • 39.
    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.D EMFPath v0.4.0 is available NOW! http://code.google.com/a/eclipselabs.org/p/emfpath/
  • 40.