This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ch03] Calculator with Template/Callback.
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package ch03.springbook.calculator; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
public class Calculator { | ||
|
||
public Integer calcSum(String filePath) throws IOException { | ||
|
||
return lineReadTemplate( | ||
filePath, | ||
new LineCallBack() { | ||
@Override | ||
public Integer doSomethingWithLine(String line, Integer value) { | ||
return value + Integer.valueOf(line); | ||
} | ||
}, | ||
0 | ||
); | ||
} | ||
|
||
public Integer calcMultiply(String filePath) throws IOException { | ||
|
||
return lineReadTemplate( | ||
filePath, | ||
new LineCallBack() { | ||
@Override | ||
public Integer doSomethingWithLine(String line, Integer value) { | ||
return value * Integer.valueOf(line); | ||
} | ||
}, | ||
1 | ||
); | ||
} | ||
|
||
public Integer lineReadTemplate(String filePath, LineCallBack lineCallBack, int initVal) throws IOException { | ||
|
||
BufferedReader br = null; | ||
|
||
try { | ||
|
||
br = new BufferedReader(new FileReader(filePath)); | ||
|
||
Integer res = initVal; | ||
String line = null; | ||
|
||
while ((line = br.readLine()) != null) { | ||
res = lineCallBack.doSomethingWithLine(line, res); | ||
} | ||
|
||
return res; | ||
|
||
} catch (IOException e) { | ||
throw e; | ||
|
||
} finally { | ||
|
||
if (br != null) { | ||
try { | ||
br.close(); | ||
} catch (IOException e) { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ch03.springbook.calculator; | ||
|
||
public interface LineCallBack { | ||
Integer doSomethingWithLine(String line, Integer value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1 | ||
2 | ||
3 | ||
4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ch03.springbook.calculator; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class CalcSumTest { | ||
|
||
private Calculator calculator; | ||
private String numFilePath; | ||
|
||
@Before | ||
public void setUp() { | ||
this.calculator = new Calculator(); | ||
this.numFilePath = getClass().getResource("/numbers.txt").getPath(); | ||
} | ||
|
||
@Test | ||
public void sumOfNumbers() throws IOException { | ||
assertThat(calculator.calcSum(numFilePath), is(10)); | ||
} | ||
|
||
@Test | ||
public void multiplyOfNumbers() throws IOException { | ||
assertThat(calculator.calcMultiply(numFilePath), is(24)); | ||
} | ||
} |