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

net.gdface.thrift.Swift2ThriftyExceptionTransformer Maven / Gradle / Ivy

There is a newer version: 2.10.3
Show newest version
package net.gdface.thrift;

import static com.google.common.base.Preconditions.*;
import static net.gdface.thrift.ThriftUtils.*;
import static net.gdface.thrift.ThriftyStructMetadata.STRUCTS_CACHE;

import java.util.Map;

import com.facebook.swift.codec.metadata.ThriftStructMetadata;
import com.google.common.base.Function;

/**
 * 有{@link com.facebook.swift.codec.ThriftStruct}注释的异常类型之间的转换
 * @author guyadong
 *
 * @param  thrift 生成的client异常类型
 * @param  实现 {@link ThriftDecorator}的装饰类异常类型
 */
public class Swift2ThriftyExceptionTransformer> 
			implements Function{
	private final ThriftStructMetadata leftMetadata;
	private final ThriftyStructMetadata rightMetadata;
	public Swift2ThriftyExceptionTransformer(Class left, Class right) {
		checkArgument(isThriftException(left) && isThriftyException(right),
				"left must be Exception with @com.facebook.swift.codec.ThriftStruct annotation,"
				+ "right must be Exception implement com.microsoft.thrifty.Struct interface"	);
		this.leftMetadata = CATALOG.getThriftStructMetadata(left);
		this.rightMetadata = STRUCTS_CACHE.getUnchecked(checkNotNull(right,"right is null"));
	}
	@Override
	public R apply(L input) {
		if(null == input){
			return null;
		}
		Map data = getFieldValues(input,leftMetadata);
		return rightMetadata.constructStruct(data);

	}

    @Override
	public String toString() {
		StringBuilder builder = new StringBuilder()
				.append("Swift2ThriftyExceptionTransformer [leftClass=")
				.append(leftMetadata.getStructType())
				.append(", rightClass=")
				.append(rightMetadata.getStructType())
				.append("]");
		return builder.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy