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

com.facebook.presto.spi.session.PropertyMetadata Maven / Gradle / Ivy

There is a newer version: 0.291
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.
 */
package com.facebook.presto.spi.session;

import com.facebook.presto.common.type.Type;
import io.airlift.units.DataSize;
import io.airlift.units.Duration;

import java.util.Objects;
import java.util.function.Function;

import static com.facebook.presto.common.type.BigintType.BIGINT;
import static com.facebook.presto.common.type.BooleanType.BOOLEAN;
import static com.facebook.presto.common.type.DoubleType.DOUBLE;
import static com.facebook.presto.common.type.IntegerType.INTEGER;
import static com.facebook.presto.common.type.TinyintType.TINYINT;
import static com.facebook.presto.common.type.VarcharType.VARCHAR;
import static java.util.Objects.requireNonNull;

public final class PropertyMetadata
{
    private final SessionPropertyMetadata sessionPropertyMetadata;
    private final Class javaType;
    private final Type sqlType;
    private final T defaultValue;
    private final Function decoder;
    private final Function encoder;

    public PropertyMetadata(
            String name,
            String description,
            Type sqlType,
            Class javaType,
            T defaultValue,
            boolean hidden,
            Function decoder,
            Function encoder)
    {
        this.sqlType = requireNonNull(sqlType, "sqlType is null");
        this.sessionPropertyMetadata = new SessionPropertyMetadata(
                name,
                description,
                sqlType.getTypeSignature(),
                defaultValue == null ? "" : defaultValue.toString(),
                hidden);
        this.javaType = requireNonNull(javaType, "javaType is null");
        this.defaultValue = defaultValue;
        this.decoder = requireNonNull(decoder, "decoder is null");
        this.encoder = requireNonNull(encoder, "encoder is null");
    }

    /**
     * Name of the property.  This must be a valid identifier.
     */
    public String getName()
    {
        return sessionPropertyMetadata.getName();
    }

    /**
     * Description for the end user.
     */
    public String getDescription()
    {
        return sessionPropertyMetadata.getDescription();
    }

    /**
     * SQL type of the property.
     */
    public Type getSqlType()
    {
        return sqlType;
    }

    /**
     * Java type of this property.
     */
    public Class getJavaType()
    {
        return javaType;
    }

    /**
     * Gets the default value for this property.
     */
    public T getDefaultValue()
    {
        return defaultValue;
    }

    /**
     * Is this property hidden from users?
     */
    public boolean isHidden()
    {
        return sessionPropertyMetadata.isHidden();
    }

    /**
     * Decodes the SQL type object value to the Java type of the property.
     */
    public T decode(Object value)
    {
        return decoder.apply(value);
    }

    /**
     * Encodes the Java type value to SQL type object value
     */
    public Object encode(T value)
    {
        return encoder.apply(value);
    }

    public static PropertyMetadata booleanProperty(String name, String description, Boolean defaultValue, boolean hidden)
    {
        return new PropertyMetadata<>(
                name,
                description,
                BOOLEAN,
                Boolean.class,
                defaultValue,
                hidden,
                Boolean.class::cast,
                object -> object);
    }

    public static PropertyMetadata integerProperty(String name, String description, Integer defaultValue, boolean hidden)
    {
        return new PropertyMetadata<>(
                name,
                description,
                INTEGER,
                Integer.class,
                defaultValue,
                hidden,
                value -> ((Number) value).intValue(),
                object -> object);
    }

    public static PropertyMetadata longProperty(String name, String description, Long defaultValue, boolean hidden)
    {
        return new PropertyMetadata<>(
                name,
                description,
                BIGINT,
                Long.class,
                defaultValue,
                hidden,
                value -> ((Number) value).longValue(),
                object -> object);
    }

    public static PropertyMetadata tinyIntProperty(String name, String description, Byte defaultValue, boolean hidden)
    {
        return new PropertyMetadata<>(
                name,
                description,
                TINYINT,
                Byte.class,
                defaultValue,
                hidden,
                value -> ((Number) value).byteValue(),
                object -> object);
    }

    public static PropertyMetadata doubleProperty(String name, String description, Double defaultValue, boolean hidden)
    {
        return new PropertyMetadata<>(
                name,
                description,
                DOUBLE,
                Double.class,
                defaultValue,
                hidden,
                value -> ((Number) value).doubleValue(),
                object -> object);
    }

    public static PropertyMetadata stringProperty(String name, String description, String defaultValue, boolean hidden)
    {
        return new PropertyMetadata<>(
                name,
                description,
                VARCHAR,
                String.class,
                defaultValue,
                hidden,
                String.class::cast,
                object -> object);
    }

    public static PropertyMetadata dataSizeProperty(String name, String description, DataSize defaultValue, boolean hidden)
    {
        return new PropertyMetadata<>(
                name,
                description,
                VARCHAR,
                DataSize.class,
                defaultValue,
                hidden,
                value -> DataSize.valueOf((String) value),
                DataSize::toString);
    }

    public static PropertyMetadata durationProperty(String name, String description, Duration defaultValue, boolean hidden)
    {
        return new PropertyMetadata<>(
                name,
                description,
                VARCHAR,
                Duration.class,
                defaultValue,
                hidden,
                value -> Duration.valueOf((String) value),
                Duration::toString);
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) {
            return true;
        }
        if (!(o instanceof PropertyMetadata)) {
            return false;
        }

        PropertyMetadata that = (PropertyMetadata) o;

        boolean isSessionPropertyMetadataEqual = this.sessionPropertyMetadata.equals(that.sessionPropertyMetadata);

        boolean isJavaTypeEqual = this.javaType.equals(that.javaType);
        boolean isDefaultValueEqual = (this.defaultValue == null && that.defaultValue == null)
                || (this.defaultValue != null && this.defaultValue.equals(that.defaultValue));

        return isSessionPropertyMetadataEqual && isJavaTypeEqual && isDefaultValueEqual;
    }

    @Override
    public int hashCode()
    {
        int result = sessionPropertyMetadata.hashCode();

        result = 31 * result + Objects.hashCode(javaType);
        result = 31 * result + Objects.hashCode(defaultValue);

        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy