SlideShare ist ein Scribd-Unternehmen logo
1 von 31
Hello, Guava !
What is Guava ?

    The Guava project contains several of Google's core
    libraries that we rely on in our Java-based projects:
    collections, caching, primitives support, concurrency
    libraries, common annotations, string processing, I/O,
    and so forth. 
                          http://code.google.com/p/guava-libraries/




               GoogleによるJavaライブラリ

                   Guaaaaaaaaaaaaaaaaaaaava!
2                                     Copyright © 2012 Akira Koyasu. Some rights reserved.
Guava provides...


    •いつも書いている煩雑なコードを簡潔に書きやすく
    •使い勝手の良いユーティリティクラス
    •コンパクトなAPI

            小さな悩みをすっきり解決。
         あまり大きな悩みは解決してくれません。



          Guaaaaaaaaaaaaaaaaaaaava!
3                         Copyright © 2012 Akira Koyasu. Some rights reserved.
How to use

    Maven dependency

           <dependency>
           	 <groupId>com.google.guava</groupId>
           	 <artifactId>guava</artifactId>
           	 <version>11.0.2</version>
           </dependency>


                   ... or Use other dependency managements,
                            Download jar from the site


                 Guaaaaaaaaaaaaaaaaaaaava!
4                                 Copyright © 2012 Akira Koyasu. Some rights reserved.
Packages
           com.google.common.annotations
           com.google.common.base
           com.google.common.cache
           com.google.common.collect
           com.google.common.eventbus
           com.google.common.hash
           com.google.common.io
           com.google.common.math
           com.google.common.net
           com.google.common.primitives
           com.google.common.util.concurrent


            Guaaaaaaaaaaaaaaaaaaaava!
5                                 Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.base




        Guaaaaaaaaaaaaaaaaaaaava!
                        Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.base




        Guaaaaaaaaaaaaaaaaaaaava!
                        Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.base

    Subject 0.

            プログラムの実行時間を測りましょう


    Subject 1.

            コストの高い処理の結果をキャッシュしましょう




                 Guaaaaaaaaaaaaaaaaaaaava!
7                                Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.base
    大切なインタフェース

       Function<F,T>
                       T apply(F input)

       Predicate<T>
                       boolean apply(T input)

        Supplier<T>
                       T get()


               Guaaaaaaaaaaaaaaaaaaaava!
8                                Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.base
    Preconditions


     import static com.google.common.base.Preconditions.*;
     ...
     	 public void someMethod(int pos) {
     	 	 checkArgument(pos > 0);
     	 }



          posが0以下の場合はIllegalArgumentExceptionが
                        スローされます


                    Guaaaaaaaaaaaaaaaaaaaava!
9                                     Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.base
     Joiner


         String[] strs = { "taro", "jiro", "saburo" };
         String str = Joiner.on(",").join(strs);



     Splitter


         String str = "taro,jiro,saburo";
         Iterable<String> strs = Splitter.on(",").split(str);




                       Guaaaaaaaaaaaaaaaaaaaava!
10                                      Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.base
     CaseFormat

                  LOWER_HYPHEN
                  LOWER_UNDERSCORE
                  LOWER_CAMEL
                  UPPER_CAMEL
                  UPPER_UNDERSCORE

     import static com.google.common.base.CaseFormat.*;

     LOWER_UNDERSCORE.to(LOWER_CAMEL, "create_date");              // createDate
     UPPER_CAMEL.to(LOWER_UNDERSCORE, "SomeName");                 // some_name


                      Guaaaaaaaaaaaaaaaaaaaava!
11                                        Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.base
     Stopwatch


        Stopwatch stopwatch = new Stopwatch().start();
        stopwatch.stop();
        System.out.printf("time: %s%n", stopwatch);



     Charsets


        Charset charset = Charsets.UTF_8;




                      Guaaaaaaaaaaaaaaaaaaaava!
12                                     Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.collect




         Guaaaaaaaaaaaaaaaaaaaava!
                         Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.collect




         Guaaaaaaaaaaaaaaaaaaaava!
                         Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.collect

     Subject 2.

           あるリストを加工して別のリストを作りましょう




                  Guaaaaaaaaaaaaaaaaaaaava!
14                                Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.collect
     頻出メソッド


         Collection<E> filter(Collection<E> unfiltered,
                          Predicate<? super E> predicate)


      Collection<T> transform(Collection<F> fromCollection,
                       Function<? super F,T> function)




                     Guaaaaaaaaaaaaaaaaaaaava!
15                                   Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.collect
     Lists

        List<String> list1 = Lists.newArrayList();
        List<String> list2
                 = Lists.newArrayListWithCapacity(100);



     Maps

        Map<Integer, String> map1 = Maps.newHashMap();
        Map<Integer, String> map2
                 = Maps.newHashMapWithExpectedSize(100);




                      Guaaaaaaaaaaaaaaaaaaaava!
16                                     Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.collect
 ForwardingCollection
    List<String> list = new ForwardingList<String>() {
    	 @Override
    	 protected List<String> delegate() {
    	 	 return backingList;
    	 }
    };


 ImmutableCollection
    ImmutableSet<String> set
    	 =ImmutableSet.<String>builder()
    	 	 .add("taro")
    	 	 .add("jiro")
    	 	 .add("saburo").build();




                      Guaaaaaaaaaaaaaaaaaaaava!
                                              Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.collect
     ComparisonChain

        public class SomeOne implements Comparable<SomeOne> {
        	 private String name;
        	 private int age;
        	
        	 @Override
        	 public int compareTo(SomeOne that) {
        	 	 return ComparisonChain.start()
        	 	           .compare(this.age, that.age)
        	 	           .compare(this.name, that.name)
        	 	           .result();
        	 }
        }


                       Guaaaaaaaaaaaaaaaaaaaava!
18                                        Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.collect
     Range


        Range<Integer> range1 = Ranges.closed(1, 10);
        range1.apply(10);   // true

        Range<Integer> range2 = Ranges.open(1, 10);
        range2.apply(10);   // false




                     Guaaaaaaaaaaaaaaaaaaaava!
19                                     Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.io
                                                                      @Beta




         Guaaaaaaaaaaaaaaaaaaaava!
                         Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.io
                                                                      @Beta




         Guaaaaaaaaaaaaaaaaaaaava!
                         Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.io

     Subject 3.

           ファイルの内容を標準出力へ出力しましょう




                  Guaaaaaaaaaaaaaaaaaaaava!
21                                Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.io
     ByteStreams

              static long copy(InputStream from, OutputStream to)

              static byte[] toByteArray(InputStream in)



     CharStreams

              static long copy(Readable from, Appendable to)

              static String toString(Readable r)




                      Guaaaaaaaaaaaaaaaaaaaava!
22                                           Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.io
     Files

             static void touch(File file)

             static void copy(File from, File to)

             static void move(File from, File to)

             static BufferedReader newReader(File file, Charset charset)

             static BufferedWriter newWriter(File file, Charset charset)




                         Guaaaaaaaaaaaaaaaaaaaava!
23                                             Copyright © 2012 Akira Koyasu. Some rights reserved.
com.google.common.io
     Resources


        URL url = Resources.getResource(Sample.class, "test.txt");
        try {
        	 String str = Resources.toString(url, Charsets.UTF_8);
        	 System.out.println(str);
        } catch (IOException e) {
        	 e.printStackTrace();
        }



           これはなんとなく微妙・・・変わるかもしれません


                       Guaaaaaaaaaaaaaaaaaaaava!
24                                        Copyright © 2012 Akira Koyasu. Some rights reserved.
Conclusion




         Guaaaaaaaaaaaaaaaaaaaava!
                         Copyright © 2012 Akira Koyasu. Some rights reserved.
Conclusion




         Guaaaaaaaaaaaaaaaaaaaava!
                         Copyright © 2012 Akira Koyasu. Some rights reserved.
Conclusion

           Google + Java = Guava


           今日から使えるライブラリ
         コンパクトなAPI=学習コスト低
              開発効率は劇的に向上


        Happy programming with Guava!!


           Guaaaaaaaaaaaaaaaaaaaava!
26                         Copyright © 2012 Akira Koyasu. Some rights reserved.
Notes


     This work is licensed under the Creative Commons Attribution-
     NonCommercial 3.0 Unported License. To view a copy of this
     license, visit http://creativecommons.org/licenses/by-nc/3.0/.



     Page1 photo from:
       http://www.flickr.com/photos/hermansaksono/4297175782/




                      Guaaaaaaaaaaaaaaaaaaaava!
27                                       Copyright © 2012 Akira Koyasu. Some rights reserved.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Scala
ScalaScala
Scala
 
Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch? Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch?
 
Gpars concepts explained
Gpars concepts explainedGpars concepts explained
Gpars concepts explained
 
Scalding: Twitter's Scala DSL for Hadoop/Cascading
Scalding: Twitter's Scala DSL for Hadoop/CascadingScalding: Twitter's Scala DSL for Hadoop/Cascading
Scalding: Twitter's Scala DSL for Hadoop/Cascading
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
 
Functional programming in Java 8 - workshop at flatMap Oslo 2014
Functional programming in Java 8 - workshop at flatMap Oslo 2014Functional programming in Java 8 - workshop at flatMap Oslo 2014
Functional programming in Java 8 - workshop at flatMap Oslo 2014
 
concurrency with GPars
concurrency with GParsconcurrency with GPars
concurrency with GPars
 
Clojure And Swing
Clojure And SwingClojure And Swing
Clojure And Swing
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Groovy intro for OUDL
Groovy intro for OUDLGroovy intro for OUDL
Groovy intro for OUDL
 
Java 8 - Return of the Java
Java 8 - Return of the JavaJava 8 - Return of the Java
Java 8 - Return of the Java
 
Joy of scala
Joy of scalaJoy of scala
Joy of scala
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
 
Spark Schema For Free with David Szakallas
 Spark Schema For Free with David Szakallas Spark Schema For Free with David Szakallas
Spark Schema For Free with David Szakallas
 
Scalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsScalaz 8 vs Akka Actors
Scalaz 8 vs Akka Actors
 
Spark schema for free with David Szakallas
Spark schema for free with David SzakallasSpark schema for free with David Szakallas
Spark schema for free with David Szakallas
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
Scala vs java 8
Scala vs java 8Scala vs java 8
Scala vs java 8
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 

Ähnlich wie Hello, Guava !

Google Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG NantesGoogle Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG Nantes
mikaelbarbero
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
Guillaume Laforge
 

Ähnlich wie Hello, Guava ! (20)

Java is Container Ready - Vaibhav - Container Conference 2018
Java is Container Ready - Vaibhav - Container Conference 2018Java is Container Ready - Vaibhav - Container Conference 2018
Java is Container Ready - Vaibhav - Container Conference 2018
 
Java Cloud and Container Ready
Java Cloud and Container ReadyJava Cloud and Container Ready
Java Cloud and Container Ready
 
Guava & EMF
Guava & EMFGuava & EMF
Guava & EMF
 
Google Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG NantesGoogle Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG Nantes
 
Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497]
Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497]Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497]
Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497]
 
BeJUG Meetup - What's coming in the OSGi R7 Specification
BeJUG Meetup - What's coming in the OSGi R7 SpecificationBeJUG Meetup - What's coming in the OSGi R7 Specification
BeJUG Meetup - What's coming in the OSGi R7 Specification
 
"Quantum" Performance Effects
"Quantum" Performance Effects"Quantum" Performance Effects
"Quantum" Performance Effects
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
 
What's New in Groovy 1.6?
What's New in Groovy 1.6?What's New in Groovy 1.6?
What's New in Groovy 1.6?
 
JavaFX 2.1 - следующее поколение клиентской Java
JavaFX 2.1 - следующее поколение клиентской JavaJavaFX 2.1 - следующее поколение клиентской Java
JavaFX 2.1 - следующее поколение клиентской Java
 
Tales About Scala Performance
Tales About Scala PerformanceTales About Scala Performance
Tales About Scala Performance
 
Java, Up to Date
Java, Up to DateJava, Up to Date
Java, Up to Date
 
Sightly_techInsight
Sightly_techInsightSightly_techInsight
Sightly_techInsight
 
“Quantum” Performance Effects: beyond the Core
“Quantum” Performance Effects: beyond the Core“Quantum” Performance Effects: beyond the Core
“Quantum” Performance Effects: beyond the Core
 
Leveraging the Latest OSGi R7 Specifications - C Ziegeler & D Bosschaert
Leveraging the Latest OSGi R7 Specifications - C Ziegeler & D BosschaertLeveraging the Latest OSGi R7 Specifications - C Ziegeler & D Bosschaert
Leveraging the Latest OSGi R7 Specifications - C Ziegeler & D Bosschaert
 
Drupal.Behaviors
Drupal.BehaviorsDrupal.Behaviors
Drupal.Behaviors
 
Project Helidon Overview (Japanese)
Project Helidon Overview (Japanese)Project Helidon Overview (Japanese)
Project Helidon Overview (Japanese)
 
GraalVM Native Images by Oleg Selajev @shelajev
GraalVM Native Images by Oleg Selajev @shelajevGraalVM Native Images by Oleg Selajev @shelajev
GraalVM Native Images by Oleg Selajev @shelajev
 
Zero To Dojo
Zero To DojoZero To Dojo
Zero To Dojo
 
Java SE 8
Java SE 8Java SE 8
Java SE 8
 

Mehr von 輝 子安

Mehr von 輝 子安 (11)

Protractor under the hood
Protractor under the hoodProtractor under the hood
Protractor under the hood
 
そろそろLambda(CI/CD編)
そろそろLambda(CI/CD編)そろそろLambda(CI/CD編)
そろそろLambda(CI/CD編)
 
Dockerで構成するWebサービス 〜EmotionTechの場合〜
Dockerで構成するWebサービス 〜EmotionTechの場合〜Dockerで構成するWebサービス 〜EmotionTechの場合〜
Dockerで構成するWebサービス 〜EmotionTechの場合〜
 
Workshop: Docker on Elastic Beanstalk
Workshop: Docker on Elastic BeanstalkWorkshop: Docker on Elastic Beanstalk
Workshop: Docker on Elastic Beanstalk
 
PHP conference 2013 ja report
PHP conference 2013 ja reportPHP conference 2013 ja report
PHP conference 2013 ja report
 
Garbage Collection for Dummies
Garbage Collection for DummiesGarbage Collection for Dummies
Garbage Collection for Dummies
 
JavaOne Guide for the Petite Bourgeoisie
JavaOne Guide for the Petite BourgeoisieJavaOne Guide for the Petite Bourgeoisie
JavaOne Guide for the Petite Bourgeoisie
 
Java, Moving Forward
Java, Moving ForwardJava, Moving Forward
Java, Moving Forward
 
Java, Up to Date Sources
Java, Up to Date SourcesJava, Up to Date Sources
Java, Up to Date Sources
 
Hello, Guava ! samples
Hello, Guava ! samplesHello, Guava ! samples
Hello, Guava ! samples
 
Tokyo Cabinet & Tokyo Tyrant
Tokyo Cabinet & Tokyo TyrantTokyo Cabinet & Tokyo Tyrant
Tokyo Cabinet & Tokyo Tyrant
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Hello, Guava !

  • 2. What is Guava ? The Guava project contains several of Google's core libraries that we rely on in our Java-based projects: collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, and so forth.  http://code.google.com/p/guava-libraries/ GoogleによるJavaライブラリ Guaaaaaaaaaaaaaaaaaaaava! 2 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 3. Guava provides... •いつも書いている煩雑なコードを簡潔に書きやすく •使い勝手の良いユーティリティクラス •コンパクトなAPI 小さな悩みをすっきり解決。 あまり大きな悩みは解決してくれません。 Guaaaaaaaaaaaaaaaaaaaava! 3 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 4. How to use Maven dependency <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>11.0.2</version> </dependency> ... or Use other dependency managements, Download jar from the site Guaaaaaaaaaaaaaaaaaaaava! 4 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 5. Packages com.google.common.annotations com.google.common.base com.google.common.cache com.google.common.collect com.google.common.eventbus com.google.common.hash com.google.common.io com.google.common.math com.google.common.net com.google.common.primitives com.google.common.util.concurrent Guaaaaaaaaaaaaaaaaaaaava! 5 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 6. com.google.common.base Guaaaaaaaaaaaaaaaaaaaava! Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 7. com.google.common.base Guaaaaaaaaaaaaaaaaaaaava! Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 8. com.google.common.base Subject 0. プログラムの実行時間を測りましょう Subject 1. コストの高い処理の結果をキャッシュしましょう Guaaaaaaaaaaaaaaaaaaaava! 7 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 9. com.google.common.base 大切なインタフェース Function<F,T> T apply(F input) Predicate<T> boolean apply(T input) Supplier<T> T get() Guaaaaaaaaaaaaaaaaaaaava! 8 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 10. com.google.common.base Preconditions import static com.google.common.base.Preconditions.*; ... public void someMethod(int pos) { checkArgument(pos > 0); } posが0以下の場合はIllegalArgumentExceptionが スローされます Guaaaaaaaaaaaaaaaaaaaava! 9 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 11. com.google.common.base Joiner String[] strs = { "taro", "jiro", "saburo" }; String str = Joiner.on(",").join(strs); Splitter String str = "taro,jiro,saburo"; Iterable<String> strs = Splitter.on(",").split(str); Guaaaaaaaaaaaaaaaaaaaava! 10 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 12. com.google.common.base CaseFormat LOWER_HYPHEN LOWER_UNDERSCORE LOWER_CAMEL UPPER_CAMEL UPPER_UNDERSCORE import static com.google.common.base.CaseFormat.*; LOWER_UNDERSCORE.to(LOWER_CAMEL, "create_date"); // createDate UPPER_CAMEL.to(LOWER_UNDERSCORE, "SomeName"); // some_name Guaaaaaaaaaaaaaaaaaaaava! 11 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 13. com.google.common.base Stopwatch Stopwatch stopwatch = new Stopwatch().start(); stopwatch.stop(); System.out.printf("time: %s%n", stopwatch); Charsets Charset charset = Charsets.UTF_8; Guaaaaaaaaaaaaaaaaaaaava! 12 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 14. com.google.common.collect Guaaaaaaaaaaaaaaaaaaaava! Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 15. com.google.common.collect Guaaaaaaaaaaaaaaaaaaaava! Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 16. com.google.common.collect Subject 2. あるリストを加工して別のリストを作りましょう Guaaaaaaaaaaaaaaaaaaaava! 14 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 17. com.google.common.collect 頻出メソッド Collection<E> filter(Collection<E> unfiltered, Predicate<? super E> predicate) Collection<T> transform(Collection<F> fromCollection, Function<? super F,T> function) Guaaaaaaaaaaaaaaaaaaaava! 15 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 18. com.google.common.collect Lists List<String> list1 = Lists.newArrayList(); List<String> list2 = Lists.newArrayListWithCapacity(100); Maps Map<Integer, String> map1 = Maps.newHashMap(); Map<Integer, String> map2 = Maps.newHashMapWithExpectedSize(100); Guaaaaaaaaaaaaaaaaaaaava! 16 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 19. com.google.common.collect ForwardingCollection List<String> list = new ForwardingList<String>() { @Override protected List<String> delegate() { return backingList; } }; ImmutableCollection ImmutableSet<String> set =ImmutableSet.<String>builder() .add("taro") .add("jiro") .add("saburo").build(); Guaaaaaaaaaaaaaaaaaaaava! Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 20. com.google.common.collect ComparisonChain public class SomeOne implements Comparable<SomeOne> { private String name; private int age; @Override public int compareTo(SomeOne that) { return ComparisonChain.start() .compare(this.age, that.age) .compare(this.name, that.name) .result(); } } Guaaaaaaaaaaaaaaaaaaaava! 18 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 21. com.google.common.collect Range Range<Integer> range1 = Ranges.closed(1, 10); range1.apply(10); // true Range<Integer> range2 = Ranges.open(1, 10); range2.apply(10); // false Guaaaaaaaaaaaaaaaaaaaava! 19 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 22. com.google.common.io @Beta Guaaaaaaaaaaaaaaaaaaaava! Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 23. com.google.common.io @Beta Guaaaaaaaaaaaaaaaaaaaava! Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 24. com.google.common.io Subject 3. ファイルの内容を標準出力へ出力しましょう Guaaaaaaaaaaaaaaaaaaaava! 21 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 25. com.google.common.io ByteStreams static long copy(InputStream from, OutputStream to) static byte[] toByteArray(InputStream in) CharStreams static long copy(Readable from, Appendable to) static String toString(Readable r) Guaaaaaaaaaaaaaaaaaaaava! 22 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 26. com.google.common.io Files static void touch(File file) static void copy(File from, File to) static void move(File from, File to) static BufferedReader newReader(File file, Charset charset) static BufferedWriter newWriter(File file, Charset charset) Guaaaaaaaaaaaaaaaaaaaava! 23 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 27. com.google.common.io Resources URL url = Resources.getResource(Sample.class, "test.txt"); try { String str = Resources.toString(url, Charsets.UTF_8); System.out.println(str); } catch (IOException e) { e.printStackTrace(); } これはなんとなく微妙・・・変わるかもしれません Guaaaaaaaaaaaaaaaaaaaava! 24 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 28. Conclusion Guaaaaaaaaaaaaaaaaaaaava! Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 29. Conclusion Guaaaaaaaaaaaaaaaaaaaava! Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 30. Conclusion Google + Java = Guava 今日から使えるライブラリ コンパクトなAPI=学習コスト低 開発効率は劇的に向上 Happy programming with Guava!! Guaaaaaaaaaaaaaaaaaaaava! 26 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 31. Notes This work is licensed under the Creative Commons Attribution- NonCommercial 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/. Page1 photo from: http://www.flickr.com/photos/hermansaksono/4297175782/ Guaaaaaaaaaaaaaaaaaaaava! 27 Copyright © 2012 Akira Koyasu. Some rights reserved.

Hinweis der Redaktion

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n