[jOOQ/jOOQ#12304] Add CallbackFormattingProvider
This commit is contained in:
parent
05f15bef71
commit
5ec13feba9
@ -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<? extends TXTFormat> newOnTxtFormat) {
|
||||
return new CallbackFormattingProvider().onTxtFormat(newOnTxtFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an {@link FormattingProvider} with a {@link #csvFormat()}
|
||||
* implementation.
|
||||
*/
|
||||
@NotNull
|
||||
public static CallbackFormattingProvider onCsvFormat(Supplier<? extends CSVFormat> newOnCsvFormat) {
|
||||
return new CallbackFormattingProvider().onCsvFormat(newOnCsvFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an {@link FormattingProvider} with a
|
||||
* {@link #jsonFormatForResults()} implementation.
|
||||
*/
|
||||
@NotNull
|
||||
public static CallbackFormattingProvider onJsonFormatForResults(Supplier<? extends JSONFormat> newOnJsonFormatForResults) {
|
||||
return new CallbackFormattingProvider().onJsonFormatForResults(newOnJsonFormatForResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an {@link FormattingProvider} with a
|
||||
* {@link #jsonFormatForRecords()} implementation.
|
||||
*/
|
||||
@NotNull
|
||||
public static CallbackFormattingProvider onJsonFormatForRecords(Supplier<? extends JSONFormat> newOnJsonFormatForRecords) {
|
||||
return new CallbackFormattingProvider().onJsonFormatForRecords(newOnJsonFormatForRecords);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an {@link FormattingProvider} with a
|
||||
* {@link #xmlFormatForResults()} implementation.
|
||||
*/
|
||||
@NotNull
|
||||
public static CallbackFormattingProvider onXmlFormatForResults(Supplier<? extends XMLFormat> newOnXmlFormatForResults) {
|
||||
return new CallbackFormattingProvider().onXmlFormatForResults(newOnXmlFormatForResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an {@link FormattingProvider} with a {@link #xmlFormatForRecords()}
|
||||
* implementation.
|
||||
*/
|
||||
@NotNull
|
||||
public static CallbackFormattingProvider onXmlFormatForRecords(Supplier<? extends XMLFormat> newOnXmlFormatForRecords) {
|
||||
return new CallbackFormattingProvider().onXmlFormatForRecords(newOnXmlFormatForRecords);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an {@link FormattingProvider} with a {@link #chartFormat()}
|
||||
* implementation.
|
||||
*/
|
||||
@NotNull
|
||||
public static CallbackFormattingProvider onChartFormat(Supplier<? extends ChartFormat> newOnChartFormat) {
|
||||
return new CallbackFormattingProvider().onChartFormat(newOnChartFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an {@link FormattingProvider} with a {@link #width(String)}
|
||||
* implementation.
|
||||
*/
|
||||
@NotNull
|
||||
public static CallbackFormattingProvider onWidth(ToIntFunction<? super String> newOnWidth) {
|
||||
return new CallbackFormattingProvider().onWidth(newOnWidth);
|
||||
}
|
||||
}
|
||||
|
||||
271
jOOQ/src/main/java/org/jooq/impl/CallbackFormattingProvider.java
Normal file
271
jOOQ/src/main/java/org/jooq/impl/CallbackFormattingProvider.java
Normal file
@ -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.
|
||||
* <p>
|
||||
* For example: <code><pre>
|
||||
* FormattingProvider listener = FormattingProvider
|
||||
* .on(ctx -> something())
|
||||
* .onVisitEnd(ctx -> something());
|
||||
* </pre></code>
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public final class CallbackFormattingProvider implements FormattingProvider {
|
||||
|
||||
private static final DefaultFormattingProvider DEFAULT = new DefaultFormattingProvider();
|
||||
|
||||
private final Supplier<? extends TXTFormat> onTxtFormat;
|
||||
private final Supplier<? extends CSVFormat> onCsvFormat;
|
||||
private final Supplier<? extends JSONFormat> onJsonFormatForResults;
|
||||
private final Supplier<? extends JSONFormat> onJsonFormatForRecords;
|
||||
private final Supplier<? extends XMLFormat> onXmlFormatForResults;
|
||||
private final Supplier<? extends XMLFormat> onXmlFormatForRecords;
|
||||
private final Supplier<? extends ChartFormat> onChartFormat;
|
||||
private final ToIntFunction<? super String> onWidth;
|
||||
|
||||
public CallbackFormattingProvider() {
|
||||
this(
|
||||
DEFAULT::txtFormat,
|
||||
DEFAULT::csvFormat,
|
||||
DEFAULT::jsonFormatForResults,
|
||||
DEFAULT::jsonFormatForRecords,
|
||||
DEFAULT::xmlFormatForResults,
|
||||
DEFAULT::xmlFormatForRecords,
|
||||
DEFAULT::chartFormat,
|
||||
DEFAULT::width
|
||||
);
|
||||
}
|
||||
|
||||
private CallbackFormattingProvider(
|
||||
Supplier<? extends TXTFormat> onTxtFormat,
|
||||
Supplier<? extends CSVFormat> onCsvFormat,
|
||||
Supplier<? extends JSONFormat> onJsonFormatForResults,
|
||||
Supplier<? extends JSONFormat> onJsonFormatForRecords,
|
||||
Supplier<? extends XMLFormat> onXmlFormatForResults,
|
||||
Supplier<? extends XMLFormat> onXmlFormatForRecords,
|
||||
Supplier<? extends ChartFormat> onChartFormat,
|
||||
ToIntFunction<? super String> 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<? extends TXTFormat> newOnTxtFormat) {
|
||||
return new CallbackFormattingProvider(
|
||||
newOnTxtFormat,
|
||||
onCsvFormat,
|
||||
onJsonFormatForResults,
|
||||
onJsonFormatForRecords,
|
||||
onXmlFormatForResults,
|
||||
onXmlFormatForRecords,
|
||||
onChartFormat,
|
||||
onWidth
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final CallbackFormattingProvider onCsvFormat(Supplier<? extends CSVFormat> newOnCsvFormat) {
|
||||
return new CallbackFormattingProvider(
|
||||
onTxtFormat,
|
||||
newOnCsvFormat,
|
||||
onJsonFormatForResults,
|
||||
onJsonFormatForRecords,
|
||||
onXmlFormatForResults,
|
||||
onXmlFormatForRecords,
|
||||
onChartFormat,
|
||||
onWidth
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final CallbackFormattingProvider onJsonFormatForResults(Supplier<? extends JSONFormat> newOnJsonFormatForResults) {
|
||||
return new CallbackFormattingProvider(
|
||||
onTxtFormat,
|
||||
onCsvFormat,
|
||||
newOnJsonFormatForResults,
|
||||
onJsonFormatForRecords,
|
||||
onXmlFormatForResults,
|
||||
onXmlFormatForRecords,
|
||||
onChartFormat,
|
||||
onWidth
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final CallbackFormattingProvider onJsonFormatForRecords(Supplier<? extends JSONFormat> newOnJsonFormatForRecords) {
|
||||
return new CallbackFormattingProvider(
|
||||
onTxtFormat,
|
||||
onCsvFormat,
|
||||
onJsonFormatForResults,
|
||||
newOnJsonFormatForRecords,
|
||||
onXmlFormatForResults,
|
||||
onXmlFormatForRecords,
|
||||
onChartFormat,
|
||||
onWidth
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final CallbackFormattingProvider onXmlFormatForResults(Supplier<? extends XMLFormat> newOnXmlFormatForResults) {
|
||||
return new CallbackFormattingProvider(
|
||||
onTxtFormat,
|
||||
onCsvFormat,
|
||||
onJsonFormatForResults,
|
||||
onJsonFormatForRecords,
|
||||
newOnXmlFormatForResults,
|
||||
onXmlFormatForRecords,
|
||||
onChartFormat,
|
||||
onWidth
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final CallbackFormattingProvider onXmlFormatForRecords(Supplier<? extends XMLFormat> newOnXmlFormatForRecords) {
|
||||
return new CallbackFormattingProvider(
|
||||
onTxtFormat,
|
||||
onCsvFormat,
|
||||
onJsonFormatForResults,
|
||||
onJsonFormatForRecords,
|
||||
onXmlFormatForResults,
|
||||
newOnXmlFormatForRecords,
|
||||
onChartFormat,
|
||||
onWidth
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final CallbackFormattingProvider onChartFormat(Supplier<? extends ChartFormat> newOnChartFormat) {
|
||||
return new CallbackFormattingProvider(
|
||||
onTxtFormat,
|
||||
onCsvFormat,
|
||||
onJsonFormatForResults,
|
||||
onJsonFormatForRecords,
|
||||
onXmlFormatForResults,
|
||||
onXmlFormatForRecords,
|
||||
newOnChartFormat,
|
||||
onWidth
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final CallbackFormattingProvider onWidth(ToIntFunction<? super String> newOnWidth) {
|
||||
return new CallbackFormattingProvider(
|
||||
onTxtFormat,
|
||||
onCsvFormat,
|
||||
onJsonFormatForResults,
|
||||
onJsonFormatForRecords,
|
||||
onXmlFormatForResults,
|
||||
onXmlFormatForRecords,
|
||||
onChartFormat,
|
||||
newOnWidth
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user