diff --git a/src/main/java/lsh/ext/gson/IBuilder1.java b/src/main/java/lsh/ext/gson/IBuilder1.java index cf88952e..0d23cef5 100644 --- a/src/main/java/lsh/ext/gson/IBuilder1.java +++ b/src/main/java/lsh/ext/gson/IBuilder1.java @@ -33,6 +33,43 @@ public R build() { }; } + static IBuilder1 of( + final Supplier createAux, + final BiConsumer consume, + final Function build + ) { + return new IBuilder1<>() { + private boolean isInitialized; + @Nullable + private AUX aux; + + @Override + public void accept(final A1 a1) { + consume.accept(a1, createOrGet()); + } + + @Override + public R build() { + return build.apply(createOrGet()); + } + + private AUX createOrGet() { + if ( !isInitialized ) { + aux = createAux.get(); + isInitialized = true; + } + return aux; + } + }; + } + + static IBuilder1 of( + final Supplier createResult, + final BiConsumer consume + ) { + return of(createResult, consume, Function.identity()); + } + static IBuilder1 of(final Collector collector) { return new IBuilder1<>() { private final Supplier supplier = collector.supplier(); diff --git a/src/main/java/lsh/ext/gson/IBuilder2.java b/src/main/java/lsh/ext/gson/IBuilder2.java index c77b9654..6a21dc0f 100644 --- a/src/main/java/lsh/ext/gson/IBuilder2.java +++ b/src/main/java/lsh/ext/gson/IBuilder2.java @@ -33,6 +33,43 @@ public R build() { }; } + static IBuilder2 of( + final Supplier createAux, + final TriConsumer consume, + final Function build + ) { + return new IBuilder2<>() { + private boolean isInitialized; + @Nullable + private AUX aux; + + @Override + public void accept(final A1 a1, final A2 a2) { + consume.accept(a1, a2, createOrGet()); + } + + @Override + public R build() { + return build.apply(createOrGet()); + } + + private AUX createOrGet() { + if ( !isInitialized ) { + aux = createAux.get(); + isInitialized = true; + } + return aux; + } + }; + } + + static IBuilder2 of( + final Supplier createResult, + final TriConsumer consume + ) { + return of(createResult, consume, Function.identity()); + } + static IBuilder2 of( final Collector collector, final BiFunction toElementType