From 5ec13feba9c073eb69db77863aa423397425cb83 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Mon, 16 Aug 2021 17:06:34 +0200 Subject: [PATCH] [jOOQ/jOOQ#12304] Add CallbackFormattingProvider --- .../java/org/jooq/FormattingProvider.java | 77 ++++- .../jooq/impl/CallbackFormattingProvider.java | 271 ++++++++++++++++++ 2 files changed, 347 insertions(+), 1 deletion(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/CallbackFormattingProvider.java diff --git a/jOOQ/src/main/java/org/jooq/FormattingProvider.java b/jOOQ/src/main/java/org/jooq/FormattingProvider.java index 338b0fd5ab..c1b70e8e85 100644 --- a/jOOQ/src/main/java/org/jooq/FormattingProvider.java +++ b/jOOQ/src/main/java/org/jooq/FormattingProvider.java @@ -37,7 +37,10 @@ */ package org.jooq; -import org.jooq.impl.DefaultExecuteListener; +import java.util.function.Supplier; +import java.util.function.ToIntFunction; + +import org.jooq.impl.CallbackFormattingProvider; import org.jooq.impl.DefaultFormattingProvider; import org.jetbrains.annotations.NotNull; @@ -113,4 +116,76 @@ public interface FormattingProvider { * {@link Character#isIdeographic(int)} character. */ int width(String string); + + /** + * Create an {@link FormattingProvider} with a {@link #txtFormat()} + * implementation. + */ + @NotNull + public static CallbackFormattingProvider onTxtFormat(Supplier newOnTxtFormat) { + return new CallbackFormattingProvider().onTxtFormat(newOnTxtFormat); + } + + /** + * Create an {@link FormattingProvider} with a {@link #csvFormat()} + * implementation. + */ + @NotNull + public static CallbackFormattingProvider onCsvFormat(Supplier newOnCsvFormat) { + return new CallbackFormattingProvider().onCsvFormat(newOnCsvFormat); + } + + /** + * Create an {@link FormattingProvider} with a + * {@link #jsonFormatForResults()} implementation. + */ + @NotNull + public static CallbackFormattingProvider onJsonFormatForResults(Supplier newOnJsonFormatForResults) { + return new CallbackFormattingProvider().onJsonFormatForResults(newOnJsonFormatForResults); + } + + /** + * Create an {@link FormattingProvider} with a + * {@link #jsonFormatForRecords()} implementation. + */ + @NotNull + public static CallbackFormattingProvider onJsonFormatForRecords(Supplier newOnJsonFormatForRecords) { + return new CallbackFormattingProvider().onJsonFormatForRecords(newOnJsonFormatForRecords); + } + + /** + * Create an {@link FormattingProvider} with a + * {@link #xmlFormatForResults()} implementation. + */ + @NotNull + public static CallbackFormattingProvider onXmlFormatForResults(Supplier newOnXmlFormatForResults) { + return new CallbackFormattingProvider().onXmlFormatForResults(newOnXmlFormatForResults); + } + + /** + * Create an {@link FormattingProvider} with a {@link #xmlFormatForRecords()} + * implementation. + */ + @NotNull + public static CallbackFormattingProvider onXmlFormatForRecords(Supplier newOnXmlFormatForRecords) { + return new CallbackFormattingProvider().onXmlFormatForRecords(newOnXmlFormatForRecords); + } + + /** + * Create an {@link FormattingProvider} with a {@link #chartFormat()} + * implementation. + */ + @NotNull + public static CallbackFormattingProvider onChartFormat(Supplier newOnChartFormat) { + return new CallbackFormattingProvider().onChartFormat(newOnChartFormat); + } + + /** + * Create an {@link FormattingProvider} with a {@link #width(String)} + * implementation. + */ + @NotNull + public static CallbackFormattingProvider onWidth(ToIntFunction newOnWidth) { + return new CallbackFormattingProvider().onWidth(newOnWidth); + } } diff --git a/jOOQ/src/main/java/org/jooq/impl/CallbackFormattingProvider.java b/jOOQ/src/main/java/org/jooq/impl/CallbackFormattingProvider.java new file mode 100644 index 0000000000..5128446e91 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/CallbackFormattingProvider.java @@ -0,0 +1,271 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + + +import java.util.function.Consumer; +import java.util.function.Supplier; +import java.util.function.ToIntFunction; + +import org.jooq.CSVFormat; +import org.jooq.ChartFormat; +import org.jooq.FormattingProvider; +import org.jooq.JSONFormat; +import org.jooq.TXTFormat; +import org.jooq.VisitContext; +import org.jooq.XMLFormat; + +import org.jetbrains.annotations.NotNull; + +/** + * A {@link FormattingProvider} that allows for functional composition. + *

+ * For example:

+ * FormattingProvider listener = FormattingProvider
+ *   .on(ctx -> something())
+ *   .onVisitEnd(ctx -> something());
+ * 
+ * + * @author Lukas Eder + */ +public final class CallbackFormattingProvider implements FormattingProvider { + + private static final DefaultFormattingProvider DEFAULT = new DefaultFormattingProvider(); + + private final Supplier onTxtFormat; + private final Supplier onCsvFormat; + private final Supplier onJsonFormatForResults; + private final Supplier onJsonFormatForRecords; + private final Supplier onXmlFormatForResults; + private final Supplier onXmlFormatForRecords; + private final Supplier onChartFormat; + private final ToIntFunction onWidth; + + public CallbackFormattingProvider() { + this( + DEFAULT::txtFormat, + DEFAULT::csvFormat, + DEFAULT::jsonFormatForResults, + DEFAULT::jsonFormatForRecords, + DEFAULT::xmlFormatForResults, + DEFAULT::xmlFormatForRecords, + DEFAULT::chartFormat, + DEFAULT::width + ); + } + + private CallbackFormattingProvider( + Supplier onTxtFormat, + Supplier onCsvFormat, + Supplier onJsonFormatForResults, + Supplier onJsonFormatForRecords, + Supplier onXmlFormatForResults, + Supplier onXmlFormatForRecords, + Supplier onChartFormat, + ToIntFunction onWidth + ) { + this.onTxtFormat = onTxtFormat; + this.onCsvFormat = onCsvFormat; + this.onJsonFormatForResults = onJsonFormatForResults; + this.onJsonFormatForRecords = onJsonFormatForRecords; + this.onXmlFormatForResults = onXmlFormatForResults; + this.onXmlFormatForRecords = onXmlFormatForRecords; + this.onChartFormat = onChartFormat; + this.onWidth = onWidth; + } + + @NotNull + @Override + public final TXTFormat txtFormat() { + return onTxtFormat.get(); + } + + @NotNull + @Override + public final CSVFormat csvFormat() { + return onCsvFormat.get(); + } + + @NotNull + @Override + public final JSONFormat jsonFormatForResults() { + return onJsonFormatForResults.get(); + } + + @NotNull + @Override + public final JSONFormat jsonFormatForRecords() { + return onJsonFormatForRecords.get(); + } + + @NotNull + @Override + public final XMLFormat xmlFormatForResults() { + return onXmlFormatForResults.get(); + } + + @NotNull + @Override + public final XMLFormat xmlFormatForRecords() { + return onXmlFormatForRecords.get(); + } + + @NotNull + @Override + public final ChartFormat chartFormat() { + return onChartFormat.get(); + } + + @Override + public final int width(String string) { + return onWidth.applyAsInt(string); + } + + @NotNull + public final CallbackFormattingProvider onTxtFormat(Supplier newOnTxtFormat) { + return new CallbackFormattingProvider( + newOnTxtFormat, + onCsvFormat, + onJsonFormatForResults, + onJsonFormatForRecords, + onXmlFormatForResults, + onXmlFormatForRecords, + onChartFormat, + onWidth + ); + } + + @NotNull + public final CallbackFormattingProvider onCsvFormat(Supplier newOnCsvFormat) { + return new CallbackFormattingProvider( + onTxtFormat, + newOnCsvFormat, + onJsonFormatForResults, + onJsonFormatForRecords, + onXmlFormatForResults, + onXmlFormatForRecords, + onChartFormat, + onWidth + ); + } + + @NotNull + public final CallbackFormattingProvider onJsonFormatForResults(Supplier newOnJsonFormatForResults) { + return new CallbackFormattingProvider( + onTxtFormat, + onCsvFormat, + newOnJsonFormatForResults, + onJsonFormatForRecords, + onXmlFormatForResults, + onXmlFormatForRecords, + onChartFormat, + onWidth + ); + } + + @NotNull + public final CallbackFormattingProvider onJsonFormatForRecords(Supplier newOnJsonFormatForRecords) { + return new CallbackFormattingProvider( + onTxtFormat, + onCsvFormat, + onJsonFormatForResults, + newOnJsonFormatForRecords, + onXmlFormatForResults, + onXmlFormatForRecords, + onChartFormat, + onWidth + ); + } + + @NotNull + public final CallbackFormattingProvider onXmlFormatForResults(Supplier newOnXmlFormatForResults) { + return new CallbackFormattingProvider( + onTxtFormat, + onCsvFormat, + onJsonFormatForResults, + onJsonFormatForRecords, + newOnXmlFormatForResults, + onXmlFormatForRecords, + onChartFormat, + onWidth + ); + } + + @NotNull + public final CallbackFormattingProvider onXmlFormatForRecords(Supplier newOnXmlFormatForRecords) { + return new CallbackFormattingProvider( + onTxtFormat, + onCsvFormat, + onJsonFormatForResults, + onJsonFormatForRecords, + onXmlFormatForResults, + newOnXmlFormatForRecords, + onChartFormat, + onWidth + ); + } + + @NotNull + public final CallbackFormattingProvider onChartFormat(Supplier newOnChartFormat) { + return new CallbackFormattingProvider( + onTxtFormat, + onCsvFormat, + onJsonFormatForResults, + onJsonFormatForRecords, + onXmlFormatForResults, + onXmlFormatForRecords, + newOnChartFormat, + onWidth + ); + } + + @NotNull + public final CallbackFormattingProvider onWidth(ToIntFunction newOnWidth) { + return new CallbackFormattingProvider( + onTxtFormat, + onCsvFormat, + onJsonFormatForResults, + onJsonFormatForRecords, + onXmlFormatForResults, + onXmlFormatForRecords, + onChartFormat, + newOnWidth + ); + } +} +