Example usage for com.amazonaws.services.simpledb.util SimpleDBUtils encodeZeroPadding

List of usage examples for com.amazonaws.services.simpledb.util SimpleDBUtils encodeZeroPadding

Introduction

In this page you can find the example usage for com.amazonaws.services.simpledb.util SimpleDBUtils encodeZeroPadding.

Prototype

public static String encodeZeroPadding(float number, int maxNumDigits) 

Source Link

Document

Encodes positive float value into a string by zero-padding number up to the specified number of digits

Usage

From source file:wwutil.jsoda.DataUtil.java

License:Mozilla Public License

/** Caller should handle custom valueType first before calling this.
 * E.g. DynamoDB's Set<String> and Set<long> fields are encoded as Multi-Value AttributeValue.
 *///from  w  ww.  j  a v a2 s .co m
@SuppressWarnings("unchecked")
static String encodeValueToAttrStr(Object value, Class valueType) {
    if (value == null)
        return null; // Caller needs to handle null correctly, e.g. skip storing AttributeValue.

    if (valueType == String.class)
        return value.toString();

    // NOTE: Don't change encoding and padding once data have been created.  Different encoding will mess up sorting.
    // Stringify basic type and encode them for sorting.
    if (valueType == Byte.class || valueType == byte.class) {
        Byte casted = (Byte) ConvertUtils.convert(value, Byte.class);
        return SimpleDBUtils.encodeZeroPadding(casted.intValue(), 3); // 0-Padded for sorting
    } else if (valueType == Short.class || valueType == short.class) {
        Short casted = (Short) ConvertUtils.convert(value, Short.class);
        return SimpleDBUtils.encodeZeroPadding(casted.intValue(), 5); // 0-Padded for sorting
    } else if (valueType == Integer.class || valueType == int.class) {
        Integer casted = (Integer) ConvertUtils.convert(value, Integer.class);
        return SimpleDBUtils.encodeZeroPadding(casted.intValue(), 10); // 0-Padded for sorting
    } else if (valueType == Long.class || valueType == long.class) {
        Long casted = (Long) ConvertUtils.convert(value, Long.class);
        return SimpleDBUtils.encodeZeroPadding(casted.longValue(), 19); // 0-Padded for sorting
    } else if (valueType == Float.class || valueType == float.class) {
        Float casted = (Float) ConvertUtils.convert(value, Float.class);
        return SimpleDBUtils.encodeZeroPadding(casted.floatValue(), 16); // 0-Padded for sorting
    } else if (valueType == Double.class || valueType == double.class) {
        // SimpleDBUtils has no padding for double.  Just convert it to String.
        return value.toString();
    } else if (valueType == Boolean.class || valueType == boolean.class) {
        return value.toString();
    } else if (valueType == Character.class || valueType == char.class) {
        return value.toString();
    } else if (valueType == Date.class) {
        return SimpleDBUtils.encodeDate((Date) value);
    } else if (valueType.isEnum()) {
        return ((Enum) value).name();
    }

    // JSONify the rest.
    return toJson(value);
}