Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 1&1 Internet AG, https://github.com/1and1/
*
* 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 net.oneandone.troilus;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.ProtocolVersion;
import com.datastax.driver.core.TupleType;
import com.datastax.driver.core.UDTValue;
import com.datastax.driver.core.UserType;
import com.google.common.base.Optional;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
class UDTValueMapper {
private final ProtocolVersion protocolVersion;
private final BeanMapper beanMapper;
UDTValueMapper(ProtocolVersion protocolVersion, BeanMapper beanMapper) {
this.protocolVersion = protocolVersion;
this.beanMapper = beanMapper;
}
private UserType getUserType(LoadingCache userTypeCache, String usertypeName) {
try {
return userTypeCache.get(usertypeName);
} catch (ExecutionException e) {
throw new RuntimeException(e.getCause());
}
}
static boolean isBuildInType(DataType dataType) {
if (dataType.isCollection()) {
for (DataType type : dataType.getTypeArguments()) {
if (!isBuildInType(type)) {
return false;
}
}
return true;
} else {
return DataType.allPrimitiveTypes().contains(dataType) || (TupleType.class.isAssignableFrom(dataType.getClass()));
}
}
/**
* @param datatype the db datatype
* @param udtValue the udt value
* @param fieldtype1 the field 1 type
* @param fieldtype2 the field 2 type
* @param fieldname the fieldname
* @return the mapped value or null
*/
public Object fromUdtValue(DataType datatype,
UDTValue udtValue,
Class> fieldtype1,
Class> fieldtype2,
String fieldname) {
// build-in type
if (isBuildInType(datatype)) {
return datatype.deserialize(udtValue.getBytesUnsafe(fieldname), protocolVersion);
// udt collection
} else if (datatype.isCollection()) {
Class> type = datatype.getName().asJavaClass();
// set
if (Set.class.isAssignableFrom(type)) {
return fromUdtValues(datatype.getTypeArguments().get(0),
ImmutableSet.copyOf(udtValue.getSet(fieldname, UDTValue.class)),
fieldtype1);
// list
} else if (List.class.isAssignableFrom(type)) {
return fromUdtValues(datatype.getTypeArguments().get(0),
ImmutableList.copyOf(udtValue.getList(fieldname, UDTValue.class)),
fieldtype1);
// map
} else {
if (isBuildInType(datatype.getTypeArguments().get(0))) {
return fromUdtValues(datatype.getTypeArguments().get(0),
datatype.getTypeArguments().get(1),
ImmutableMap.