Hello, Guava ! samples

  • Published on
    21-May-2015

  • View
    954

  • Download
    1

Embed Size (px)

DESCRIPTION

Sample codes of the presentation "Hello, Guava !" Its presentation is here. http://www.slideshare.net/akirakoyasu/hello-guava

Transcript

  • 1. Guava-samplesSubject 0. Sample0AJDK Page 1Sample0BGuava Page 2Subject 1. Sample1A Page 3Sample1BJDK Page 4Sample1CGuava Page 5Subject 2. Sample2AJDK Page 6Sample2BGuava Page 7Subject 3. Sample3AJDK io Page 8Sample3BJDK nio Page 9Sample3CGuava Page10Copyright 2012 Akira Koyasu Some rights reserved.

2. Sample0A.java 1 p a c k a g e guava.subject0; 2 3 /** 4 * Subject 0. 5 * JDK 6 * @ a u t h o r koyasu 7 */ 8 p u b l i c c l a s s Sample0A { 910 p u b l i c s t a t i c v o i d main(String[] args) {1112 // 13l o n g time1 = System. currentTimeMillis ();1415l o n g sum = 0;16f o r (i n t i = 0; i < Integer. MAX_VALUE ; i++) {i17 sum += i;18}1920l o n g time2 = System. currentTimeMillis ();2122System. out .printf("sum: %d%n", sum);23// 24System. out .printf("time: %d ms%n", time2 - time1);25}26 }27 Page 1 3. Sample0B.java 1 p a c k a g e guava.subject0; 2 3 i m p o r t com.google.common.base.Stopwatch; 4 5 /** 6* Subject 0. 7 * Guava 8 * @ a u t h o r koyasu 9 */10 p u b l i c c l a s s Sample0B {1112 p u b l i c s t a t i c v o i d main(String[] args) {1314 // 15 Stopwatch stopwatch = n e w Stopwatch().start();1617 l o n g sum = 0;18 f o r (i n t i = 0; i < Integer. MAX_VALUE ; i++) { i19sum += i;20 }2122 stopwatch.stop();2324 System. out .printf("sum: %d%n", sum);25 System. out .printf("time: %s%n", stopwatch);26}27 }28 Page 2 4. Sample1A.java 1 p a c k a g e guava.subject1; 2 3 i m p o r t java.math.BigInteger; 4 5 i m p o r t com.google.common.base.Stopwatch; 6 7 /** 8* Subject 1. 9 * 10 * @ a u t h o r koyasu11 */12 p u b l i c c l a s s Sample1A {1314 p u b l i c s t a t i c v o i d main(String[] args) {1516 Stopwatch stopwatch = n e w Stopwatch().start();1718 BigInteger[] f;19 f = getFibonacci ();20 f = getFibonacci ();21 f = getFibonacci ();2223 System. out .println(f[f.length - 1]);2425 System. out .printf("time: %s%n", stopwatch);26 }2728 /**29 * 2000030* @return31*/32p r i v a t e s t a t i c BigInteger[] getFibonacci() {33f i n a l i n t n = 20000;34BigInteger[] f = n e w BigInteger[n];3536 f[0] = BigInteger. ZERO ;37 f[1] = BigInteger. ONE ;38 f o r (i n t i = 0; i < n - 2; i++) { i39 f[i + 2] = f[i].add(f[i + 1]);40 }41 System. out .println("Calc fibonacci complete.");42 r e t u r n f;43}44 }45Page 3 5. Sample1B.java 1 p a c k a g e guava.subject1; 2 3 i m p o r t java.math.BigInteger; 4 5 i m p o r t com.google.common.base.Stopwatch; 6 7 /** 8* Subject 1. 9 * JDK10 * @ a u t h o r koyasu11 */12 p u b l i c c l a s s Sample1B {1314 p u b l i c s t a t i c v o i d main(String[] args) {1516 Stopwatch stopwatch = n e w Stopwatch().start();1718 BigInteger[] f;19 f = FibonacciCalculator. get ();20 f = FibonacciCalculator. get ();21 f = FibonacciCalculator. get ();2223 System. out .println(f[f.length - 1]);2425 System. out .printf("time: %s%n", stopwatch);26 }2728 /**29 * 2000030* @return31*/32p r i v a t e s t a t i c c l a s s FibonacciCalculator {33p r i v a t e s t a t i c BigInteger[] f ;3435 p r i v a t e s t a t i c BigInteger[] get() {36 // 37i f ( f == n u l l ) {38s y n c h r o n i z e d (FibonacciCalculator.c l a s s ) { c39i f ( f == n u l l ) {40f i n a l i n t n = 20000;41f = n e w BigInteger[n];4243 f [0] = BigInteger. ZERO ;44 f [1] = BigInteger. ONE ;45 f o r (i n t i = 0; i < n - 2; i++) { i46 f [i+2] = f [i].add( f [i+1]);47 }48 System. out .println("Calc fibonacci complete.");49 }50}51}52return f;53 }54}55 }56 Page 4 6. Sample1C.java 1 p a c k a g e guava.subject1; 2 3 i m p o r t java.math.BigInteger; 4 5 i m p o r t com.google.common.base.Stopwatch; 6 i m p o r t com.google.common.base.Supplier; 7 i m p o r t com.google.common.base.Suppliers; 8 9 /**10* Subject 1. 11 * Guava12 * @ a u t h o r koyasu13 */14 p u b l i c c l a s s Sample1C {1516 p u b l i c s t a t i c v o i d main(String[] args) {1718 Stopwatch stopwatch = n e w Stopwatch().start();1920 BigInteger[] f;21 f = fibonacciSupplier .get();22 f = fibonacciSupplier .get();23 f = fibonacciSupplier .get();2425 System. out .println(f[f.length - 1]);2627 System. out .printf("time: %s%n", stopwatch);28 }2930 // 31p r i v a t e s t a t i c Supplier fibonacciSupplier32= Suppliers. memoize (n e w Supplier() { n33p u b l i c BigInteger[] get() {34f i n a l i n t n = 20000;35BigInteger[] f = n e w BigInteger[n];3637f[0] = BigInteger. ZERO ;38f[1] = BigInteger. ONE ;39f o r (i n t i = 0; i < n - 2; i++) {i40f[i+2] = f[i].add(f[i+1]);41}42System. out .println("Calc fibonacci complete.");43r e t u r n f;44}45});46 }47Page 5 7. Sample2A.java 1 p a c k a g e guava.subject2; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 i m p o r t com.google.common.base.Stopwatch;1011 /**12* Subject 2. 13 * JDK14 * @ a u t h o r koyasu15 */16 p u b l i c c l a s s Sample2A {1718 // 19@SuppressWarnings("serial")20p r i v a t e s t a t i c List srcList = n e w ArrayList(){{21f o r (i n t i = 0; i < 1000; i++) {i22add(Integer. valueOf (i));23}24}};2526// 27@SuppressWarnings("serial")28p r i v a t e s t a t i c Map map = Collections. unmodifiableMap (29n e w HashMap(){{30f o r (i n t i = 0; i < 10000; i++) {i31put(Integer. valueOf (i), "[" + i + "]");32 }33}});3435p u b l i c s t a t i c v o i d main(String[] args) {36System. out .printf("src[0]: %s%n", srcList .get(0));37System. out .printf("map[0]: %s%n", map .entrySet().iterator().next());3839 Stopwatch stopwatch = n e w Stopwatch().start();4041 List dstList = n e w ArrayList( srcList .size());42 // 43 f o r (Integer src : srcList ) {44 String val = map .get(src);45 dstList.add(val);46 }4748 stopwatch.stop();4950 System. out .printf("dst[0]: %s%n", dstList.get(0));51 System. out .printf("time: %s%n", stopwatch);52}53 }54Page 6 8. Sample2B.java 1 p a c k a g e guava.subject2; 2 3 i m p o r t java.util.ArrayList; 4 i m p o r t java.util.List; 5 i m p o r t java.util.Map; 6 7 import com.google.common.base.Function; 8 import com.google.common.base.Functions; 9 import com.google.common.base.Stopwatch;10 import com.google.common.collect.ImmutableMap;11 import com.google.common.collect.Iterables;12 import com.google.common.collect.Lists;1314 /**15* Subject 2. 16 * Guava17 * @ a u t h o r koyasu18 */19 p u b l i c c l a s s Sample2B {2021 // 22@SuppressWarnings("serial")23p r i v a t e s t a t i c List srcList = n e w ArrayList(){{24f o r (i n t i = 0; i < 1000; i++) {i25add(Integer. valueOf (i));26}27}};2829// 30p r i v a t e s t a t i c f i n a l Map map ;31static {32ImmutableMap.Builder builder33= ImmutableMap. builder ();34f o r (i n t i = 0; i < 10000; i++) {i35builder.put(Integer. valueOf (i), "[" + i + "]");36 }37 map = builder.build();38}3940p u b l i c s t a t i c v o i d main(String[] args) {41System. out .printf("src[0]: %s%n", srcList .get(0));42System. out .printf("map[0]: %s%n", Iterables. get ( map .entrySet(), 0));4344 Stopwatch stopwatch = n e w Stopwatch().start();4546 // 47 List dstList = Lists. transform (48 srcList , n e w Function(){49 @Override50 p u b l i c String apply(Integer input) {51 r e t u r n map .get(input);52 }});5354 stopwatch.stop();5556 System. out .printf("dst[0]: %s%n", dstList.get(0));57 System. out .printf("time: %s%n", stopwatch);5859 // 60 List dstList2 = Lists. transform (61 srcList , Functions. forMap ( map ));6263 System. out .println();64 System. out .printf("dst2[0]: %s%n", dstList2.get(0));65}66 }Page 7 9. Sample3A.java 1 p a c k a g e guava.subject3; 2 3 i m p o r t java.io.BufferedInputStream; 4 i m p o r t java.io.FileInputStream; 5 i m p o r t java.io.IOException; 6 7 i m p o r t com.google.common.base.Stopwatch; 8 9 /**10* Subject 3. 11 * JDK io12 * @ a u t h o r koyasu13 */14 p u b l i c c l a s s Sample3A {1516 p u b l i c s t a t i c v o i d main(String[] args) {17 String path = "/path/to/file.txt";1819 Stopwatch stopwatch = n e w Stopwatch().start();20 BufferedInputStream is = n u l l ;21 try {22 is = n e w BufferedInputStream(n e w FileInputStream(path)); n23 // 24b y t e [] buf = n e w b y t e [0x1000];25i n t len;26// 27 w h i l e ((len = is.read(buf)) >= 0) {28 System. out .write(buf, 0, len);29 }30 } c a t c h (IOException e) {31 e.printStackTrace();32 } finally {33 i f (is != n u l l ) {34 try {35 is.close();36 } c a t c h (IOException e) {37 e.printStackTrace();38 }39 }40 }41 stopwatch.stop();4243 System. out .printf("time: %s%n", stopwatch);44}45 }46 Page 8 10. Sample3B.java 1 p a c k a g e guava.subject3; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.nio.channels.Channels; 7 import java.nio.channels.FileChannel; 8 9 i m p o r t com.google.common.base.Stopwatch;1011 /**12* Subject 3. 13 * JDK nio14 * @ a u t h o r koyasu15 */16 p u b l i c c l a s s Sample3B {1718 p u b l i c s t a t i c v o i d main(String[] args) {19 String path = "/path/to/file.txt";2021 Stopwatch stopwatch = n e w Stopwatch().start();2223 FileChannel channel = n u l l ;24 try {25 // 26channel = n e w FileInputStream(path).getChannel();27channel.transferTo(0, channel.size(),28Channels. newChannel (System. out ));2930 } c a t c h (FileNotFoundException e) {31 e.printStackTrace();32 } c a t c h (IOException e) {33 e.printStackTrace();34 } finally {35 i f (channel != n u l l ) {36 try {37 channel.close();38 } c a t c h (IOException e) {39 e.printStackTrace();40 }41 }42 }434445 stopwatch.stop();4647 System. out .printf("time: %s%n", stopwatch);48}49 }50 Page 9 11. Sample3C.java 1 p a c k a g e guava.subject3; 2 3 i m p o r t java.io.File; 4 i m p o r t java.io.IOException; 5 6 i m p o r t com.google.common.base.Stopwatch; 7 i m p o r t com.google.common.io.Files; 8 9 /**10* Subject 3. 11 * Guava12 * @ a u t h o r koyasu13 */14 p u b l i c c l a s s Sample3C {1516 p u b l i c s t a t i c v o i d main(String[] args) {17 String path = "/path/to/file.txt";1819 Stopwatch stopwatch = n e w Stopwatch().start();20 try {21 // 22 Files. copy (n e w File(path), System. out );n23 } c a t c h (IOException e) {24 e.printStackTrace();25 }26 stopwatch.stop();2728 System. out .printf("time: %s%n", stopwatch);29}30 }31Page 10 12. NotesThis work is licensed under the Creative Commons Attribution-NonCommercial 3.0 UnportedLicense. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.