Slide 1

Slide 1 text

Cleaner Code [email protected]

Slide 2

Slide 2 text

Using Java 8 features in Android

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

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/

Slide 5

Slide 5 text

Lambdas & Streams
 inside
 Android

Slide 6

Slide 6 text

What are lambdas?

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Classy Java 8 Lambda implementation ErrorDialog.build() .onOK( b -> router.displayScene(b,TopicsScene.class)) .onCancel( b -> router.displayScene(b,TopicsScene.class));

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Lambdas implement the interface behind the scene reloadAndStore(key) .subscribe(v -> log.debug … reloadAndStore(key).subscribe(new Action1() { @Override public void call(V v) { log.debug("Reloaded item with key={}", key); } }

Slide 13

Slide 13 text

Java 8 Streams

Slide 14

Slide 14 text

Using Java 8 Streams import java8.util.stream.StreamSupport; public class MyClass { public void tryMe() { List 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))); } }

Slide 15

Slide 15 text

Using Java 8 Streams List 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

Slide 16

Slide 16 text

Using Groovy in Android

Slide 17

Slide 17 text

Using Groovy in Android Groovy Android Gradle Plugin https://github.com/groovy/groovy-android-gradle-

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

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 ..

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

A Groovy Interface Implementation ErrorDialog.build() .onOK({ b -> router.displayScene(b,TopicsScene.class)}) .onCancel({ b -> router.displayScene(b,TopicsScene.class)});

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Declarations - Java 7 vs Groovy def myText = “““
 
 This
 is
 some
 text!
 
 “““ String myText = “\n\nThis\nis\nsome\ntext!\n”;

Slide 27

Slide 27 text

Closures = Anonymous code def someCode = { 
 def n = 1; println n; } someCode()

Slide 28

Slide 28 text

Closures = Anonymous code methodA(someCode) def methodA(code) { 
 methodB(code)
 } def methodB(code) { 
 code()
 }

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Questions?