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

org.babyfish.jimmer.sql.runtime.AbstractScalarProvider Maven / Gradle / Ivy

The newest version!
package org.babyfish.jimmer.sql.runtime;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.util.UUID;

public abstract class AbstractScalarProvider implements ScalarProvider {

    static final ScalarProvider UUID_BY_BYTE_ARRAY =
            new AbstractScalarProvider() {
                @Override
                public UUID toScalar(byte[] sqlValue) {
                    ByteBuffer byteBuffer = ByteBuffer.wrap(sqlValue);
                    long high = byteBuffer.getLong();
                    long low = byteBuffer.getLong();
                    return new UUID(high, low);
                }

                @Override
                public byte[] toSql(UUID scalarValue) {
                    ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
                    byteBuffer.putLong(scalarValue.getMostSignificantBits());
                    byteBuffer.putLong(scalarValue.getLeastSignificantBits());
                    return byteBuffer.array();
                }

                @Override
                public String toString() {
                    return "ScalarProvider.UUID_BY_BYTE_ARRAY";
                }
            };

    static final ScalarProvider UUID_BY_STRING =
            new AbstractScalarProvider() {
                @Override
                public UUID toScalar(String sqlValue) {
                    return UUID.fromString(sqlValue);
                }

                @Override
                public String toSql(UUID scalarValue) {
                    return scalarValue.toString();
                }

                @Override
                public String toString() {
                    return "ScalarProvider.UUID_BY_STRING";
                }
            };

    private final Type scalarType;

    private final Class sqlType;

    public AbstractScalarProvider(Class scalarType, Class sqlType) {
        Meta.validateScalarType(scalarType);
        validateSqlType(sqlType);
        this.scalarType = scalarType;
        this.sqlType = sqlType;
    }

    @SuppressWarnings("unchecked")
    protected AbstractScalarProvider() {
        Meta meta = Meta.create(this.getClass());
        this.scalarType = meta.scalarType;
        this.sqlType = (Class) meta.sqlType;
    }

    @NotNull
    public final Type getScalarType() {
        return scalarType;
    }

    @NotNull
    public final Class getSqlType() {
        return sqlType;
    }

    private void validateSqlType(Class sqlType) {
        if (isJsonScalar() && !String.class.equals(sqlType)) {
            throw new IllegalArgumentException(
                    "Illegal sql type \"" +
                    sqlType.getName() +
                    "\", json scalar provider must have String sql type"
            );
        }
    }
}