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

com.jd.blockchain.ledger.resolver.BooleanToBytesValueResolver Maven / Gradle / Ivy

There is a newer version: 1.6.5.RELEASE
Show newest version
package com.jd.blockchain.ledger.resolver;

import com.jd.blockchain.ledger.TypedValue;

import utils.Bytes;
import utils.io.BytesUtils;

import com.jd.blockchain.ledger.BytesValue;
import com.jd.blockchain.ledger.DataType;

import java.util.Set;

public class BooleanToBytesValueResolver extends AbstractBytesValueResolver {

	private final Class[] supportClasses = { Boolean.class, boolean.class };

	private final DataType[] supportDataTypes = { DataType.BOOLEAN };

	private final Set> convertClasses = initBooleanConvertSet();

	@Override
	public BytesValue encode(Object value, Class type) {
		if (!isSupport(type)) {
			throw new IllegalStateException(String.format("Un-support encode Class[%s] Object !!!", type.getName()));
		}
		return TypedValue.fromBoolean((boolean) value);
	}

	@Override
	public Class[] supportClasses() {
		return supportClasses;
	}

	@Override
	public DataType[] supportDataTypes() {
		return supportDataTypes;
	}

	@Override
	protected Object decode(Bytes value) {
		return BytesUtils.toBoolean(value.toBytes()[0]);
	}

	@Override
	public Object decode(BytesValue value, Class clazz) {
		boolean intVal = (boolean) decode(value);
		if (!convertClasses.contains(clazz)) {
			throw new IllegalStateException(String.format("Un-Support decode value to class[%s] !!!", clazz.getName()));
		}

		return intVal;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy