Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
| 18 | package net.floodlightcontroller.storage; |
| 19 | |
| 20 | import java.util.Iterator; |
| 21 | import java.util.NoSuchElementException; |
| 22 | |
| 23 | /** Iterator wrapper for an IResultSet, useful for iterating through query |
| 24 | * results in an enhanced for (foreach) loop. |
| 25 | * |
| 26 | * Note that the iterator manipulates the state of the underlying IResultSet. |
| 27 | */ |
| 28 | public class ResultSetIterator implements Iterator<IResultSet> { |
| 29 | private IResultSet resultSet; |
| 30 | private boolean hasAnother; |
| 31 | private boolean peekedAtNext; |
| 32 | |
| 33 | public ResultSetIterator(IResultSet resultSet) { |
| 34 | this.resultSet = resultSet; |
| 35 | this.peekedAtNext = false; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public IResultSet next() { |
| 40 | if (!peekedAtNext) { |
| 41 | hasAnother = resultSet.next(); |
| 42 | } |
| 43 | peekedAtNext = false; |
| 44 | if (!hasAnother) |
| 45 | throw new NoSuchElementException(); |
| 46 | return resultSet; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public boolean hasNext() { |
| 51 | if (!peekedAtNext) { |
| 52 | hasAnother = resultSet.next(); |
| 53 | peekedAtNext = true; |
| 54 | } |
| 55 | return hasAnother; |
| 56 | } |
| 57 | |
| 58 | /** Row removal is not supported; use IResultSet.deleteRow instead. |
| 59 | */ |
| 60 | @Override |
| 61 | public void remove() { |
| 62 | throw new UnsupportedOperationException(); |
| 63 | } |
| 64 | } |