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

Lightweight-Stream-APIのあるAndroidアプリ開発

operandoOS
November 09, 2016

 Lightweight-Stream-APIのあるAndroidアプリ開発

operandoOS

November 09, 2016
Tweet

More Decks by operandoOS

Other Decks in Technology

Transcript

  1. Lightweight-Stream-API? "Stream API from Java 8 rewritten on iterators for

    Java 7 and below." https://github.com/aNNiMON/Lightweight-Stream-API
  2. Lightweight-Stream-API • "without parallel processing but with a variety of

    additional methods and with custom operators" • parallel͸ఏڙ͞Ε͍ͯͳ͍
  3. Lightweight-Stream-API ≠ Java 8 Stream • ΠίʔϧͰ͸ͳ͍ • Java 8

    Stream APIͷΑ͏ͳΠϯλʔϑΣΠε ΋ͬͨ΋ͷ
  4. Java 8 Stream • είΞͷList͔Β30఺Ҏ্ͷ΋ͷΛදࣔ͢Δ • Java 8ͷCollectionʹStreamʹม׵͢Δϝιου͕͋Δ List<Integer> scores

    = Arrays.asList(100, 30, 35, 20); scores.stream() // Streamͷੜ੒ .filter(score -> score >= 30) // தؒૢ࡞ .forEach(System.out::println); // ऴ୺ૢ࡞
  5. Java 8 Parallel Stream • parallelStream()Ͱฒྻॲཧ • ฒྻͰॲཧ͢ΔͷͰ݁Ռͷॱ൪͸มΘΔՄೳੑ͕͋Δ List<Integer> scores

    = Arrays.asList(100, 30, 35, 20); scores.parallelStream() .filter(score -> score >= 30) .forEach(System.out::println);
  6. Lightweight-Stream-API • είΞͷList͔Β30఺Ҏ্ͷ΋ͷΛදࣔ͢Δ • Stream.ofͰStreamΛ࡞Δ List<Integer> scores = Arrays.asList(100, 30,

    35, 20); Stream.of(scores) // Streamͷੜ੒ .filter(score -> score >= 30) // தؒૢ࡞ .forEach(System.out::println); // ऴ୺ૢ࡞
  7. ग़ྗ͡Όͳͯ͘Listʹ͍ͨ͠ʂ(Lightweight-Stream-API) • ऴ୺ૢ࡞Λcollectʹ͢Δ͚ͩͳͷͰ؆୯ʂ List<Integer> scores = Arrays.asList(100, 30, 35, 20);

    List<Integer> filteredScores = Stream.of(scores) .filter(score -> score >= 30) .collect(Collectors.toCollection(ArrayList::new));
  8. Fragment#getContext() "Return the Context this fragment is currently associated with."

    https://developer.android.com/reference/android/support/v4/app/Fragment.html#getContext()
  9. Fragment + Optional public static Optional<Context> getOptionalContext(Fragment fragment) { if

    (fragment.isDetached()) { return Optional.empty(); } return Optional.ofNullable(fragment.getContext()); } // ҆શ! FragmentUtil.getOptionalContext(this) .ifPresent(context -> { Toast.makeText(context, "Optional", Toast.LENGTH_SHORT).show(); });
  10. Fragment + Optional public static Optional<FragmentActivity> getOptionalFragmentActivity(Fragment fragment) { if

    (fragment.isDetached()) { return Optional.empty(); } return Optional.ofNullable(fragment.getActivity()); } // menuͷߋ৽ // ҆શ! FragmentUtil.getOptionalFragmentActivity(this) .ifPresent(FragmentActivity::supportInvalidateOptionsMenu);
  11. Fragment + Optional public static Optional<Bundle> getArguments(Fragment fragment) { return

    Optional.ofNullable(fragment.getArguments()); } // ҆શ! FragmentUtil.getArguments(this) .ifPresent(bundle -> { // bundle͔Β஋ΛऔΓग़͢ });
  12. ෳ਺ͷOptionalΛѻ͏ • Optional AͱOptional Bͷ྆ํ͕nullͰͳ͚Ε͹...Έ͍ͨͳύλʔϯ • ͦ΋ͦ΋͜ΕͰਖ਼͍͠ͷ͔Θ͔Βͳ͍... Optional<FragmentActivity> optionalFragmentActivity =

    FragmentUtil.getOptionalFragmentActivity(this); Optional<String> titleOptional = titleOptional(); optionalFragmentActivity.ifPresent(fragmentActivity -> { titleOptional.ifPresent(title -> { fragmentActivity.setTitle(title); }); });
  13. ෳ਺ͷOptionalΛѻ͏ public static <T, R> Optional<Pair<T, R>> flatMapPair(Optional<T> a, Optional<R>

    b) { if (a.isPresent() && b.isPresent()) { // 2ͭͷOptionalʹ஋͕ೖ͍ͬͯΕ͹TupleΛOptionalͰϥοϓͯ͠ฦ͢ return Optional.ofNullable(Pair.create(a.get(), b.get())); } else { // ͦ͏Ͱͳ͍ͳΒempty return Optional.empty(); } } OptionalUtil.flatMapPair(optionalFragmentActivity,titleOptional) .ifPresent(pair -> { pair.getFirst()setTitle(pair.getSecond()); });
  14. ෳ਺ͷOptionalΛѻ͏ public static <F, R, T> Optional<Triplet<F, R, T>> flatMapTriplet(Optional<F>

    a, Optional<R> b, Optional<T> c) { if (a.isPresent() && b.isPresent() && c.isPresent()) { return Optional.ofNullable( Triplet.create(a.get(), b.get(), c.get())); } else { return Optional.empty(); } }
  15. Optionalʹ΋தؒૢ࡞ɺऴ୺ૢ࡞తͳͷ͕͋Δ • தؒૢ࡞?తͳͷ • map,flagMap,filter,etc… • ऴ୺ૢ࡞?తͳͷ • ifPresent,orElse,etc… •

    Optional javadoc - Lightweight-Stream-API • http://static.javadoc.io/com.annimon/stream/1.1.3/com/ annimon/stream/Optional.html
  16. Optional • AndroidͰJava 8ͷOptionalͱಉ͡Α͏ͳ΋ͷ͕࢖͑Δ • Javaྗ UP • ifͰnullνΣοΫ͢ΔΑΓࣗવͳײ͡ͰϓϩάϥϜ͕ॻ͚Δ •

    null͕ͳ͘ͳΔΘ͚Ͱ͸ͳ͍ • ѻ͍͕೉͍͜͠ͱ΋͋Δ • Optionalͷϥοϓͯ͠Δ஋͕nullͩͬͨ࣌ͷॲཧΛ๨ΕΔͱ͔...
  17. Objects΋͍͍ͧʂ • ̎ͭͷจࣈྻΛൺֱ͍ͨ͠ String s1 = Stringฦ͢ϝιου(); String s2 =

    Stringฦ͢ϝιου(); // s1͸null͔΋͠Εͳ͍... s1.equals(s2); // Objects - Lightweight-Stream-API // ͲͪΒ͔͕nullͰ΋҆શ! Objects.equals(s1,s2);
  18. Andorid N minSdkVersion 24 • Java 8ͷAPI͕͍͔ͭ͘ೖͬͯΔ • StreamͱOptional͸ೖͬͯΔ •

    ଞʹ΋͍͔ͭ͘… • Lightweight-Stream-API࢖Θͳͯ͘΋Α͍ • minSdkVersion 24...Կ೥ઌ?
  19. Links • Lightweight-Stream-API version 1.1.3 javadoc • http://static.javadoc.io/com.annimon/stream/1.1.3/overview-summary.html • Stream

    - Java 8 • https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html • Java 8 Stream Tutorial • http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/ • Optional - Java 8 • https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html • OptionalͷऔΓѻ͍͔ͨ • http://irof.hateblo.jp/entry/2015/05/05/071450