Function in Java with Examples - Java 147

Function in Java with Examples – Java 147

Function in Java with Examples

只有一個抽象方法的接口,定義了 apply 方法,可以在一個元素上測試或應用一些操作,常用於物件轉換或數字運算,取代傳統實作接口的方法,讓程式碼更加簡潔和易讀, Function in Java 本篇增加了範例,並透過單元測試來驗證產出結果。

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
}

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- functional
       |                   +- function
       |                       +- FunctionWithExamplesTest.java   

單元測試

Function Java 提供 apply 、 andThen 、 compose 條件或組合成鏈式判斷等操作 Function 。

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();
		}
	}	

apply

建立 Function 物件,傳入 1 個參數,執行程式邏輯,傳回物件。

	@Test
	public void apply() {
		Function<Food, Boolean> startsWith = o -> o.name.startsWith("B");
		boolean result = startsWith.apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertTrue(result);
		result = startsWith.apply(new Food("Ham", 3, 1));
		System.out.println(result);
		assertFalse(result);

		Function<Food, Boolean> greaterThan = o -> o.name.length() > 3;
		result = greaterThan.apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertTrue(result);
		result = greaterThan.apply(new Food("Ham", 3, 1));
		System.out.println(result);
		assertFalse(result);
	}
true
false
true
false

andThen

Function Java Methods 建立 2 個 Function 物件,傳入 1 個參數,使用 andThen 組合執行程式邏輯,傳回物件,其中若有例外拋出,將會中斷執行。

	@Test
	public void andThen() {
		Function<Food, Double> half = d -> d.quantity / 2;
		Function<Double, Double> twice = d -> d * d;
		double result = half.andThen(twice).apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertEquals(0.25d, result, 2);

		Function<Food, Integer> length = s -> s.name.length();
		Function<Integer, Integer> multiply = i -> i * 2;
		int intResult = length.andThen(multiply).apply(new Food("Ham", 3, 1));
		System.out.println(intResult);
		assertEquals(6, intResult);
	}
0.25
6

andThenThrowException

Function Java Methods 建立 Function 物件,傳入參數 null ,會拋出例外 。

	@Test(expected = NullPointerException.class)
	public void andThenThrowException() {
		Function<Food, Double> half = d -> d.quantity / 2;
		half = half.andThen(null);
	}
java.lang.NullPointerException
	at java.util.Objects.requireNonNull(Objects.java:203)
	at java.util.function.Function.andThen(Function.java:87)
	at org.ruoxue.java_147.functional.function.FunctionWithExamplesTest.andThenThrowException(FunctionWithExamplesTest.java:96)

compose

Function Java Methods 建立 2 個 Function 物件,傳入 1 個參數,使用 compose 組合執行程式邏輯,傳回物件,其中若有例外拋出,將會中斷執行。

	@Test
	public void compose() {
		Function<Double, Double> half = d -> d / 2;
		Function<Food, Double> twice = d -> d.quantity * d.quantity;
		double result = half.compose(twice).apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertEquals(0.5, result, 2);
	}
0.5

composeThrowException

Function Methods in Java 建立 Function 物件,傳入參數 null ,會拋出例外 。

	@Test(expected = NullPointerException.class)
	public void composeThrowException() {
		Function<Double, Double> half = d -> d / 2;
		half = half.compose(null);
	}
java.lang.NullPointerException
	at java.util.Objects.requireNonNull(Objects.java:203)
	at java.util.function.Function.compose(Function.java:67)
	at org.ruoxue.java_147.functional.function.FunctionWithExamplesTest.composeThrowException(FunctionWithExamplesTest.java:111)

identity

Function Methods in Java 建立 Function 物件,傳入 1 個參數,傳回參數。

	@Test
	public void identity() {
		Function<Food, Food> identity = Function.identity();
		Food result = identity.apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertEquals("Bacon", result.getName());

		Object objResult = Function.identity().apply(new Food("Ham", 2, 1));
		System.out.println(objResult);
		assertEquals("Ham", ((Food) objResult).getName());

		Function<Integer, Integer> intIdentity = i -> i;
		int intResult = intIdentity.apply(7);
		System.out.println(intResult);
		assertEquals(7, intResult);
	}
{"name":"Bacon","quantity":1.0,"type":1}
{"name":"Ham","quantity":2.0,"type":1}
7

traditional

Function Methods in Java 使用傳統方式,實作 Function 接口,傳回物件。

	public static class Length<E, F> implements Function<Food, Integer> {
		@Override
		public Integer apply(Food t) {
			return t.getName().length();
		}
	}

	@Test
	public void traditional() {
		Function<Food, Integer> length = new Length<Food, Integer>();
		Function<Integer, Integer> multiply = i -> i * 2;
		int result = length.andThen(multiply).apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertEquals(10, result);
		result = length.andThen(multiply).apply(new Food("Ham", 3, 1));
		System.out.println(result);
		assertEquals(6, result);
	}
10
6

FunctionWithExamplesTest.java

Function in Java 新增單元測試,驗證 Java Function Example 是否符合預期。

package org.ruoxue.java_147.functional.function;

import static org.junit.Assert.*;

import java.util.function.Function;
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 FunctionWithExamplesTest {

	@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 apply() {
		Function<Food, Boolean> startsWith = o -> o.name.startsWith("B");
		boolean result = startsWith.apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertTrue(result);
		result = startsWith.apply(new Food("Ham", 3, 1));
		System.out.println(result);
		assertFalse(result);

		Function<Food, Boolean> greaterThan = o -> o.name.length() > 3;
		result = greaterThan.apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertTrue(result);
		result = greaterThan.apply(new Food("Ham", 3, 1));
		System.out.println(result);
		assertFalse(result);
	}

	@Test
	public void andThen() {
		Function<Food, Double> half = d -> d.quantity / 2;
		Function<Double, Double> twice = d -> d * d;
		double result = half.andThen(twice).apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertEquals(0.25d, result, 2);

		Function<Food, Integer> length = s -> s.name.length();
		Function<Integer, Integer> multiply = i -> i * 2;
		int intResult = length.andThen(multiply).apply(new Food("Ham", 3, 1));
		System.out.println(intResult);
		assertEquals(6, intResult);
	}

	@Test(expected = NullPointerException.class)
	public void andThenThrowException() {
		Function<Food, Double> half = d -> d.quantity / 2;
		half = half.andThen(null);
	}

	@Test
	public void compose() {
		Function<Double, Double> half = d -> d / 2;
		Function<Food, Double> twice = d -> d.quantity * d.quantity;
		double result = half.compose(twice).apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertEquals(0.5, result, 2);
	}

	@Test(expected = NullPointerException.class)
	public void composeThrowException() {
		Function<Double, Double> half = d -> d / 2;
		half = half.compose(null);
	}

	@Test
	public void identity() {
		Function<Food, Food> identity = Function.identity();
		Food result = identity.apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertEquals("Bacon", result.getName());

		Object objResult = Function.identity().apply(new Food("Ham", 2, 1));
		System.out.println(objResult);
		assertEquals("Ham", ((Food) objResult).getName());

		Function<Integer, Integer> intIdentity = i -> i;
		int intResult = intIdentity.apply(7);
		System.out.println(intResult);
		assertEquals(7, intResult);
	}

	public static class Length<E, F> implements Function<Food, Integer> {
		@Override
		public Integer apply(Food t) {
			return t.getName().length();
		}
	}

	@Test
	public void traditional() {
		Function<Food, Integer> length = new Length<Food, Integer>();
		Function<Integer, Integer> multiply = i -> i * 2;
		int result = length.andThen(multiply).apply(new Food("Bacon", 1, 1));
		System.out.println(result);
		assertEquals(10, result);
		result = length.andThen(multiply).apply(new Food("Ham", 3, 1));
		System.out.println(result);
		assertEquals(6, result);
	}
}

心得分享

Java Function Example 功能接口,使用 Lambda 語法,可當作其他方法的傳入參數或是引用其他方法為實例, Java Function 提供了幾種 Function 常見方法的操作範例,例如: apply 、 andThen 、 compose 等方法。

發佈留言