Lambda之Supplier

特質

生成者模型,無輸入參數,返回指定類型

測試

測試參照

@Data
public static class Foo {
	String strVal = "Hello";
	 int intVal = 1;
	 Double doubleVal = 2.0;
	 Long longVal = 3L;
	 Boolean trueVal = true;

	String strTmp;
	int intTmp = 1;
	Double doubleTmp;
	Long longTmp;
}

萬能型Supplier

可以指定返回類型。

Supplier<Foo> supplier = Foo::new;
Assert.assertTrue(supplier.get().trueVal);

固定類型Supplier

無需指定輸出類型。

BooleanSupplier booleanSupplier = () -> Boolean.TRUE;
Assert.assertTrue(booleanSupplier.getAsBoolean());

DoubleSupplier doubleSupplier = () -> foo.doubleVal;
assertEquals(foo.doubleVal, foo.doubleVal, doubleSupplier.getAsDouble());

IntSupplier intSupplier = () -> foo.intVal;
assertEquals(foo.intVal, intSupplier.getAsInt());

LongSupplier longSupplier = () -> foo.longVal;
assertEquals(foo.longVal.longValue(), longSupplier.getAsLong());
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章