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 org.apache.hbase.thirdparty.com.google.common.base.Throwables;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
026
027final class SimpleScanResultConsumer implements ScanResultConsumer {
028
029  private ScanMetrics scanMetrics;
030
031  private final List<Result> results = new ArrayList<>();
032
033  private Throwable error;
034
035  private boolean finished = false;
036
037  @Override
038  public void onScanMetricsCreated(ScanMetrics scanMetrics) {
039    this.scanMetrics = scanMetrics;
040  }
041
042  @Override
043  public synchronized boolean onNext(Result result) {
044    results.add(result);
045    return true;
046  }
047
048  @Override
049  public synchronized void onError(Throwable error) {
050    this.error = error;
051    finished = true;
052    notifyAll();
053  }
054
055  @Override
056  public synchronized void onComplete() {
057    finished = true;
058    notifyAll();
059  }
060
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  public ScanMetrics getScanMetrics() {
073    return scanMetrics;
074  }
075}