All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jooq.impl.CallbackFormattingProvider Maven / Gradle / Ivy

There is a newer version: 3.19.18
Show newest version
/*
 * 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
 * Apache-2.0 license 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 ); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy