net.gdface.thrift.ThriftStructTransformer Maven / Gradle / Ivy
The newest version!
package net.gdface.thrift;
import static com.google.common.base.Preconditions.*;
import java.util.Map;
import com.facebook.swift.codec.metadata.ThriftCatalogWithTransformer;
import com.facebook.swift.codec.metadata.ThriftStructMetadata;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import static net.gdface.thrift.ThriftUtils.*;
/**
* 有{@link com.facebook.swift.codec.ThriftStruct}注释的类型之间的转换
* @author guyadong
*
* @param
* @param
*/
public class ThriftStructTransformer implements Function{
private final ThriftStructMetadata leftMetadata;
private final ThriftStructMetadata rightMetadata;
public ThriftStructTransformer(Class left, Class right) {
this.leftMetadata= ThriftCatalogWithTransformer.CATALOG.getThriftStructMetadata(checkNotNull(left,"left is null"));
this.rightMetadata= ThriftCatalogWithTransformer.CATALOG.getThriftStructMetadata(checkNotNull(right,"right is null"));
}
@Override
public R apply(L input) {
if(null == input){
return null;
}
try {
Map data = getFieldValues(input,leftMetadata);
return constructStruct(data,rightMetadata);
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder()
.append("ThriftStructTransformer [leftClass=")
.append(leftMetadata.getStructType())
.append(", rightClass=")
.append(rightMetadata.getStructType())
.append("]");
return builder.toString();
}
}