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

info.archinnov.achilles.internals.metamodel.Tuple1Property Maven / Gradle / Ivy

There is a newer version: 6.1.0
Show newest version
/*
 * Copyright (C) 2012-2016 DuyHai DOAN
 *
 * 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 info.archinnov.achilles.internals.metamodel;

import static info.archinnov.achilles.internals.cql.TupleExtractor.extractType;
import static java.lang.String.format;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.datastax.driver.core.DataType;
import com.datastax.driver.core.GettableData;
import com.datastax.driver.core.TupleType;
import com.datastax.driver.core.TupleValue;
import com.google.common.reflect.TypeToken;

import info.archinnov.achilles.internals.metamodel.columns.FieldInfo;
import info.archinnov.achilles.type.tuples.Tuple1;
import info.archinnov.achilles.validation.Validator;

public class Tuple1Property extends AbstractTupleProperty> {

    private static final Logger LOGGER = LoggerFactory.getLogger(Tuple1.class);

    private final AbstractProperty aProperty;

    public Tuple1Property(FieldInfo> fieldInfo, AbstractProperty aProperty) {
        super(new TypeToken>() {
        }, fieldInfo);
        this.aProperty = aProperty;
    }

    @Override
    public TupleValue encodeFromJavaInternal(Tuple1 tuple1) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("Encode from Java '%s' tuple1 %s to CQL type", fieldName, tuple1));
        }
        return tupleType.newValue(aProperty.encodeFromRaw(tuple1._1()));
    }

    @Override
    public TupleValue encodeFromRawInternal(Object o) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("Encode raw '%s' tuple1 object %s", fieldName, o));
        }

        Validator.validateTrue(Tuple1.class.isAssignableFrom(o.getClass()), "The class of object %s to encode should be Tuple1", o);
        return encodeFromJava((Tuple1) o);
    }

    @Override
    public Tuple1 decodeFromGettableInternal(GettableData gettableData) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("Decode '%s' tuple1 from gettable object %s", fieldName, gettableData));
        }

        return decodeFromRaw(gettableData.getTupleValue(fieldInfo.quotedCqlColumn));
    }

    @Override
    public Tuple1 decodeFromRawInternal(Object o) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("Decode '%s' tuple1 raw object %s", fieldName, o));
        }
        Validator.validateTrue(TupleValue.class.isAssignableFrom(o.getClass()), "The class of object %s to decode should be %s", o, TupleValue.class.getCanonicalName());
        final List types = tupleType.getComponentTypes();
        return new Tuple1<>(aProperty.decodeFromRaw(extractType((TupleValue) o, types.get(0), aProperty, 0)));
    }

    @Override
    public TupleType buildType() {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(format("Build current '%s' tuple1 data type", fieldName));
        }

        return tupleTypeFactory.typeFor(aProperty.buildType());
    }

    @Override
    protected List> componentsProperty() {
        return Arrays.asList(aProperty);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy