~testplan-team/testplan/main

« back to all changes in this revision

Viewing changes to src/share/testplan/util/TypeConvertIterator.java

  • Committer: edA-qa mort-ora-y
  • Date: 2010-06-09 11:42:29 UTC
  • mto: This revision was merged to the branch mainline in revision 343.
  • Revision ID: eda-qa@disemia.com-20100609114229-yzuim03r8kqn6ei0
adding TypeConvertIterator

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: GPL 3.0
 
3
 * This file is part of TestPlan.
 
4
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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/>.
 
16
 *
 
17
 * Copyright (C) 2010 edA-qa mort-ora-y
 
18
 * 
 
19
 * Contributors:
 
20
 *              edA-qa mort-ora-y <edA-qa@disemia.com>
 
21
 * ***** END LICENSE BLOCK ***** */
 
22
package testplan.util;
 
23
 
 
24
import java.util.*;
 
25
 
 
26
/**
 
27
 * Wraps an iterator to force type conversion on every item in the iteration.
 
28
 */
 
29
public class TypeConvertIterator<T> implements Iterator<T> {
 
30
 
 
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
 
34
        
 
35
        public TypeConvertIterator( Class<T> as, Iterator<Object> back, boolean allowNull ) {
 
36
                this.back = back;
 
37
                this.as = as;
 
38
                this.allowNull = allowNull;
 
39
        }
 
40
        
 
41
        public boolean hasNext() {
 
42
                return back.hasNext();
 
43
        }
 
44
        
 
45
        public T next() {
 
46
                Object next = back.next();
 
47
                T res;
 
48
                try {
 
49
                        res = TypeConvert.asType( as, next );
 
50
                } catch( Exception ex ) {
 
51
                        throw new RuntimeException( ex );
 
52
                }
 
53
                if( res == null && !allowNull )
 
54
                        TypeConvert.throwConvertError( next, as );
 
55
                                
 
56
                return res;
 
57
        }
 
58
        
 
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" );
 
64
        }
 
65
}