Table of Contents
ToggleJava Supplier Interface
常用於初始化、預設值物件,也可當作其他方法的傳入參數或是引用其他方法為實例, Supplier Interface 介紹 Optional 中的 orElseGet 、 orElseThrow 與 Stream 中的 collect 、 generate 等方法,了解 Supplier 的不同操作和方法,本篇增加了範例,並透過單元測試來驗證產出結果。
@FunctionalInterface
public interface Supplier<T> {
T get();
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- functional
| +- supplier
| +- SupplierInterfaceTest.java
單元測試
Supplier Interface Java 提供 Optional 中的 orElseGet 、 orElseThrow 與 Stream 中的 collect 、 generate 等方法操作 Supplier Interface 。
Food
BiConsumer Interface Java 建立 Food 類別,覆寫 equals 、 hashCode ,定義屬性和方法,用來建立一個物件。
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Food {
private String name;
private double quantity;
private int type;
public Food(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
public boolean equals(Object object) {
if (!(object instanceof Food)) {
return false;
}
if (this == object) {
return true;
}
Food other = (Food) object;
return new EqualsBuilder().append(getName(), other.getName()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getName()).toHashCode();
}
}
Optional_orElseGet
建立 Supplier , 建立 Optional 容器,取得容器內的值,若值為 Null 則傳回預設值。
@Test
public void Optional_orElseGet() {
Optional<String> opt = Optional.ofNullable(null);
Supplier<String> defaultValue = () -> "DEFAULT_VALUE";
String result = opt.orElseGet(defaultValue);
System.out.println(result);
assertEquals("DEFAULT_VALUE", result);
Optional<Integer> intOpt = Optional.ofNullable(null);
Supplier<Integer> intSupplier = () -> 0;
int intResult = intOpt.orElseGet(intSupplier);
System.out.println(intResult);
assertEquals(0, intResult);
Optional<List<String>> listOpt = Optional.ofNullable(null);
Supplier<List<String>> listSupplier = ArrayList::new;
List<String> listResult = listOpt.orElseGet(listSupplier);
System.out.println(listResult);
assertEquals(0, listResult.size());
Optional<Food> foodOpt = Optional.ofNullable(null);
Supplier<Food> foodSupplier = () -> new Food("DEFAULT_FOOD", 1, 1);
Food foodResult = foodOpt.orElseGet(foodSupplier);
System.out.println(foodResult);
assertEquals("DEFAULT_FOOD", foodResult.getName());
}
DEFAULT_VALUE
0
[]
{"name":"DEFAULT_FOOD","quantity":1.0,"type":1}
Optional_orElseThrow
Supplier Interface Java 建立 Supplier ,建立 Optional 容器,取得容器內的值,若值為 Null 則拋出例外。
@Test(expected = IllegalArgumentException.class)
public void Optional_orElseThrow() {
Optional<String> opt = Optional.ofNullable(null);
String result = opt.orElseThrow(IllegalArgumentException::new);
System.out.println(result);
assertFalse(opt.isPresent());
}
java.lang.IllegalArgumentException
at java.util.Optional.orElseThrow(Optional.java:290)
at org.ruoxue.java_147.functional.SupplierInterfaceTest.Optional_orElseThrow(SupplierInterfaceTest.java:97)
Stream_collect
Supplier Interface Java 建立 Supplier , List 增加三個元素,過濾符合條件的元素轉成新的 List 。
@Test
public void Stream_collect() {
int expectedSize = 2;
List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
Supplier<List<String>> supplier = () -> new ArrayList<>();
BiConsumer<List<String>, String> accumulator = (l, s) -> l.add(s);
BiConsumer<List<String>, List<String>> combiner = (l, l2) -> l.addAll(l2);
List<String> result = list.stream().parallel().filter(e -> e.length() > 3).collect(supplier, accumulator,
combiner);
System.out.println(result);
assertEquals(expectedSize, result.size());
}
[Bacon, Pork]
Stream_generate
Supplier Interface Java 建立 Supplier , 使用 Stream generate 建立 1 個物件。
@Test
public void Stream_generate() {
Stream.generate(() -> "Bacon").limit(1).forEach(e -> System.out.println(e));
Supplier<Integer> intSupplier = () -> 0;
Consumer<Integer> intConsumer = System.out::println;
Stream.generate(intSupplier).limit(1).forEach(intConsumer);
Supplier<List<String>> listSupplier = ArrayList::new;
Stream.generate(listSupplier).limit(1).forEach(System.out::println);
Supplier<Food> foodSupplier = () -> new Food("Bacon", 1, 1);
Stream.generate(foodSupplier).limit(1).forEach(System.out::println);
}
Bacon
0
[]
{"name":"Bacon","quantity":1.0,"type":1}
SupplierInterfaceTest.java
Supplier Interface in Java 新增單元測試,驗證 Java Supplier Interface Example 是否符合預期。
package org.ruoxue.java_147.functional.supplier;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class SupplierInterfaceTest {
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Food {
private String name;
private double quantity;
private int type;
public Food(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
public boolean equals(Object object) {
if (!(object instanceof Food)) {
return false;
}
if (this == object) {
return true;
}
Food other = (Food) object;
return new EqualsBuilder().append(getName(), other.getName()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getName()).toHashCode();
}
}
@Test
public void Optional_orElseGet() {
Optional<String> opt = Optional.ofNullable(null);
Supplier<String> defaultValue = () -> "DEFAULT_VALUE";
String result = opt.orElseGet(defaultValue);
System.out.println(result);
assertEquals("DEFAULT_VALUE", result);
Optional<Integer> intOpt = Optional.ofNullable(null);
Supplier<Integer> intSupplier = () -> 0;
int intResult = intOpt.orElseGet(intSupplier);
System.out.println(intResult);
assertEquals(0, intResult);
Optional<List<String>> listOpt = Optional.ofNullable(null);
Supplier<List<String>> listSupplier = ArrayList::new;
List<String> listResult = listOpt.orElseGet(listSupplier);
System.out.println(listResult);
assertEquals(0, listResult.size());
Optional<Food> foodOpt = Optional.ofNullable(null);
Supplier<Food> foodSupplier = () -> new Food("DEFAULT_FOOD", 1, 1);
Food foodResult = foodOpt.orElseGet(foodSupplier);
System.out.println(foodResult);
assertEquals("DEFAULT_FOOD", foodResult.getName());
}
@Test(expected = IllegalArgumentException.class)
public void Optional_orElseThrow() {
Optional<String> opt = Optional.ofNullable(null);
String result = opt.orElseThrow(IllegalArgumentException::new);
System.out.println(result);
assertFalse(opt.isPresent());
}
@Test
public void Stream_collect() {
int expectedSize = 2;
List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
Supplier<List<String>> supplier = () -> new ArrayList<>();
BiConsumer<List<String>, String> accumulator = (l, s) -> l.add(s);
BiConsumer<List<String>, List<String>> combiner = (l, l2) -> l.addAll(l2);
List<String> result = list.stream().parallel().filter(e -> e.length() > 3).collect(supplier, accumulator,
combiner);
System.out.println(result);
assertEquals(expectedSize, result.size());
}
@Test
public void Stream_generate() {
Stream.generate(() -> "Bacon").limit(1).forEach(e -> System.out.println(e));
Supplier<Integer> intSupplier = () -> 0;
Consumer<Integer> intConsumer = System.out::println;
Stream.generate(intSupplier).limit(1).forEach(intConsumer);
Supplier<List<String>> listSupplier = ArrayList::new;
Stream.generate(listSupplier).limit(1).forEach(System.out::println);
Supplier<Food> foodSupplier = () -> new Food("Bacon", 1, 1);
Stream.generate(foodSupplier).limit(1).forEach(System.out::println);
}
}
心得分享
Java Supplier Interface Example 使用 Lambda 表達式能讓程式碼更加簡潔與直接,取代傳統實作接口的方法,減少了很多程式碼,大幅提高可讀性, Supplier Interface in Java 提供了幾種 Supplier 常見方法的操作範例,熟悉這些方法的操作,能夠提高開發效率,節省維護上的成本。