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

org.jooq.Converters Maven / Gradle / Ivy

There is a newer version: 3.19.15
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
 * 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;

import java.io.Serializable;
import java.util.function.Function;

import org.jooq.exception.DataTypeException;
import org.jooq.impl.AbstractConverter;
import org.jooq.impl.IdentityConverter;
import org.jooq.impl.SQLDataType;

import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;

/**
 * A chain of converters.
 *
 * @param  The database type - i.e. any type available from
 *            {@link SQLDataType}
 * @param  The user type
 * @author Lukas Eder
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Converters extends AbstractConverter {

    final Converter[] chain;

    /**
     * Create an identity converter.
     */
    @NotNull
    public static  Converter identity(final Class type) {
        return new IdentityConverter(type);
    }

    /**
     * Create an identity converter.
     *
     * @deprecated - [#10689] - 3.14.0 - This converter does not work. Do not
     *             use this method, use {@link #identity(Class)} instead.
     */
    @Deprecated(forRemoval = true, since = "3.14")
    @NotNull
    public static  Converter of() {
        return new Converters();
    }

    /**
     * Create a single converter.
     *
     * @deprecated - [#10689] - 3.14.0 - This method does not provide any useful
     *             functionality and will be removed in the future.
     */
    @Deprecated(forRemoval = true, since = "3.14")
    @NotNull
    public static  Converter of(Converter converter) {
        return new Converters(converter);
    }

    /**
     * Chain two converters.
     */
    @NotNull
    public static  Converter of(Converter c1, Converter c2) {
        return new Converters(c1, c2);
    }

    /**
     * Chain three converters.
     */
    @NotNull
    public static  Converter of(Converter c1, Converter c2, Converter c3) {
        return new Converters(c1, c2, c3);
    }

    /**
     * Chain four converters.
     */
    @NotNull
    public static  Converter of(Converter c1, Converter c2, Converter c3, Converter c4) {
        return new Converters(c1, c2, c3, c4);
    }

    /**
     * Inverse a converter.
     */
    public static  Converter inverse(final Converter converter) {

    	// [#11099] Allow instanceof checks on IdentityConverter for performance reasons
    	if (converter instanceof IdentityConverter)
            return (Converter) converter;
        else
            return Converter.of(converter.toType(), converter.fromType(), converter::to, converter::from);
    }

    /**
     * Create a converter that can convert arrays with the component types being
     * the argument converter's types.
     */
    public static  Converter forArrays(Converter converter) {
        if (converter instanceof ArrayComponentConverter a)
            return a.converter;
        else
            return new ArrayConverter<>(converter);
    }

    /**
     * Create a converter that can convert component types based on the argument
     * converter, which converts array types.
     */
    public static  Converter forArrayComponents(Converter converter) {
        if (converter instanceof ArrayConverter a)
            return a.converter;
        else
            return new ArrayComponentConverter<>(converter);
    }


    Converters(Converter... chain) {
        super(chain[0].fromType(), chain[chain.length - 1].toType());

        this.chain = chain;
    }

    @Override
    public final U from(T t) {
        Object result = t;

        for (int i = 0; i < chain.length; i++)
            result = chain[i].from(result);

        return (U) result;
    }

    @Override
    public final T to(U u) {
        Object result = u;

        for (int i = chain.length - 1; i >= 0; i--)
            result = chain[i].to(result);

        return (T) result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        String separator = " -> ";

        sb.append("Converters [ ");
        sb.append(fromType().getName());

        for (Converter converter : chain) {
            sb.append(separator);
            sb.append(converter.toType().getName());
        }

        sb.append(" ]");
        return sb.toString();
    }

    static final  Function nullable(Function f) {
        return f instanceof Serializable
            ? (Function & Serializable) t -> t == null ? null
            : f.apply(t) : t -> t == null ? null : f.apply(t);
    }

    static final  Function notImplemented() {
        return t -> { throw new DataTypeException("Conversion function not implemented"); };
    }

    /**
     * An unknown type that is used when users do not provide any explicit user
     * type {@link Class} reference e.g. in
     * {@link DataType#asConvertedDataTypeFrom(Function)} or
     * {@link DataType#asConvertedDataTypeTo(Function)}
     */
    @Internal
    public static final class UnknownType {
        private UnknownType() {}
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy