View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import java.io.IOException;
21  import java.util.Iterator;
22  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  
25  /**
26   * Helper class for custom client scanners.
27   */
28  @InterfaceAudience.Private
29  public abstract class AbstractClientScanner implements ResultScanner {
30  
31    @Override
32    public Iterator<Result> iterator() {
33      return new Iterator<Result>() {
34        // The next RowResult, possibly pre-read
35        Result next = null;
36  
37        // return true if there is another item pending, false if there isn't.
38        // this method is where the actual advancing takes place, but you need
39        // to call next() to consume it. hasNext() will only advance if there
40        // isn't a pending next().
41        public boolean hasNext() {
42          if (next == null) {
43            try {
44              next = AbstractClientScanner.this.next();
45              return next != null;
46            } catch (IOException e) {
47              throw new RuntimeException(e);
48            }
49          }
50          return true;
51        }
52  
53        // get the pending next item and advance the iterator. returns null if
54        // there is no next item.
55        public Result next() {
56          // since hasNext() does the real advancing, we call this to determine
57          // if there is a next before proceeding.
58          if (!hasNext()) {
59            return null;
60          }
61  
62          // if we get to here, then hasNext() has given us an item to return.
63          // we want to return the item and then null out the next pointer, so
64          // we use a temporary variable.
65          Result temp = next;
66          next = null;
67          return temp;
68        }
69  
70        public void remove() {
71          throw new UnsupportedOperationException();
72        }
73      };
74    }
75  }