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 {
9
10 p u b l i c s t a t i c v o i d main(String[] args) {
11
12 // システム時間
13 l o n g time1 = System. currentTimeMillis ();
14
15 l o n g sum = 0;
16 f o r (i n t i = 0; i < Integer. MAX_VALUE ; i++) {
i
17 sum += i;
18 }
19
20 l o n g time2 = System. currentTimeMillis ();
21
22 System. out .printf("sum: %d%n", sum);
23 // 引き算をします。
24 System. 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 {
11
12 p u b l i c s t a t i c v o i d main(String[] args) {
13
14 // ストップウォッチ!!
15 Stopwatch stopwatch = n e w Stopwatch().start();
16
17 l o n g sum = 0;
18 f o r (i n t i = 0; i < Integer. MAX_VALUE ; i++) {
i
19 sum += i;
20 }
21
22 stopwatch.stop();
23
24 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 koyasu
11 */
12 p u b l i c c l a s s Sample1A {
13
14 p u b l i c s t a t i c v o i d main(String[] args) {
15
16 Stopwatch stopwatch = n e w Stopwatch().start();
17
18 BigInteger[] f;
19 f = getFibonacci ();
20 f = getFibonacci ();
21 f = getFibonacci ();
22
23 System. out .println(f[f.length - 1]);
24
25 System. out .printf("time: %s%n", stopwatch);
26 }
27
28 /**
29 * 20000個のフィボナッチ数を返す
30 * @return
31 */
32 p r i v a t e s t a t i c BigInteger[] getFibonacci() {
33 f i n a l i n t n = 20000;
34 BigInteger[] f = n e w BigInteger[n];
35
36 f[0] = BigInteger. ZERO ;
37 f[1] = BigInteger. ONE ;
38 f o r (i n t i = 0; i < n - 2; i++) {
i
39 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 }
45
Page 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 * JDK版
10 * @ a u t h o r koyasu
11 */
12 p u b l i c c l a s s Sample1B {
13
14 p u b l i c s t a t i c v o i d main(String[] args) {
15
16 Stopwatch stopwatch = n e w Stopwatch().start();
17
18 BigInteger[] f;
19 f = FibonacciCalculator. get ();
20 f = FibonacciCalculator. get ();
21 f = FibonacciCalculator. get ();
22
23 System. out .println(f[f.length - 1]);
24
25 System. out .printf("time: %s%n", stopwatch);
26 }
27
28 /**
29 * 20000個のフィボナッチ数を返すクラス
30 * @return
31 */
32 p r i v a t e s t a t i c c l a s s FibonacciCalculator {
33 p r i v a t e s t a t i c BigInteger[] f ;
34
35 p r i v a t e s t a t i c BigInteger[] get() {
36 // キャッシュを自分で書くのが、良くない
37 i f ( f == n u l l ) {
38 s y n c h r o n i z e d (FibonacciCalculator.c l a s s ) {
c
39 i f ( f == n u l l ) {
40 f i n a l i n t n = 20000;
41 f = n e w BigInteger[n];
42
43 f [0] = BigInteger. ZERO ;
44 f [1] = BigInteger. ONE ;
45 f o r (i n t i = 0; i < n - 2; i++) {
i
46 f [i+2] = f [i].add( f [i+1]);
47 }
48 System. out .println("Calc fibonacci complete.");
49 }
50 }
51 }
52 return 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 * Guava版
12 * @ a u t h o r koyasu
13 */
14 p u b l i c c l a s s Sample1C {
15
16 p u b l i c s t a t i c v o i d main(String[] args) {
17
18 Stopwatch stopwatch = n e w Stopwatch().start();
19
20 BigInteger[] f;
21 f = fibonacciSupplier .get();
22 f = fibonacciSupplier .get();
23 f = fibonacciSupplier .get();
24
25 System. out .println(f[f.length - 1]);
26
27 System. out .printf("time: %s%n", stopwatch);
28 }
29
30 // 初回の計算結果をキャッシュするサプライヤー
31 p r i v a t e s t a t i c Supplier<BigInteger[]> fibonacciSupplier
32 = Suppliers. memoize (n e w Supplier<BigInteger[]>() {
n
33 p u b l i c BigInteger[] get() {
34 f i n a l i n t n = 20000;
35 BigInteger[] f = n e w BigInteger[n];
36
37 f[0] = BigInteger. ZERO ;
38 f[1] = BigInteger. ONE ;
39 f o r (i n t i = 0; i < n - 2; i++) {
i
40 f[i+2] = f[i].add(f[i+1]);
41 }
42 System. out .println("Calc fibonacci complete.");
43 r e t u r n f;
44 }
45 });
46 }
47
Page 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;
10
11 /**
12 * Subject 2. あるリストを加工して別のリストを作りましょう
13 * JDK版
14 * @ a u t h o r koyasu
15 */
16 p u b l i c c l a s s Sample2A {
17
18 // 元のリスト
19 @SuppressWarnings("serial")
20 p r i v a t e s t a t i c List<Integer> srcList = n e w ArrayList<Integer>(){{
21 f o r (i n t i = 0; i < 1000; i++) {
i
22 add(Integer. valueOf (i));
23 }
24 }};
25
26 // マスタマップ(不変にする)
27 @SuppressWarnings("serial")
28 p r i v a t e s t a t i c Map<Integer, String> map = Collections. unmodifiableMap (
29 n e w HashMap<Integer, String>(){{
30 f o r (i n t i = 0; i < 10000; i++) {
i
31 put(Integer. valueOf (i), "[" + i + "]の値");
32 }
33 }});
34
35 p u b l i c s t a t i c v o i d main(String[] args) {
36 System. out .printf("src[0]: %s%n", srcList .get(0));
37 System. out .printf("map[0]: %s%n", map .entrySet().iterator().next());
38
39 Stopwatch stopwatch = n e w Stopwatch().start();
40
41 List<String> dstList = n e w ArrayList<String>( srcList .size());
42 // ループして変換する
43 f o r (Integer src : srcList ) {
44 String val = map .get(src);
45 dstList.add(val);
46 }
47
48 stopwatch.stop();
49
50 System. out .printf("dst[0]: %s%n", dstList.get(0));
51 System. out .printf("time: %s%n", stopwatch);
52 }
53 }
54
Page 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;
13
14 /**
15 * Subject 2. あるリストを加工して別のリストを作りましょう
16 * Guava版
17 * @ a u t h o r koyasu
18 */
19 p u b l i c c l a s s Sample2B {
20
21 // 元のリスト
22 @SuppressWarnings("serial")
23 p r i v a t e s t a t i c List<Integer> srcList = n e w ArrayList<Integer>(){{
24 f o r (i n t i = 0; i < 1000; i++) {
i
25 add(Integer. valueOf (i));
26 }
27 }};
28
29 // マスタマップ(不変にする)
30 p r i v a t e s t a t i c f i n a l Map<Integer, String> map ;
31 static {
32 ImmutableMap.Builder<Integer, String> builder
33 = ImmutableMap. builder ();
34 f o r (i n t i = 0; i < 10000; i++) {
i
35 builder.put(Integer. valueOf (i), "[" + i + "]の値");
36 }
37 map = builder.build();
38 }
39
40 p u b l i c s t a t i c v o i d main(String[] args) {
41 System. out .printf("src[0]: %s%n", srcList .get(0));
42 System. out .printf("map[0]: %s%n", Iterables. get ( map .entrySet(), 0));
43
44 Stopwatch stopwatch = n e w Stopwatch().start();
45
46 // 変換関数で変換
47 List<String> dstList = Lists. transform (
48 srcList , n e w Function<Integer, String>(){
49 @Override
50 p u b l i c String apply(Integer input) {
51 r e t u r n map .get(input);
52 }});
53
54 stopwatch.stop();
55
56 System. out .printf("dst[0]: %s%n", dstList.get(0));
57 System. out .printf("time: %s%n", stopwatch);
58
59 // 運が良ければこんな方法もある
60 List<String> dstList2 = Lists. transform (
61 srcList , Functions. forMap ( map ));
62
63 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版 旧io
12 * @ a u t h o r koyasu
13 */
14 p u b l i c c l a s s Sample3A {
15
16 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";
18
19 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));
n
23 // バッファを用意する
24 b y t e [] buf = n e w b y t e [0x1000];
25 i 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();
42
43 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;
10
11 /**
12 * Subject 3. ファイルの内容を標準出力へ出力しましょう
13 * JDK版 nio
14 * @ a u t h o r koyasu
15 */
16 p u b l i c c l a s s Sample3B {
17
18 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";
20
21 Stopwatch stopwatch = n e w Stopwatch().start();
22
23 FileChannel channel = n u l l ;
24 try {
25 // ファイルチャネルを使う(悪くない)
26 channel = n e w FileInputStream(path).getChannel();
27 channel.transferTo(0, channel.size(),
28 Channels. newChannel (System. out ));
29
30 } 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 }
43
44
45 stopwatch.stop();
46
47 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 * Guava版
12 * @ a u t h o r koyasu
13 */
14 p u b l i c c l a s s Sample3C {
15
16 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";
18
19 Stopwatch stopwatch = n e w Stopwatch().start();
20 try {
21 // コピー!!
22 Files. copy (n e w File(path), System. out );
n
23 } c a t c h (IOException e) {
24 e.printStackTrace();
25 }
26 stopwatch.stop();
27
28 System. out .printf("time: %s%n", stopwatch);
29 }
30 }
31
Page 10
12. 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/.