001// ______________________________________________________
002// Generated by codegen - https://gitee.com/l0km/codegen 
003// template: thrift/client/perservice/client.async.decorator.class.vm
004// ______________________________________________________
005
006
007package net.gdface.sdk.fse.thrift;
008import java.nio.ByteBuffer;
009import net.gdface.sdk.fse.CodeBean;
010import net.gdface.sdk.fse.FeatureSe;
011import net.gdface.thrift.TypeTransformer;
012
013import com.google.common.base.Function;
014import com.google.common.net.HostAndPort;
015import com.google.common.util.concurrent.FutureCallback;
016import com.google.common.util.concurrent.Futures;
017import com.google.common.util.concurrent.ListenableFuture;
018import net.gdface.thrift.ClientFactory;
019import static com.google.common.base.Preconditions.*;
020
021/**
022 * 基于thrift/swift框架生成的client端代码提供{@link FeatureSe}接口的异步RPC实现(线程安全)<br>
023 * 转发所有{@link FeatureSe}接口方法到{@link #delegate()}指定的实例<br>
024 * 所有服务端抛出的{@link RuntimeException}异常被封装到{@link ServiceRuntimeException}中抛出<br>
025 * Example:<br>
026 * <pre>
027 *        FeatureSeThriftClientAsync thriftInstance = ClientFactory
028 *            .builder()
029 *            .setHostAndPort("127.0.0.1",26413)
030 *            .build(FeatureSe.class, FeatureSeThriftClientAsync.class);
031 * </pre>
032 * 计算机生成代码(generated by automated tools ThriftServiceDecoratorGenerator @author guyadong)<br>
033 * @author guyadong
034 *
035 */
036public class FeatureSeThriftClientAsync {
037    private final ClientFactory factory;
038
039    public ClientFactory getFactory() {
040        return factory;
041    }
042    public FeatureSeThriftClientAsync(ClientFactory factory) {
043        super();
044        this.factory = checkNotNull(factory,"factory is null");
045    }
046    /**
047     * @param host RPC service host
048     * @param port RPC service port 
049     */
050    public FeatureSeThriftClientAsync(String host,int port) {
051        this(ClientFactory.builder().setHostAndPort(host,port));
052    }
053    /**
054     * @param hostAndPort RPC service host and port 
055     */
056    public FeatureSeThriftClientAsync(HostAndPort hostAndPort) {
057        this(ClientFactory.builder().setHostAndPort(hostAndPort));
058    }
059    /**
060     * test if connectable for RPC service
061     * @return return {@code true} if connectable ,otherwise {@code false}
062     */
063    public boolean testConnect(){
064        return factory.testConnect();
065    }
066
067    /**
068     * @return 返回{@link net.gdface.sdk.fse.thrift.client.FeatureSe.Async}实例
069     */
070    protected net.gdface.sdk.fse.thrift.client.FeatureSe.Async delegate() {
071        return factory.applyInstance(net.gdface.sdk.fse.thrift.client.FeatureSe.Async.class);
072    }
073    @Override
074    public String toString() {
075        StringBuilder builder = new StringBuilder();
076        builder.append("FeatureSeThriftClientAsync [factory=");
077        builder.append(factory);
078        builder.append(",interface=");
079        builder.append(FeatureSe.class.getName());
080        builder.append("]");
081        return builder.toString();
082    }
083    /**
084     * 默认的{@link FutureCallback}实现
085     * @author guyadong
086     *
087     * @param <V>
088     */
089    public static class DefaultCallback<V> implements FutureCallback<V>{
090        @Override
091        public void onSuccess(V result) {
092            // DO NOTHING
093        }
094        @Override
095        public void onFailure(Throwable t) {
096            try{
097                throw t;
098            }
099            catch(net.gdface.sdk.fse.thrift.client.ServiceRuntimeException e){
100                onServiceRuntimeException(e);
101            }
102            catch(Throwable e){
103                onThrowable(e);
104            }
105        }
106        protected void onServiceRuntimeException(net.gdface.sdk.fse.thrift.client.ServiceRuntimeException e){
107            System.out.println(e.getServiceStackTraceMessage());
108        }
109        protected void onThrowable(Throwable e){
110            e.printStackTrace();
111        }
112    }
113    /**
114     * see also {@link net.gdface.sdk.fse.FeatureSe#addFeature(byte[],byte[],java.lang.String)}
115     */
116    public ListenableFuture<Boolean> addFeature(byte[] featureId,
117        byte[] feature,
118        String imgMD5){        
119        net.gdface.sdk.fse.thrift.client.FeatureSe.Async async = delegate();
120        ListenableFuture<Boolean> future = async.addFeatureToFse(featureId,
121            feature,
122            imgMD5);
123        return factory.wrap(async,future);
124    }
125    public void addFeature(byte[] featureId,
126        byte[] feature,
127        String imgMD5,
128        FutureCallback<Boolean>callback){
129        factory.addCallback(addFeature(featureId,feature,imgMD5), callback);
130    }
131    /**
132     * see also {@link net.gdface.sdk.fse.FeatureSe#addFeature(byte[],byte[],long)}
133     */
134    public ListenableFuture<Boolean> addFeature(byte[] featureId,
135        byte[] feature,
136        long appid){        
137        net.gdface.sdk.fse.thrift.client.FeatureSe.Async async = delegate();
138        ListenableFuture<Boolean> future = async.addFeatureToFseWithAppId(featureId,
139            feature,
140            appid);
141        return factory.wrap(async,future);
142    }
143    public void addFeature(byte[] featureId,
144        byte[] feature,
145        long appid,
146        FutureCallback<Boolean>callback){
147        factory.addCallback(addFeature(featureId,feature,appid), callback);
148    }
149    /**
150     * see also {@link net.gdface.sdk.fse.FeatureSe#clearAll()}
151     */
152    public ListenableFuture<Void> clearAll(){        
153        net.gdface.sdk.fse.thrift.client.FeatureSe.Async async = delegate();
154        ListenableFuture<Void> future = async.clearAllOfFse();
155        return factory.wrap(async,future);
156    }
157    public void clearAll(FutureCallback<Void>callback){
158        factory.addCallback(clearAll(), callback);
159    }
160    /**
161     * see also {@link net.gdface.sdk.fse.FeatureSe#getFeature(byte[])}
162     */
163    public ListenableFuture<CodeBean> getFeature(byte[] featureId){        
164        net.gdface.sdk.fse.thrift.client.FeatureSe.Async async = delegate();
165        ListenableFuture<CodeBean> future = Futures.transform(
166            async.getFeatureFromFse(featureId),
167            new Function<net.gdface.sdk.fse.thrift.client.CodeBean,CodeBean>(){
168                @Override
169                public CodeBean apply(net.gdface.sdk.fse.thrift.client.CodeBean input) {
170                    return TypeTransformer.getInstance().to(
171                    input,
172                    net.gdface.sdk.fse.thrift.client.CodeBean.class,
173                    CodeBean.class);
174                }
175            });
176        return factory.wrap(async,future);
177    }
178    public void getFeature(byte[] featureId,
179        FutureCallback<CodeBean>callback){
180        factory.addCallback(getFeature(featureId), callback);
181    }
182    /**
183     * see also {@link net.gdface.sdk.fse.FeatureSe#getFeatureByHex(java.lang.String)}
184     */
185    public ListenableFuture<CodeBean> getFeatureByHex(String featureId){        
186        net.gdface.sdk.fse.thrift.client.FeatureSe.Async async = delegate();
187        ListenableFuture<CodeBean> future = Futures.transform(
188            async.getFeatureByHexFromFse(featureId),
189            new Function<net.gdface.sdk.fse.thrift.client.CodeBean,CodeBean>(){
190                @Override
191                public CodeBean apply(net.gdface.sdk.fse.thrift.client.CodeBean input) {
192                    return TypeTransformer.getInstance().to(
193                    input,
194                    net.gdface.sdk.fse.thrift.client.CodeBean.class,
195                    CodeBean.class);
196                }
197            });
198        return factory.wrap(async,future);
199    }
200    public void getFeatureByHex(String featureId,
201        FutureCallback<CodeBean>callback){
202        factory.addCallback(getFeatureByHex(featureId), callback);
203    }
204    /**
205     * see also {@link net.gdface.sdk.fse.FeatureSe#removeFeature(byte[])}
206     */
207    public ListenableFuture<Boolean> removeFeature(byte[] featureId){        
208        net.gdface.sdk.fse.thrift.client.FeatureSe.Async async = delegate();
209        ListenableFuture<Boolean> future = async.removeFeatureFromFse(featureId);
210        return factory.wrap(async,future);
211    }
212    public void removeFeature(byte[] featureId,
213        FutureCallback<Boolean>callback){
214        factory.addCallback(removeFeature(featureId), callback);
215    }
216    /**
217     * see also {@link net.gdface.sdk.fse.FeatureSe#removeFeatureByHex(java.lang.String)}
218     */
219    public ListenableFuture<Boolean> removeFeatureByHex(String featureId){        
220        net.gdface.sdk.fse.thrift.client.FeatureSe.Async async = delegate();
221        ListenableFuture<Boolean> future = async.removeFeatureByHexFromFse(featureId);
222        return factory.wrap(async,future);
223    }
224    public void removeFeatureByHex(String featureId,
225        FutureCallback<Boolean>callback){
226        factory.addCallback(removeFeatureByHex(featureId), callback);
227    }
228    /**
229     * see also {@link net.gdface.sdk.fse.FeatureSe#searchCode(byte[],double,int)}
230     */
231    public ListenableFuture<CodeBean[]> searchCode(byte[] code,
232        double sim,
233        int rows){        
234        net.gdface.sdk.fse.thrift.client.FeatureSe.Async async = delegate();
235        ListenableFuture<CodeBean[]> future = Futures.transform(
236            async.searchCodeFromFse(code,
237            sim,
238            rows),
239            new Function<java.util.List<net.gdface.sdk.fse.thrift.client.CodeBean>,CodeBean[]>(){
240                @Override
241                public CodeBean[] apply(java.util.List<net.gdface.sdk.fse.thrift.client.CodeBean> input) {
242                    return TypeTransformer.getInstance().toArray(
243                    input,
244                    net.gdface.sdk.fse.thrift.client.CodeBean.class,
245                    CodeBean.class);
246                }
247            });
248        return factory.wrap(async,future);
249    }
250    public void searchCode(byte[] code,
251        double sim,
252        int rows,
253        FutureCallback<CodeBean[]>callback){
254        factory.addCallback(searchCode(code,sim,rows), callback);
255    }
256    /**
257     * see also {@link net.gdface.sdk.fse.FeatureSe#size()}
258     */
259    public ListenableFuture<Integer> size(){        
260        net.gdface.sdk.fse.thrift.client.FeatureSe.Async async = delegate();
261        ListenableFuture<Integer> future = async.sizeOfFse();
262        return factory.wrap(async,future);
263    }
264    public void size(FutureCallback<Integer>callback){
265        factory.addCallback(size(), callback);
266    }
267}