-
Notifications
You must be signed in to change notification settings - Fork 1
Home
yipuran edited this page Nov 3, 2022
·
37 revisions
public class Item{
public int id;
public String name;
public void setId(int id){
this.id = id;
}
public void setName(Strin s){
this.name = s;
}
}
Item item = GenericBuilder.of(Item::new)
.with(Item::setId, 1).with(Item::setName, "uranus")
.build();
setter 有無や参照スコープに関係なく、フィールドに値をセット
例)name フィールドに 文字列 "uranus" をセット
Fieldsetter.of((t, u)->"name").accept(item, "uran");
Fieldsetter を使うと、GenericBuilderもsetter有無に関係なくセットできる
public class Some{
private int id;
}
Some some = GenericBuilder.of(Some::new)
.with(Fieldsetter.of((t, u)->"id"), 1002)
.build();
getter 有無や参照スコープに関係なく、フィールドの値を取得する。
Fieldgetter 実行で cast が必要であるが継承元フィールドでも取得が可能
int id = (int)Fieldgetter.of(t->"id").apply(some);
Function apply の実行結果で Biconsumer accept を実行する。 例)Baseクラスが持つ値を Itemクラスにセットする
Base base;
Iten item;
//
ApplyConsumer.of(Base::getId, Item::setId)
.and(Base::getName, Item::setName)
.and(Base::getDate, Item::setDate)
.accept(base, item);
Consumer実行結果を取得する
setter など、返却値をもたないメソッドを実行した後のインスタンス取得に効果を発揮する。
ReturnalConsumer を使い回す方法もできる。
Item item = ReturnalConsumer.of(Item.class)
.with(e->e.setName("Apple"))
.with(e->e.setLength(142))
.get(new Item());
Pattern Matcher で使う
String str = "aaa134_cd45_def";
String res = ReturnalConsumer.of(Matcher.class)
.with(Matcher::find)
.get(Pattern.compile("\\d+").matcher(str))
.group();
134
BiConsumer として accept 実行した結果を Function<T, T>として生成する。 例)Optional の map を利用してインスタンス生成から属性値セットまで実行する
Foo foo = Optional.of(new Foo())
.map(AcceptFunction.of(Foo::setId, 1001))
.map(AcceptFunction.of(Foo::setName, "Yip"))
.map(AcceptFunction.of(Foo::setLocate, "Tokyo"))
.get();
PATH指定の配下のファイルツリーをコレクションとして収集する。 org.yipuran.file.FileCollection
FileCollection.of("/opt").scan().stream().forEach(f->{
System.out.println(f.getAbsolutePath());
});