| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ArrayIterator |
|
| 2.2;2.2 |
| 1 | /* | |
| 2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
| 3 | * | |
| 4 | * Redistribution and use in source and binary forms, with or without | |
| 5 | * modification, are permitted under the terms of either of the following | |
| 6 | * Open Source licenses: | |
| 7 | * | |
| 8 | * The GNU General Public License, version 2, or any later version, as | |
| 9 | * published by the Free Software Foundation | |
| 10 | * (http://www.fsf.org/copyleft/gpl.html); | |
| 11 | * | |
| 12 | * or | |
| 13 | * | |
| 14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
| 15 | * | |
| 16 | * This software is provided "as is", with NO WARRANTY, not even the | |
| 17 | * implied warranties of fitness to purpose, or merchantability. You | |
| 18 | * assume all risks and liabilities associated with its use. | |
| 19 | * | |
| 20 | * See www.webmacro.org for more information on the WebMacro project. | |
| 21 | */ | |
| 22 | ||
| 23 | package org.webmacro.util; | |
| 24 | ||
| 25 | import java.util.Iterator; | |
| 26 | import java.util.NoSuchElementException; | |
| 27 | ||
| 28 | /** | |
| 29 | * An Iterator interface to an array. | |
| 30 | */ | |
| 31 | final public class ArrayIterator implements Iterator | |
| 32 | { | |
| 33 | ||
| 34 | final Object[] a; | |
| 35 | int pos; | |
| 36 | ||
| 37 | /** | |
| 38 | * Construct an iterator given an enumeration. | |
| 39 | */ | |
| 40 | public ArrayIterator (Object[] array) | |
| 41 | 479 | { |
| 42 | 479 | this.a = array; |
| 43 | 479 | pos = 0; |
| 44 | 479 | } |
| 45 | ||
| 46 | /** | |
| 47 | * @return true if we have not yet reached the end of the enumeration. | |
| 48 | */ | |
| 49 | final public boolean hasNext () | |
| 50 | { | |
| 51 | 2333 | return (pos < a.length); |
| 52 | } | |
| 53 | ||
| 54 | /** | |
| 55 | * Advance the iterator and return the next value. Return null if we | |
| 56 | * reach the end of the enumeration. | |
| 57 | */ | |
| 58 | final public Object next () | |
| 59 | { | |
| 60 | 1959 | if (pos < a.length) |
| 61 | { | |
| 62 | 1959 | return a[pos++]; |
| 63 | } | |
| 64 | else | |
| 65 | { | |
| 66 | 0 | throw new NoSuchElementException("Advanced beyond end of array"); |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * Unsupported. | |
| 72 | */ | |
| 73 | final public void remove () | |
| 74 | { | |
| 75 | 0 | throw new UnsupportedOperationException(); |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Test harness. | |
| 80 | */ | |
| 81 | static public void main (String arg[]) | |
| 82 | { | |
| 83 | ||
| 84 | try | |
| 85 | { | |
| 86 | 0 | Iterator i = new ArrayIterator(arg); |
| 87 | 0 | while (i.hasNext()) |
| 88 | { | |
| 89 | 0 | System.out.println("item: " + i.next()); |
| 90 | } | |
| 91 | } | |
| 92 | 0 | catch (Exception e) |
| 93 | { | |
| 94 | 0 | e.printStackTrace(); |
| 95 | 0 | } |
| 96 | 0 | } |
| 97 | } | |
| 98 |