ソースを参照

Merge a0205240fff870968ec27c640523c44f80dc2971 into 99c0c83b2f011a8e58c86fca53df6bfa67c86565

vvg3 6 年 前
コミット
b2bea7b518
コミット者のEメールアドレスに関連付けられたアカウントが存在しません
共有3 個のファイルを変更した5435 個の追加6 個の削除を含む
  1. 42
    6
      src/main/java/HamletParser.java
  2. 10
    0
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

+ 42
- 6
src/main/java/HamletParser.java ファイルの表示

@@ -1,6 +1,8 @@
1 1
 import java.io.File;
2 2
 import java.io.IOException;
3 3
 import java.util.Scanner;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4 6
 
5 7
 /**
6 8
  * Created by thook on 10/7/15.
@@ -8,31 +10,65 @@ import java.util.Scanner;
8 10
 public class HamletParser {
9 11
 
10 12
     private String hamletData;
13
+    private Pattern pattern;
14
+    private Matcher matcher;
11 15
 
12
-    public HamletParser(){
16
+    public HamletParser() {
13 17
         this.hamletData = loadFile();
14 18
     }
15 19
 
16
-    private String loadFile(){
20
+    public void changeHamletToLeon() {
21
+        String hamlet = "(Hamlet|HAMLET)";
22
+        this.hamletData = change(hamlet, "LEON");
23
+    }
24
+
25
+    public void changeHoratioToTariq() {
26
+        String horatio = "(Horatio|HORATIO)";
27
+        this.hamletData = change(horatio, "TARIQ");
28
+    }
29
+
30
+    public boolean findHoratio() {
31
+        String horatio = "(Horatio|HORATIO)";
32
+        return find(horatio);
33
+    }
34
+
35
+    public boolean findHamlet() {
36
+        String hamlet = "(Hamlet|HAMLET)";
37
+        return find(hamlet);
38
+    }
39
+
40
+    private boolean find(String aString) {
41
+        pattern = Pattern.compile(aString);
42
+        matcher = pattern.matcher(hamletData);
43
+        return matcher.find();
44
+    }
45
+
46
+    private String change(String original, String convertTo) {
47
+        pattern = Pattern.compile(original);
48
+        matcher = pattern.matcher(hamletData);
49
+        return matcher.replaceAll(convertTo);
50
+    }
51
+
52
+    private String loadFile() {
17 53
         ClassLoader classLoader = getClass().getClassLoader();
18 54
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19 55
         StringBuilder result = new StringBuilder("");
20 56
 
21
-        try(Scanner scanner = new Scanner(file)){
22
-            while(scanner.hasNextLine()){
57
+        try (Scanner scanner = new Scanner(file)) {
58
+            while (scanner.hasNextLine()) {
23 59
                 String line = scanner.nextLine();
24 60
                 result.append(line).append("\n");
25 61
             }
26 62
 
27 63
             scanner.close();
28
-        }catch(IOException e){
64
+        } catch (IOException e) {
29 65
             e.printStackTrace();
30 66
         }
31 67
 
32 68
         return result.toString();
33 69
     }
34 70
 
35
-    public String getHamletData(){
71
+    public String getHamletData() {
36 72
         return hamletData;
37 73
     }
38 74
 

+ 10
- 0
src/test/java/HamletParserTest.java ファイルの表示

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
@@ -15,17 +16,26 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        hamletParser.changeHamletToLeon();
20
+        System.out.println(hamletParser.getHamletData());
21
+        Assert.assertFalse(hamletParser.findHamlet());
18 22
     }
19 23
 
20 24
     @Test
21 25
     public void testChangeHoratioToTariq() {
26
+        hamletParser.changeHoratioToTariq();
27
+        System.out.println(hamletParser.getHamletData());
28
+        Assert.assertFalse(hamletParser.findHoratio());
22 29
     }
23 30
 
24 31
     @Test
25 32
     public void testFindHoratio() {
33
+        Assert.assertTrue(hamletParser.findHoratio());
34
+
26 35
     }
27 36
 
28 37
     @Test
29 38
     public void testFindHamlet() {
39
+        Assert.assertTrue(hamletParser.findHoratio());
30 40
     }
31 41
 }

+ 5383
- 0
target/classes/hamlet.txt
ファイル差分が大きすぎるため省略します
ファイルの表示