Table of Contents
ToggleSupplier in Java with Examples
只有一個抽象方法的接口,定義了 get 方法,可以在一個元素上測試或應用一些操作,常用於建立預設值,例如:當物件為 Null 時,傳回預設值,取代傳統實作接口的方法,讓程式碼更加簡潔和易讀, Supplier in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
@FunctionalInterface
public interface Supplier<T> {
T get();
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- functional
| +- supplier
| +- SupplierWithExamplesTest.java
單元測試
Supplier Java 提供 get 等操作 Supplier 。
Food
建立 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();
}
}
get
建立 Supplier 物件,操作 get 方法,取得物件。
@Test
public void get() {
Supplier<Food> value = () -> new Food("Bacon", 1, 1);
Food result = value.get();
System.out.println(result);
assertNotNull(result);
value = () -> new Food("Ham", 2, 1);
result = value.get();
System.out.println(result);
assertNotNull(result);
value = () -> new Food("Pork", 3, 1);
result = value.get();
System.out.println(result);
assertNotNull(result);
}
{"name":"Bacon","quantity":1.0,"type":1}
{"name":"Ham","quantity":2.0,"type":1}
{"name":"Pork","quantity":3.0,"type":1}
traditional
Java Supplier 使用傳統方式,實作 Supplier 接口,取得物件。
public static class DefaultValue<E> implements Supplier<Food> {
@Override
public Food get() {
return new Food("DEFAULT_FOOD", 1, 1);
}
}
@Test
public void traditional() {
DefaultValue<Food> defaultValue = new DefaultValue<Food>();
Food result = defaultValue.get();
System.out.println(result);
assertNotNull(result);
}
{"name":"DEFAULT_FOOD","quantity":1.0,"type":1}
SupplierWithExamplesTest.java
Supplier in Java 新增單元測試,驗證 Java Supplier Example 是否符合預期。
package org.ruoxue.java_147.functional;
import static org.junit.Assert.*;
import java.util.function.Supplier;
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 SupplierWithExamplesTest {
@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 get() {
Supplier<Food> value = () -> new Food("Bacon", 1, 1);
Food result = value.get();
System.out.println(result);
assertNotNull(result);
value = () -> new Food("Ham", 2, 1);
result = value.get();
System.out.println(result);
assertNotNull(result);
value = () -> new Food("Pork", 3, 1);
result = value.get();
System.out.println(result);
assertNotNull(result);
}
public static class DefaultValue<E> implements Supplier<Food> {
@Override
public Food get() {
return new Food("DEFAULT_FOOD", 1, 1);
}
}
@Test
public void traditional() {
DefaultValue<Food> defaultValue = new DefaultValue<Food>();
Food result = defaultValue.get();
System.out.println(result);
assertNotNull(result);
}
}
心得分享
Java Supplier Example 功能接口,使用 Lambda 語法,可當作其他方法的傳入參數或是引用其他方法為實例, Java Supplier 提供了幾種 Supplier 常見方法的操作範例,例如: get 等方法。