info.archinnov.achilles.internals.metamodel.JdkOptionalProperty Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of achilles-core Show documentation
Show all versions of achilles-core Show documentation
CQL implementation for Achilles using Datastax Java driver
/*
* 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 java.lang.String.format;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.datastax.driver.core.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.TypeParameter;
import com.google.common.reflect.TypeToken;
import info.archinnov.achilles.internals.factory.TupleTypeFactory;
import info.archinnov.achilles.internals.factory.UserTypeFactory;
import info.archinnov.achilles.internals.metamodel.columns.FieldInfo;
import info.archinnov.achilles.type.codec.Codec;
import info.archinnov.achilles.type.codec.CodecSignature;
import info.archinnov.achilles.type.factory.BeanFactory;
import info.archinnov.achilles.validation.Validator;
public class JdkOptionalProperty extends AbstractProperty, TO> {
private static final Logger LOGGER = LoggerFactory.getLogger(JdkOptionalProperty.class);
private final AbstractProperty aProperty;
public JdkOptionalProperty(FieldInfo> fieldInfo, AbstractProperty aProperty) {
super(new TypeToken>() {}.where(new TypeParameter() {}, aProperty.valueFromTypeToken),
aProperty.valueToTypeToken, fieldInfo);
this.aProperty = aProperty;
}
@Override
boolean isOptional() {
return true;
}
@Override
public void encodeToSettable(TO a, SettableData> settableData) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Encode value %s to settable object %s", a, settableData));
}
if (a != null) {
aProperty.encodeToSettable(a, settableData);
}
}
@Override
TO encodeFromJavaInternal(Optional javaValue) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Encode raw '%s' optional object %s", fieldName, javaValue));
}
if (javaValue.isPresent()) {
return aProperty.encodeFromJavaInternal(javaValue.get());
} else {
return null;
}
}
@Override
TO encodeFromRawInternal(Object o) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Encode raw '%s' optional object %s", fieldName, o));
}
Validator.validateTrue(Optional.class.isAssignableFrom(o.getClass()), "The class of object %s to encode should be java.util.Optional", o);
return encodeFromJava((Optional) o);
}
@Override
Optional decodeFromGettableInternal(GettableData gettableData) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Decode '%s' optional from gettable object %s", fieldName, gettableData));
}
final FROM decoded = aProperty.decodeFromGettableInternal(gettableData);
if (decoded == null) {
return Optional.empty();
} else {
return Optional.of(decoded);
}
}
@Override
Optional decodeFromRawInternal(Object o) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Decode '%s' tuple1 raw object %s", fieldName, o));
}
final FROM decoded = aProperty.decodeFromRawInternal(o);
if (decoded == null) {
return Optional.empty();
} else {
return Optional.of(decoded);
}
}
@Override
public DataType buildType() {
return aProperty.buildType();
}
@Override
public void encodeFieldToUdt(ENTITY entity, UDTValue udtValue) {
final TO encoded = aProperty.encodeField(entity);
if (encoded != null) {
aProperty.encodeFieldToUdt(entity, udtValue);
}
}
@Override
public boolean containsUDTProperty() {
return aProperty.containsUDTProperty();
}
@Override
public List> getUDTClassProperties() {
return aProperty.getUDTClassProperties();
}
@Override
public void inject(BeanFactory factory) {
aProperty.inject(factory);
}
@Override
public void inject(ObjectMapper mapper) {
aProperty.inject(mapper);
}
@Override
public void injectRuntimeCodecs(Map, Codec, ?>> runtimeCodecs) {
aProperty.injectRuntimeCodecs(runtimeCodecs);
}
@Override
public void inject(UserTypeFactory userTypeFactory, TupleTypeFactory tupleTypeFactory) {
aProperty.inject(userTypeFactory, tupleTypeFactory);
}
@Override
public void injectKeyspace(String keyspace) {
aProperty.injectKeyspace(keyspace);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy