001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import java.util.ArrayList;
021import java.util.List;
022import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
023
024import org.apache.hbase.thirdparty.com.google.common.base.Throwables;
025
026class SimpleScanResultConsumerImpl implements SimpleScanResultConsumer {
027
028  private ScanMetrics scanMetrics;
029
030  protected final List<Result> results = new ArrayList<>();
031
032  private Throwable error;
033
034  private boolean finished = false;
035
036  @Override
037  public void onScanMetricsCreated(ScanMetrics scanMetrics) {
038    this.scanMetrics = scanMetrics;
039  }
040
041  @Override
042  public synchronized boolean onNext(Result result) {
043    results.add(result);
044    return true;
045  }
046
047  @Override
048  public synchronized void onError(Throwable error) {
049    this.error = error;
050    finished = true;
051    notifyAll();
052  }
053
054  @Override
055  public synchronized void onComplete() {
056    finished = true;
057    notifyAll();
058  }
059
060  @Override
061  public synchronized List<Result> getAll() throws Exception {
062    while (!finished) {
063      wait();
064    }
065    if (error != null) {
066      Throwables.propagateIfPossible(error, Exception.class);
067      throw new Exception(error);
068    }
069    return results;
070  }
071
072  @Override
073  public ScanMetrics getScanMetrics() {
074    return scanMetrics;
075  }
076}