Contenu connexe Similaire à 20150302 java8 第一回_ラムダ式(1) Similaire à 20150302 java8 第一回_ラムダ式(1) (20) Plus de Appresso Engineering Team Plus de Appresso Engineering Team (12) 20150302 java8 第一回_ラムダ式(1)4. Java 8 導入予定
• Thunderbus
• 1.0 で導入済(8u31)
• PIMSYNC
• 2.2 で導入予定
• DataSpider Servista
• 4.0 で導入予定
5. Java 8 の新機能
• ラムダ式
• Stream API
• Date and Time API
• JavaFX 8
• etc…
6. Java 8 の新機能
• ラムダ式
• Stream API
• Date and Time API
• JavaFX 8
• etc…
14. 匿名関数とは
• 匿名の関数
• 要するにこれのこと
List<Integer> numbers = Arrays.asList(1, 1, 2, 3, 5);
Collections.sort(numbers, new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return i2 - i1;
}
});
17. Excel アダプタのテスト
コードの例(Before)
• こういうのも
private interface Assertion {
public void call(Sheet expectedSheet, Sheet actualSheet, int row, int column) throws Exception;
}
private void assertDataAndFormat(String expectedExcelFileName, String sheetName,
int startRowIndex, int startColumnIndex, int endRowIndex, int endColumnIndex) throws Exception {
assertWithExpectedExcelFile(
expectedExcelFileName, sheetName, startRowIndex, startColumnIndex, endRowIndex, endColumnIndex,
new Assertion() {
@Override
public void call(Sheet expectedSheet, Sheet actualSheet, int row, int column) throws Exception {
Cell expetedCell = expectedSheet.getCell(column, row);
Cell actualCell = actualSheet.getCell(column, row);
assertEquals(expetedCell.getContents(), actualCell.getContents());
assertEquals(expetedCell.getType().toString(), actualCell.getType().toString());
assertEquals(expetedCell.getCellFormat().getFormat().getFormatString(),
actualCell.getCellFormat().getFormat().getFormatString());
AssertUtil.assertCellFormatByJExcelApi(
expetedCell.getCellFormat(), actualCell.getCellFormat());
}
}
);
}
18. Excel アダプタのテスト
コードの例(Before)
• こういうのも
private interface Assertion {
public void call(Sheet expectedSheet, Sheet actualSheet, int row, int column) throws Exception;
}
private void assertDataAndFormat(String expectedExcelFileName, String sheetName,
int startRowIndex, int startColumnIndex, int endRowIndex, int endColumnIndex) throws Exception {
assertWithExpectedExcelFile(
expectedExcelFileName, sheetName, startRowIndex, startColumnIndex, endRowIndex, endColumnIndex,
new Assertion() {
@Override
public void call(Sheet expectedSheet, Sheet actualSheet, int row, int column)
throws Exception {
Cell expetedCell = expectedSheet.getCell(column, row);
Cell actualCell = actualSheet.getCell(column, row);
assertEquals(expetedCell.getContents(), actualCell.getContents());
assertEquals(expetedCell.getType().toString(), actualCell.getType().toString());
assertEquals(expetedCell.getCellFormat().getFormat().getFormatString(),
actualCell.getCellFormat().getFormat().getFormatString());
AssertUtil.assertCellFormatByJExcelApi(
expetedCell.getCellFormat(), actualCell.getCellFormat());
}
}
);
}
19. Excel アダプタのテスト
コードの例(Before)
• こういうのも
private interface Assertion {
public void call(Sheet expectedSheet, Sheet actualSheet, int row, int column) throws Exception;
}
private void assertDataAndFormat(String expectedExcelFileName, String sheetName,
int startRowIndex, int startColumnIndex, int endRowIndex, int endColumnIndex) throws Exception {
assertWithExpectedExcelFile(
expectedExcelFileName, sheetName, startRowIndex, startColumnIndex, endRowIndex, endColumnIndex,
new Assertion() {
@Override
public void call(Sheet expectedSheet, Sheet actualSheet, int row, int column) {
Cell expetedCell = expectedSheet.getCell(column, row);
Cell actualCell = actualSheet.getCell(column, row);
assertEquals(expetedCell.getContents(), actualCell.getContents());
assertEquals(expetedCell.getType().toString(), actualCell.getType().toString());
assertEquals(expetedCell.getCellFormat().getFormat().getFormatString(),
actualCell.getCellFormat().getFormat().getFormatString());
AssertUtil.assertCellFormatByJExcelApi(
expetedCell.getCellFormat(), actualCell.getCellFormat());
}
}
);
}
ノイズ
・new
・Assertion()
・@Override
・public
・void
・call
テストの内容にとっては全部どうでもいい
20. Excel アダプタのテスト
コードの例(Before)
• こういうのも
private interface Assertion {
public void call(Sheet expectedSheet, Sheet actualSheet, int row, int column) throws Exception;
}
private void assertDataAndFormat(String expectedExcelFileName, String sheetName,
int startRowIndex, int startColumnIndex, int endRowIndex, int endColumnIndex) throws Exception {
assertWithExpectedExcelFile(
expectedExcelFileName, sheetName, startRowIndex, startColumnIndex, endRowIndex, endColumnIndex,
new Assertion() {
@Override
public void call(Sheet expectedSheet, Sheet actualSheet, int row, int column)
throws Exception {
Cell expetedCell = expectedSheet.getCell(column, row);
Cell actualCell = actualSheet.getCell(column, row);
assertEquals(expetedCell.getContents(), actualCell.getContents());
assertEquals(expetedCell.getType().toString(), actualCell.getType().toString());
assertEquals(expetedCell.getCellFormat().getFormat().getFormatString(),
actualCell.getCellFormat().getFormat().getFormatString());
AssertUtil.assertCellFormatByJExcelApi(
expetedCell.getCellFormat(), actualCell.getCellFormat());
}
}
);
}
21. Excel アダプタのテスト
コードの例(Before)
• こういうのも
private interface Assertion {
public void call(Sheet expectedSheet, Sheet actualSheet, int row, int column) throws Exception;
}
private void assertDataAndFormat(String expectedExcelFileName, String sheetName,
int startRowIndex, int startColumnIndex, int endRowIndex, int endColumnIndex) throws Exception {
assertWithExpectedExcelFile(
expectedExcelFileName, sheetName, startRowIndex, startColumnIndex, endRowIndex, endColumnIndex,
(expectedSheet, actualSheet, row, column) -> {
Cell expetedCell = expectedSheet.getCell(column, row);
Cell actualCell = actualSheet.getCell(column, row);
assertEquals(expetedCell.getContents(), actualCell.getContents());
assertEquals(expetedCell.getType().toString(), actualCell.getType().toString());
assertEquals(expetedCell.getCellFormat().getFormat().getFormatString(),
actualCell.getCellFormat().getFormat().getFormatString());
AssertUtil.assertCellFormatByJExcelApi(
expetedCell.getCellFormat(), actualCell.getCellFormat());
}
}
);
} • こう書ける
22. Excel アダプタのテスト
コードの例(Before)
• こういうのも
private interface Assertion {
public void call(Sheet expectedSheet, Sheet actualSheet, int row, int column) throws Exception;
}
private void assertDataAndFormat(String expectedExcelFileName, String sheetName,
int startRowIndex, int startColumnIndex, int endRowIndex, int endColumnIndex) throws Exception {
assertWithExpectedExcelFile(
expectedExcelFileName, sheetName, startRowIndex, startColumnIndex, endRowIndex, endColumnIndex,
(expectedSheet, actualSheet, row, column) -> {
Cell expetedCell = expectedSheet.getCell(column, row);
Cell actualCell = actualSheet.getCell(column, row);
assertEquals(expetedCell.getContents(), actualCell.getContents());
assertEquals(expetedCell.getType().toString(), actualCell.getType().toString());
assertEquals(expetedCell.getCellFormat().getFormat().getFormatString(),
actualCell.getCellFormat().getFormat().getFormatString());
AssertUtil.assertCellFormatByJExcelApi(
expetedCell.getCellFormat(), actualCell.getCellFormat());
}
}
);
}
Excel アダプタのテスト
コードの例(After)
(匿名関数)
• こう書ける
• メソッドに関数を直接渡せる
24. xxx アダプタのテスト
コードの例(Before)
• こういうのも
@Test public void ソート_標準カラム_文書管理番号_昇順()
throws Exception {
ソート(STANDARD_COLUMN, MANAGE_NUM, TAG_DOC_NUM, 0,
new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2){
return o1.getDocNum() - o2.getDocNum();
}
});
}
25. xxx アダプタのテスト
コードの例(Before)
• こういうのも
@Test public void ソート_標準カラム_文書管理番号_昇順() throws Exception {
ソート(STANDARD_COLUMN, MANAGE_NUM, TAG_DOC_NUM, 0, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
return o1.getDocNum() - o2.getDocNum();
}
});
}
@Test public void ソート_標準カラム_文書管理番号_降順() throws Exception {
ソート(STANDARD_COLUMN, MANAGE_NUM, TAG_DOC_NUM, 1, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
return o2.getDocNum() - o1.getDocNum();
}
});
}
1
26. @Test public void ソート_標準カラム_文書名_昇順() throws Exception {
ソート(STANDARD_COLUMN, DOC_NAME, TAG_DOC_NAME, 0, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
String v1 = o1.getDocName();
String v2 = o2.getDocName();
return compareNull(v1, v2);
}
});
}
@Test public void ソート_標準カラム_文書名_降順() throws Exception {
ソート(STANDARD_COLUMN, DOC_NAME, TAG_DOC_NAME, 1, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
String v1 = o1.getDocName();
String v2 = o2.getDocName();
return compareNull(v2, v1);
}
});
}
@Test public void ソート_標準カラム_コンピューター名_昇順() throws Exception {
ソート(STANDARD_COLUMN, COMPUTER_NAME, TAG_COMPUTER_NAME, 0, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
String v1 = o1.getComputerName();
String v2 = o2.getComputerName();
return compareNull(v1, v2);
}
});
}
@Test public void ソート_標準カラム_コンピューター名_降順() throws Exception {
ソート(STANDARD_COLUMN, COMPUTER_NAME, TAG_COMPUTER_NAME, 1, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
String v1 = o1.getComputerName();
String v2 = o2.getComputerName();
return compareNull(v2, v1);
}
});
}
2
27. @Test public void ソート_標準カラム_ユーザー名_昇順() throws Exception {
ソート(STANDARD_COLUMN, USER_NAME, TAG_USER_NAME, 0, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
String v1 = o1.getUserName();
String v2 = o2.getUserName();
return compareNull(v1, v2);
}
});
}
@Test public void ソート_標準カラム_ユーザー名_降順() throws Exception {
ソート(STANDARD_COLUMN, USER_NAME, TAG_USER_NAME, 1, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
String v1 = o1.getUserName();
String v2 = o2.getUserName();
return compareNull(v2, v1);
}
});
}
@Test public void ソート_標準カラム_印刷プリンター名_昇順() throws Exception {
ソート(STANDARD_COLUMN, PRINTER_NAME, TAG_PRINTER_NAME, 0, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
String v1 = o1.getPrinterName();
String v2 = o2.getPrinterName();
return compareNull(v1, v2);
}
});
}
@Test public void ソート_標準カラム_印刷プリンター名_降順() throws Exception {
ソート(STANDARD_COLUMN, PRINTER_NAME, TAG_PRINTER_NAME, 1, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
String v1 = o1.getPrinterName();
String v2 = o2.getPrinterName();
return compareNull(v2, v1);
}
});
}
3
28. @Test public void ソート_標準カラム_印刷プリンターグループ名_昇順() throws Exception {
ソート(STANDARD_COLUMN, GROUP_NAME, TAG_PRINTER_GROUP_NAME, 0, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
String v1 = o1.getPrinterGroupName();
String v2 = o2.getPrinterGroupName();
return compareNull(v1, v2);
}
});
}
@Test public void ソート_標準カラム_印刷プリンターグループ名_降順() throws Exception {
ソート(STANDARD_COLUMN, GROUP_NAME, TAG_PRINTER_GROUP_NAME, 1, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
String v1 = o1.getPrinterGroupName();
String v2 = o2.getPrinterGroupName();
return compareNull(v2, v1);
}
});
}
@Test public void ソート_標準カラム_スプール開始時刻_昇順() throws Exception {
ソート(STANDARD_COLUMN, SPOOL_START_TIME, TAG_SPOOL_START_TIME, 0, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
Date v1 = o1.getSpoolStartTime();
Date v2 = o2.getSpoolStartTime();
return compareNull(v1, v2);
}
});
}
@Test public void ソート_標準カラム_スプール開始時刻_降順() throws Exception {
ソート(STANDARD_COLUMN, SPOOL_START_TIME, TAG_SPOOL_START_TIME, 1, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
Date v1 = o1.getSpoolStartTime();
Date v2 = o2.getSpoolStartTime();
return compareNull(v2, v1);
}
});
}
4
29. @Test public void ソート_標準カラム_スプール終了時刻_昇順() throws Exception {
ソート(STANDARD_COLUMN, SPOOL_END_TIME, TAG_SPOOL_END_TIME, 0, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
Date v1 = o1.getSpoolEndTime();
Date v2 = o2.getSpoolEndTime();
return compareNull(v1, v2);
}
});
}
@Test public void ソート_標準カラム_スプール終了時刻_降順() throws Exception {
ソート(STANDARD_COLUMN, SPOOL_END_TIME, TAG_SPOOL_END_TIME, 1, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
Date v1 = o1.getSpoolEndTime();
Date v2 = o2.getSpoolEndTime();
return compareNull(v2, v1);
}
});
}
@Test public void ソート_標準カラム_印刷開始時刻_昇順() throws Exception {
ソート(STANDARD_COLUMN, PRINT_START_TIME, TAG_PRINT_START_TIME, 0, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
Date v1 = o1.getPrintStartTime();
Date v2 = o2.getPrintStartTime();
return compareNull(v1, v2);
}
});
}
@Test public void ソート_標準カラム_印刷開始時刻_降順() throws Exception {
ソート(STANDARD_COLUMN, PRINT_START_TIME, TAG_PRINT_START_TIME, 1, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
Date v1 = o1.getPrintStartTime();
Date v2 = o2.getPrintStartTime();
return compareNull(v2, v1);
}
});
}
5
30. @Test public void ソート_標準カラム_印刷終了時刻_昇順() throws Exception {
ソート(STANDARD_COLUMN, PRINT_END_TIME, TAG_PRINT_END_TIME, 0, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
Date v1 = o1.getPrintEndTime();
Date v2 = o2.getPrintEndTime();
return compareNull(v1, v2);
}
});
}
@Test public void ソート_標準カラム_印刷終了時刻_降順() throws Exception {
ソート(STANDARD_COLUMN, PRINT_END_TIME, TAG_PRINT_END_TIME, 1, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
Date v1 = o1.getPrintEndTime();
Date v2 = o2.getPrintEndTime();
return compareNull(v2, v1);
}
});
}
@Test public void ソート_標準カラム_最終更新時刻_昇順() throws Exception {
ソート(STANDARD_COLUMN, UPDATE_TIME, TAG_UPDATE_TIME, 0, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
Date v1 = o1.getUpdateTime();
Date v2 = o2.getUpdateTime();
return compareNull(v1, v2);
}
});
}
@Test public void ソート_標準カラム_最終更新時刻_降順() throws Exception {
ソート(STANDARD_COLUMN, UPDATE_TIME, TAG_UPDATE_TIME, 1, new Comparator() {
@Override
public int compare(XXXDocument o1, XXXDocument o2) {
Date v1 = o1.getUpdateTime();
Date v2 = o2.getUpdateTime();
return compareNull(v2, v1);
}
});
}
6
32. • こう書ける
@Test public void ソート_標準カラム_文書管理番号_昇順() throws Exception {
ソート(STANDARD_COLUMN, MANAGE_NUM, TAG_DOC_NUM, 0,
(o1, o2) -> o1.getDocNum() - o2.getDocNum());
}
@Test public void ソート_標準カラム_文書管理番号_降順() throws Exception {
ソート(STANDARD_COLUMN, MANAGE_NUM, TAG_DOC_NUM, 1,
(o1, o2) -> o2.getDocNum() - o1.getDocNum());
}
@Test public void ソート_標準カラム_文書名_昇順() throws Exception {
ソート(STANDARD_COLUMN, DOC_NAME, TAG_DOC_NAME, 0,
(o1, o2) -> compareNull(o1.getDocName(), o2.getDocName());
}
@Test public void ソート_標準カラム_文書名_降順() throws Exception {
ソート(STANDARD_COLUMN, DOC_NAME, TAG_DOC_NAME, 1,
(o1, o2) -> compareNull(o2.getDocName(), o1.getDocName());
}
@Test public void ソート_標準カラム_コンピューター名_昇順() throws Exception {
ソート(STANDARD_COLUMN, COMPUTER_NAME, TAG_COMPUTER_NAME, 0,
(o1, o2) -> compareNull(o1.getComputerName(), o2.getComputerName());
}
xxx アダプタのテスト
コードの例(After)
1
33. @Test public void ソート_標準カラム_コンピューター名_降順() throws Exception {
ソート(STANDARD_COLUMN, COMPUTER_NAME, TAG_COMPUTER_NAME, 1,
(o1, o2) -> compareNull(o2.getComputerName(), o1.getComputerName());
}
@Test public void ソート_標準カラム_ユーザー名_昇順() throws Exception {
ソート(STANDARD_COLUMN, USER_NAME, TAG_USER_NAME, 0,
(o1, o2) -> compareNull(o1.getUserName(), o2.getUserName());
}
@Test public void ソート_標準カラム_ユーザー名_降順() throws Exception {
ソート(STANDARD_COLUMN, USER_NAME, TAG_USER_NAME, 1,
(o1, o2) -> compareNull(o2.getUserName(), o1.getUserName());
}
@Test public void ソート_標準カラム_印刷プリンター名_昇順() throws Exception {
ソート(STANDARD_COLUMN, PRINTER_NAME, TAG_PRINTER_NAME, 0,
(o1, o2) -> compareNull(o1.getPrinterName(), o2.getPrinterName());
}
@Test public void ソート_標準カラム_印刷プリンター名_降順() throws Exception {
ソート(STANDARD_COLUMN, PRINTER_NAME, TAG_PRINTER_NAME, 1,
(o1, o2) -> compareNull(o2.getPrinterName(), o1.getPrinterName());
}
@Test public void ソート_標準カラム_印刷プリンターグループ名_昇順() throws Exception {
ソート(STANDARD_COLUMN, GROUP_NAME, TAG_PRINTER_GROUP_NAME, 0,
(o1, o2) -> compareNull(o1.getPrinterGroupName(), o2.getPrinterGroupName());
}
@Test public void ソート_標準カラム_印刷プリンターグループ名_降順() throws Exception {
ソート(STANDARD_COLUMN, GROUP_NAME, TAG_PRINTER_GROUP_NAME, 1,
(o1, o2) -> compareNull(o2.getPrinterGroupName(), o1.getPrinterGroupName());
}
@Test public void ソート_標準カラム_スプール開始時刻_昇順() throws Exception {
ソート(STANDARD_COLUMN, SPOOL_START_TIME, TAG_SPOOL_START_TIME, 0,
(o1, o2) -> compareNull(o1.getSpoolStartTime(), o2.getSpoolStartTime());
}
@Test public void ソート_標準カラム_スプール開始時刻_降順() throws Exception {
ソート(STANDARD_COLUMN, SPOOL_START_TIME, TAG_SPOOL_START_TIME, 1,
(o1, o2) -> compareNull(o2.getSpoolStartTime(), o1.getSpoolStartTime());
}
@Test public void ソート_標準カラム_スプール終了時刻_昇順() throws Exception {
ソート(STANDARD_COLUMN, SPOOL_END_TIME, TAG_SPOOL_END_TIME, 0,
(o1, o2) -> compareNull(o1.getSpoolEndTime(), o2.getSpoolEndTime());
}
2
34. @Test public void ソート_標準カラム_スプール終了時刻_降順() throws Exception {
ソート(STANDARD_COLUMN, SPOOL_END_TIME, TAG_SPOOL_END_TIME, 1,
(o1, o2) -> compareNull(o2.getSpoolEndTime(), o1.getSpoolEndTime());
}
@Test public void ソート_標準カラム_印刷開始時刻_昇順() throws Exception {
ソート(STANDARD_COLUMN, PRINT_START_TIME, TAG_PRINT_START_TIME, 0,
(o1, o2) -> compareNull(o1.getPrintStartTime(), o2.getPrintStartTime());
}
@Test public void ソート_標準カラム_印刷開始時刻_降順() throws Exception {
(o1, o2) -> compareNull(o2.getPrintStartTime(), o1.getPrintStartTime());
}
@Test public void ソート_標準カラム_印刷終了時刻_昇順() throws Exception {
ソート(STANDARD_COLUMN, PRINT_END_TIME, TAG_PRINT_END_TIME, 0,
(o1, o2) -> compareNull(o1.getPrintEndTime(), o2.getPrintEndTime());
}
@Test public void ソート_標準カラム_印刷終了時刻_降順() throws Exception {
ソート(STANDARD_COLUMN, PRINT_END_TIME, TAG_PRINT_END_TIME, 1,
(o1, o2) -> compareNull(o2.getPrintEndTime(), o1.getPrintEndTime());
}
@Test public void ソート_標準カラム_最終更新時刻_昇順() throws Exception {
(o1, o2) -> compareNull(o1.getUpdateTime(), o2.getUpdateTime());
}
@Test public void ソート_標準カラム_最終更新時刻_降順() throws Exception {
ソート(STANDARD_COLUMN, UPDATE_TIME, TAG_UPDATE_TIME, 1,
(o1, o2) -> compareNull(o2.getUpdateTime(), o1.getUpdateTime());
}
3
50. 関数型インタフェースの例
• ~ JDK 7
• Runnable
• Callable
• Comparator
• JDK 8 ~
• Predicate
• Consumer
• Supplier
51. 関数型インタフェースの例
• ~ JDK 7
• Runnable
• Callable
• Comparator
• JDK 8 ~
• Predicate
• Consumer
• Supplier
52. JDK 7 までの Comparator
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
}
53. JDK 8 の Comparator
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
... (6 default methods)
public static <T extends Comparable<? super T>> Comparator<T>
reverseOrder() {
return Collections.reverseOrder();
}
... (8 static methods)
}
54. JDK 8 の Comparator
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
... (6 default methods)
public static <T extends Comparable<? super T>> Comparator<T>
reverseOrder() {
return Collections.reverseOrder();
}
... (8 static methods)
}
55. JDK 8 の Comparator
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
... (6 default methods)
public static <T extends Comparable<? super T>> Comparator<T>
reverseOrder() {
return Collections.reverseOrder();
}
... (8 static methods)
}
56. JDK 8 の Comparator
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
... (6 default methods)
public static <T extends Comparable<? super T>> Comparator<T>
reverseOrder() {
return Collections.reverseOrder();
}
... (8 static methods)
}
57. JDK 8 の Comparator
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
... (6 default methods)
public static <T extends Comparable<? super T>> Comparator<T>
reverseOrder() {
return Collections.reverseOrder();
}
... (8 static methods)
}
58. JDK 8 の Comparator
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
... (6 default methods)
public static <T extends Comparable<? super T>> Comparator<T>
reverseOrder() {
return Collections.reverseOrder();
}
... (8 static methods)
}
64. ラムダ式の記法
• ( 実装するメソッドの引数 ) -> { 処理 }
• 例 : Comparator<Integer> の場合
int compare(Integer o1, Integer o2)
に対して
(Integer o1, Integer o2) -> { return o1 – o2 }
65. ラムダ式の記法
• ( 実装するメソッドの引数 ) -> { 処理 }
• 例 : Comparator<Integer> の場合
int compare(Integer o1, Integer o2)
に対して
(Integer o1, Integer o2) -> { return o1 – o2 }
66. ラムダ式の記法
• ( 実装するメソッドの引数 ) -> { 処理 }
• 例 : Comparator<Integer> の場合
int compare(Integer o1, Integer o2)
に対して
(Integer o1, Integer o2) -> { return o1 – o2; }
68. ラムダ式の記法 (省略前)
• ( 実装するメソッドの引数 ) -> { 処理 }
• 例 : Comparator<Integer> の場合
int compare(Integer o1, Integer o2)
に対して
(Integer o1, Integer o2) -> { return o1 – o2; }
69. ラムダ式の記法 (省略 1)
• 型推論
• 例 : Comparator の場合
int compare(Integer o1, Integer o2)
に対して
(Integer o1, Integer o2) -> { return o1 – o2; }
70. ラムダ式の記法 (省略 1)
• 型推論
• 例 : Comparator<Integer> の場合
int compare(Integer o1, Integer o2)
に対して
(o1, o2) -> { return o1 – o2; }
71. ラムダ式の記法 (省略 2)
• 波括弧(「{}」)と return の省略
• 例 : Comparator<Integer> の場合
int compare(Integer o1, Integer o2)
に対して
(o1, o2) -> { return o1 – o2; }
72. ラムダ式の記法 (省略 2)
• 波括弧(「{}」)と return の省略
• 例 : Comparator<Integer> の場合
int compare(Integer o1, Integer o2)
に対して
(o1, o2) -> o1 – o2
74. ラムダ式の記法 (省略 3)
• 丸括弧(「()」)の省略前
• 例 : Predicate<Integer> の場合
boolean test(Integer t)
に対して
(Integer t) -> { return (t > 100); }
75. ラムダ式の記法 (省略 3)
• 丸括弧(「()」)の省略後
• 例 : Predicate<Integer> の場合
boolean test(Integer t)
に対して
(Integer t) -> { return (t > 100); }
76. ラムダ式の記法 (省略 3)
• 丸括弧(「()」)の省略後
• 例 : Predicate<Integer> の場合
boolean test(Integer t)
に対して
t -> { return (t > 100); }
77. ラムダ式の記法 (省略 3)
• さらに省略すると
• 例 : Predicate<Integer> の場合
boolean test(Integer t)
に対して
t -> { return (t > 100); }
78. ラムダ式の記法 (省略 3)
• さらに省略すると
• 例 : Predicate<Integer> の場合
boolean test(Integer t)
に対して
t -> t > 100
96. 原始 for 文
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
for (int i = 0; i < members.size(); i++) {
System.out.println(members.get(i));
}
97. 原始 for 文
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
for (int i = 0; i < members.size(); i++) {
System.out.println(members.get(i));
}
• ノイズが多い
• OBOE や変数の取り違えが
起こりかねない
98. 拡張 for 文(Java 5~)
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
for (String member : members) {
System.out.println(member);
}
99. 拡張 for 文(Java 5~)
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
for (String member : members) {
System.out.println(member);
}
• もう OBOE は起こらない
• が、まだ冗長だ……。
100. forEach(Java 8~)
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
Members.forEach(new Consumer<String>() {
public void accept(final String name) {
System.out.println(name);
}
});
101. forEach(Java 8~)
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
members.forEach(new Consumer<String>() {
public void accept(final String name) {
System.out.println(name);
}
});
• ウッけっこうノイズが多いような
……
102. forEach(Java 8~)
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
members.forEach(new Consumer<String>() {
public void accept(final String name) {
System.out.println(name);
}
});
• そんなときのためのラムダ式
• 1
103. forEach(Java 8~)
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
members.forEach(
(final String name) ->
System.out.println(name));
• そんなときのためのラムダ式
• 1
104. forEach(Java 8~)
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
members.forEach(
(final String name) ->
System.out.println(name));
• そんなときのためのラムダ式
• 2
105. forEach(Java 8~)
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
members.forEach(
name -> System.out.println(name));
• そんなときのためのラムダ式
• 2
106. forEach(Java 8~)
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
members.forEach(
name -> System.out.println(name));
• そんなときのためのラムダ式
• さえいらない(メソッド参照)
107. forEach(Java 8~)
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
members.forEach(System.out.println);
• そんなときのためのラムダ式
• さえいらない(メソッド参照)
108. forEach(Java 8~)
final List<String> members =
Arrays.asList(“Ohshima”, “Hirose”, “Nancii”,
“Tanaka”, “Chen”, “Takano”);
members.forEach(System.out.println);
• そんなときのためのラムダ式
• さえいらない(メソッド参照)
メンバーを それぞれ 標準出力に出す
115. List<CellToWrite[]> rows =
Arrays.asList(new CellToWrite[][] {
new CellToWrite[neededColumnsCount]
});
for (IntermediateCellData cell : data) {
int row = cell.rowIndex - minRow;
int column = cell.columnIndex - minColumn;
rows.get(row)[column] = new CellToWrite(
cell.address, cell.column, createWriteOption(cell));
}
116. List<CellToWrite[]> rows =
Arrays.asList(new CellToWrite[][] {
new CellToWrite[neededColumnsCount]
});
data.forEach(cell -> {
int row = cell.rowIndex - minRow;
int column = cell.columnIndex - minColumn;
rows.get(row)[column] = new CellToWrite(
cell.address, cell.column, createWriteOption(cell));
});
• これだけだと
大して簡潔にならないし、しかも
117. List<CellToWrite[]> rows =
Arrays.asList(new CellToWrite[][] {
new CellToWrite[neededColumnsCount]
});
data.forEach(cell -> {
int row = cell.rowIndex - minRow;
int column = cell.columnIndex - minColumn;
rows.get(row)[column] = new CellToWrite(
cell.address, cell.column, createWriteOption(cell));
});
• 言うほど読みやすくならない
• ていうかコンパイルエラー処理されない例外の型 Exception
118. List<CellToWrite[]> rows =
Arrays.asList(new CellToWrite[][] {
new CellToWrite[neededColumnsCount]
});
data.forEach(cell -> {
int row = cell.rowIndex - minRow;
int column = cell.columnIndex - minColumn;
rows.get(row)[column] = new CellToWrite(
cell.address, cell.column, createWriteOption(cell));
});
• 言うほど読みやすくならない
• ていうかコンパイルエラー
処理されない例外の型 Exception
Iterable#forEach(Consumer<? super T> action)
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
public CellToWrite(
String position, Column column,
Excel2007WriteOption option)
throws Exception {
119. List<CellToWrite[]> rows =
Arrays.asList(new CellToWrite[][] {
new CellToWrite[neededColumnsCount]
});
data.forEach(cell -> {
int row = cell.rowIndex - minRow;
int column = cell.columnIndex - minColumn;
try {
rows.get(row)[column] = new CellToWrite(
cell.address, cell.column,
createWriteOption(cell));
} catch (Exception e) {
throw new RuntimeException(e);
}
});