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

com.pingcap.tikv.util.FutureObserver Maven / Gradle / Ivy

There is a newer version: 3.2.3
Show newest version
/*
 * Copyright 2017 PingCAP, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pingcap.tikv.util;

import com.google.common.util.concurrent.SettableFuture;
import io.grpc.stub.StreamObserver;
import java.util.concurrent.Future;

public class FutureObserver implements StreamObserver {
  private final SettableFuture resultFuture;
  private final Getter getter;

  public FutureObserver(Getter getter) {
    this.resultFuture = SettableFuture.create();
    this.getter = getter;
  }

  public Value getValue(RespT resp) {
    return getter.getValue(resp);
  }

  @Override
  public void onNext(RespT resp) {
    resultFuture.set(getValue(resp));
  }

  @Override
  public void onError(Throwable t) {
    resultFuture.setException(t);
  }

  @Override
  public void onCompleted() {}

  public Future getFuture() {
    return resultFuture;
  }

  public interface Getter {
    Value getValue(RespT resp);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy