com.datastax.driver.mapping.MappedUDTCodec Maven / Gradle / Ivy
/*
* Copyright DataStax, Inc.
*
* 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.datastax.driver.mapping;
import com.datastax.driver.core.CodecRegistry;
import com.datastax.driver.core.ProtocolVersion;
import com.datastax.driver.core.TypeCodec;
import com.datastax.driver.core.UserType;
import java.nio.ByteBuffer;
import java.util.Map;
/** Serializes a class annotated with {@code @UDT} to the corresponding CQL user-defined type. */
class MappedUDTCodec extends TypeCodec.AbstractUDTCodec {
private final UserType cqlUserType;
private final Class udtClass;
private final Map columnMappers;
private final CodecRegistry codecRegistry;
MappedUDTCodec(
UserType cqlUserType,
Class udtClass,
Map columnMappers,
MappingManager mappingManager) {
super(cqlUserType, udtClass);
this.cqlUserType = cqlUserType;
this.udtClass = udtClass;
this.columnMappers = columnMappers;
this.codecRegistry =
mappingManager.getSession().getCluster().getConfiguration().getCodecRegistry();
}
@Override
protected T newInstance() {
return ReflectionUtils.newInstance(udtClass);
}
Class getUdtClass() {
return udtClass;
}
@Override
protected ByteBuffer serializeField(T source, String fieldName, ProtocolVersion protocolVersion) {
@SuppressWarnings("unchecked")
AliasedMappedProperty aliasedMappedProperty = columnMappers.get(fieldName);
if (aliasedMappedProperty == null) return null;
Object value = aliasedMappedProperty.mappedProperty.getValue(source);
TypeCodec