org.simpleflatmapper.datastax.impl.getter.DatastaxGenericShortGetter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-datastax Show documentation
Show all versions of sfm-datastax Show documentation
Cassandra Datastax SFM supports.
package org.simpleflatmapper.datastax.impl.getter;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.GettableByIndexData;
import org.simpleflatmapper.datastax.DataHelper;
import org.simpleflatmapper.datastax.DataTypeHelper;
import org.simpleflatmapper.reflect.Getter;
import org.simpleflatmapper.reflect.primitive.ShortGetter;
public class DatastaxGenericShortGetter implements ShortGetter, Getter {
private final int index;
private final DataType.Name dataTypeName;
public DatastaxGenericShortGetter(int index, DataType dataType) {
this.index = index;
this.dataTypeName = validateName(dataType);
}
private DataType.Name validateName(DataType dataType) {
final DataType.Name name = dataType.getName();
if (DataTypeHelper.isNumber(name)) {
return name;
}
throw new IllegalArgumentException("Datatype " + dataType + " not a number");
}
@Override
public Short get(GettableByIndexData target) throws Exception {
if (target.isNull(index)) {
return null;
}
return getShort(target);
}
@Override
public short getShort(GettableByIndexData target) throws Exception {
switch (dataTypeName) {
case BIGINT:
case COUNTER:
return (short)target.getLong(index);
case VARINT:
return target.getVarint(index).shortValue();
case INT:
return (short)target.getInt(index);
case DECIMAL:
return target.getDecimal(index).shortValue();
case FLOAT:
return (short)target.getFloat(index);
case DOUBLE:
return (short)target.getDouble(index);
}
if (DataTypeHelper.isSmallInt(dataTypeName)) return DataHelper.getShort(index, target);
if (DataTypeHelper.isTinyInt(dataTypeName)) return (short)DataHelper.getByte(index, target);
if (DataTypeHelper.isTime(dataTypeName)) return (short)DataHelper.getTime(index, target);
return 0;
}
}