1
/* ***** BEGIN LICENSE BLOCK *****
3
* This file is part of TestPlan.
5
* TestPlan is free software: you can redistribute it and/or modify it under the
6
* terms of the GNU General Public License as published by the Free Software
7
* Foundation, version 3 of the License.
9
* TestPlan is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with TestPlan. If not, see <http://www.gnu.org/licenses/>.
17
* Copyright (C) 2010 edA-qa mort-ora-y
20
* edA-qa mort-ora-y <edA-qa@disemia.com>
21
* ***** END LICENSE BLOCK ***** */
22
package testplan.util;
27
* Wraps an iterator to force type conversion on every item in the iteration.
29
public class TypeConvertIterator<T> implements Iterator<T> {
31
Iterator<Object> back; //the underlying iterator
32
Class<T> as; //which type to convert as
33
boolean allowNull; //if false then conversion which result in null throw an exception
35
public TypeConvertIterator( Class<T> as, Iterator<Object> back, boolean allowNull ) {
38
this.allowNull = allowNull;
41
public boolean hasNext() {
42
return back.hasNext();
46
Object next = back.next();
49
res = TypeConvert.asType( as, next );
50
} catch( Exception ex ) {
51
throw new RuntimeException( ex );
53
if( res == null && !allowNull )
54
TypeConvert.throwConvertError( next, as );
59
public void remove() {
60
//given how this is used the backing iterator may be just a copy or view, thus removing
61
//wouldn'twork as intended in many cases
62
throw new TestPlanException.Syntax( "remove cannot be called on TypeConvertIterators",
63
"Parsing-TypeConvertIterator" );