Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cleaner Code

berglind
October 11, 2015

Cleaner Code

Using Java 8 and Groovy in Android. See presentation at https://www.youtube.com/watch?v=5vEa9dihZEs

berglind

October 11, 2015
Tweet

More Decks by berglind

Other Decks in Technology

Transcript

  1. Using Java 8 features in Android Retrolambda https://github.com/evant/gradle- Gradle Retrolambda

    Plugin https://github.com/orfjackal/retrolambda StreamSupport Library http://sourceforge.net/projects/streamsupport/
  2. Classic Java 7 Interface Implementation ErrorDialog.build().onOk(new DialogListener() { @Override public

    void onPress(Button b) { router.displayScene(b,TopicsScene.class); } }).onCancel(new DialogListener()
 { @Override public void onPress(Button b) { router.displayScene(b,TopicsScene.class); } });
  3. Java 7 with Observables reloadAndStore(key).subscribe(new Action1<V>() { @Override public void

    call(V v) { log.debug("Reloaded item with key={}", key); } }, new Action1<Throwable>() { @Override public void call(Throwable e) { log.error("Error reloading key={} to store={}", key, DataStore.this.getClass().getName(), e); } });
  4. Java 8 with Observables reloadAndStore(key) .subscribe(v -> log.debug("Reloaded item with

    key={}", key), e -> log.error("Error reloading key={} to store={}", key, DataStore.this.getClass().getName(), e));
  5. Lambdas implement the interface behind the scene reloadAndStore(key) .subscribe(v ->

    log.debug … reloadAndStore(key).subscribe(new Action1<V>() { @Override public void call(V v) { log.debug("Reloaded item with key={}", key); } }
  6. Using Java 8 Streams import java8.util.stream.StreamSupport; public class MyClass {

    public void tryMe() { List<Integer> aList = new ArrayList<>(); aList.add(new Random().nextInt()); aList.add(new Random().nextInt()); aList.add(new Random().nextInt()); StreamSupport.stream(aList) .map(el -> el.hashCode()) .forEach(el-> Log.d("SERVICE", String.valueOf(el))); } }
  7. Using Java 8 Streams List<String> myList = Arrays.asList("a1", "a2", "b1",

    "c2", "c1"); StreamSupport .stream(myList) .filter(s -> s.startsWith("c")) .map(String::toUpperCase) .sorted() .forEach(v -> logger.d(v)); // C1 // C2
  8. Groovy in Android - Why? • Pure functional language, perfect

    for writing compact code in logical workflows.
 • Hides even more boilerplate code then Java 8.
 • Toolbox with built in support for lambdas, streams, 
 json-parsing and much much more ..
  9. Groovy in Android - Why? • You can mix both

    Java and Groovy classes in the code tree (live side by side). • Java can access Groovy classes and vice versa. • You already using it. Gradle is written in Groovy
  10. Classic Java 7 Interface Implementation ErrorDialog.build().onOk(new DialogListener() { @Override public

    void onPress(Button b) { router.displayScene(b,TopicsScene.class); } }).onCancel(new DialogListener()
 { @Override public void onPress(Button b) { router.displayScene(b,TopicsScene.class); } });
  11. Groovy with “streams” def myList = ["a1", "a2", "b1", "c2",

    “c1”]; myList .findAll(s -> s.startsWith("c")) .collect(s -> s.toUpperCase()) .sort() .each(s -> println s); // C1 // C2
  12. Declarations - Java 7 vs Groovy def myList = ["a1",

    "a2", "b1", "c2", “c1”] List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");
  13. Declarations - Java 7 vs Groovy def myText = “““


    
 This
 is
 some
 text!
 
 “““ String myText = “\n\nThis\nis\nsome\ntext!\n”;
  14. Json Slurper { "staff":[ { "name":"Bob", "title":"Coder" }, { "name":"Steve",

    "title":"Boss" }, { "name":"Tim", "title":"Coder" } ] } def json = JsonSlurper().parse(text); json.staff[0].name // Bob json.staff // Array of staff json.staff.find{s-> s.name == “Steve”} json.staff.findAll{t->t.title == “Coder”} json.staff.title.unique().sort() Boss, Coder