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