All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.fastorm.rdb.codec.BooleanValueCodec Maven / Gradle / Ivy

The newest version!
package org.zodiac.fastorm.rdb.codec;

import org.zodiac.fastorm.core.ValueCodec;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

import java.sql.JDBCType;
import java.sql.SQLType;

public class BooleanValueCodec implements ValueCodec {

    private SQLType sqlType;

    public BooleanValueCodec(SQLType sqlType) {
        this.sqlType = sqlType;
    }

    @Override
    public Object encode(Object value) {
        if (value == null) {
            return null;
        }
        if (!(value instanceof Boolean)) {;
            //value = "1".equals(String.valueOf(value)) || "true".equals(String.valueOf(value));
            value = StrUtil.TRUE_SET.contains(String.valueOf(value));
        }

        if (Boolean.TRUE.equals(value)) {
            return sqlType == JDBCType.BOOLEAN ? true : 1;
        }
        if (Boolean.FALSE.equals(value)) {
            return sqlType == JDBCType.BOOLEAN ? false : 0;
        }
        return value;
    }

    @Override
    public Boolean decode(Object data) {
        if (null == data) {
            return false;
        }
        if (data instanceof Boolean) {
            return (Boolean) data;
        }
        //return "1".equals(String.valueOf(data)) || "true".equals(String.valueOf(data));
        return StrUtil.TRUE_SET.contains(String.valueOf(data));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy