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 org.apache.hadoop.classification.InterfaceAudience;
21  import org.apache.hadoop.classification.InterfaceStability;
22  
23  import java.io.IOException;
24  import java.util.Iterator;
25  
26  /**
27   * Helper class for custom client scanners.
28   */
29  @InterfaceAudience.Public
30  @InterfaceStability.Stable
31  public abstract class AbstractClientScanner implements ResultScanner {
32  
33    @Override
34    public Iterator<Result> iterator() {
35      return new Iterator<Result>() {
36        // The next RowResult, possibly pre-read
37        Result next = null;
38  
39        // return true if there is another item pending, false if there isn't.
40        // this method is where the actual advancing takes place, but you need
41        // to call next() to consume it. hasNext() will only advance if there
42        // isn't a pending next().
43        public boolean hasNext() {
44          if (next == null) {
45            try {
46              next = AbstractClientScanner.this.next();
47              return next != null;
48            } catch (IOException e) {
49              throw new RuntimeException(e);
50            }
51          }
52          return true;
53        }
54  
55        // get the pending next item and advance the iterator. returns null if
56        // there is no next item.
57        public Result next() {
58          // since hasNext() does the real advancing, we call this to determine
59          // if there is a next before proceeding.
60          if (!hasNext()) {
61            return null;
62          }
63  
64          // if we get to here, then hasNext() has given us an item to return.
65          // we want to return the item and then null out the next pointer, so
66          // we use a temporary variable.
67          Result temp = next;
68          next = null;
69          return temp;
70        }
71  
72        public void remove() {
73          throw new UnsupportedOperationException();
74        }
75      };
76    }
77  }