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

io.tarantool.driver.api.tuple.TarantoolFieldImpl Maven / Gradle / Ivy

Go to download

Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework

There is a newer version: 0.14.0
Show newest version
package io.tarantool.driver.api.tuple;

import io.tarantool.driver.mappers.MessagePackObjectMapper;
import io.tarantool.driver.mappers.MessagePackValueMapper;
import org.msgpack.value.Value;
import org.msgpack.value.ValueFactory;

import java.util.Objects;

/**
 * Basic tuple field implementation
 *
 * @author Alexey Kuzin
 */
public class TarantoolFieldImpl implements TarantoolField {

    private Object value;

    /**
     * Deserializing constructor
     * @param value MessagePack value.
     */
    TarantoolFieldImpl(Value value) {
        this.value = value;
    }

    /**
     * Serializing constructor
     * @param object entity object
     * @param  entity type
     */
     TarantoolFieldImpl(O object) {
        this.value = object;
    }

    @Override
    public Value toMessagePackValue(MessagePackObjectMapper mapper) {
        return getEntity(mapper);
    }

    @SuppressWarnings("unchecked")
    private Value getEntity(MessagePackObjectMapper mapper) {
        if (value == null) {
            return ValueFactory.newNil();
        }
        if (value instanceof Value) {
            return (Value) value;
        }
        return mapper.toValue(value);
    }

    @Override
    @SuppressWarnings("unchecked")
    public  O getValue(Class targetClass, MessagePackValueMapper mapper) {
        if (value == null) {
            return null;
        }
        if (value instanceof Value) {
            return mapper.fromValue((Value) value, targetClass);
        }
        if (value.getClass().isAssignableFrom(targetClass)) {
            return (O) value;
        }
        throw new UnsupportedOperationException(
                String.format("Cannot convert field value of type %s to type %s", value.getClass(), targetClass));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof TarantoolFieldImpl)) {
            return false;
        }
        TarantoolFieldImpl that = (TarantoolFieldImpl) o;
        return Objects.equals(value, that.value);
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy