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

io.deepsense.neptune.clientlibrary.models.impl.parameters.ParameterValueImpl Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
/**
 * Copyright (c) 2016, CodiLime Inc.
 */

package io.deepsense.neptune.clientlibrary.models.impl.parameters;

import com.google.common.base.Preconditions;
import io.deepsense.neptune.clientlibrary.exceptions.parameters.InvalidParameterTypeException;
import io.deepsense.neptune.clientlibrary.models.ParameterValue;

import java.util.Objects;
import java.util.Optional;

public final class ParameterValueImpl implements ParameterValue {

    private final String parameterName;

    private final Class clazz;

    private final Object value;

    private ParameterValueImpl(
            String parameterName,
            Class clazz,
            Object value) {

        this.parameterName = Preconditions.checkNotNull(parameterName);
        this.clazz = Preconditions.checkNotNull(clazz);
        this.value = value;
    }

    public static ParameterValue fromBoolean(String parameterName, Boolean value) {
        return new ParameterValueImpl(parameterName, Boolean.class, value);
    }

    public static ParameterValue fromDouble(String parameterName, Double value) {
        return new ParameterValueImpl(parameterName, Double.class, value);
    }

    public static ParameterValue fromInteger(String parameterName, Integer value) {
        return new ParameterValueImpl(parameterName, Integer.class, value);
    }

    public static ParameterValue fromString(String parameterName, String value) {
        return new ParameterValueImpl(parameterName, String.class, value);
    }

    @Override
    public Optional asBoolean() {
        assertType(Boolean.class);
        return Optional.ofNullable((Boolean) value);
    }

    @Override
    public Optional asDouble() {
        assertType(Double.class);
        return Optional.ofNullable((Double) value);
    }

    @Override
    public Optional asInteger() {
        assertType(Integer.class);
        return Optional.ofNullable((Integer) value);
    }

    @Override
    public Optional asString() {
        assertType(String.class);
        return Optional.ofNullable((String) value);
    }

    private void assertType(Class typeAsReferenced) {
        if (!clazz.equals(typeAsReferenced)) {
            throw new InvalidParameterTypeException(parameterName, clazz, typeAsReferenced);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ParameterValueImpl that = (ParameterValueImpl) o;
        return Objects.equals(parameterName, that.parameterName)
                && Objects.equals(clazz, that.clazz)
                && Objects.equals(value, that.value);
    }

    @Override
    public int hashCode() {
        return Objects.hash(parameterName, clazz, value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy