| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PrimitiveArrayIterator |
|
| 2.6;2.6 |
| 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 | ||
| 24 | package org.webmacro.util; | |
| 25 | ||
| 26 | import java.util.Iterator; | |
| 27 | import java.util.NoSuchElementException; | |
| 28 | ||
| 29 | ||
| 30 | /** | |
| 31 | * This provides an iterator interface to an array of primitives. | |
| 32 | */ | |
| 33 | final public class PrimitiveArrayIterator implements Iterator | |
| 34 | { | |
| 35 | ||
| 36 | private final Object a; | |
| 37 | private int _size; | |
| 38 | private int pos; | |
| 39 | ||
| 40 | /** | |
| 41 | * Construct an iterator given an enumeration. | |
| 42 | */ | |
| 43 | public PrimitiveArrayIterator (Object array) | |
| 44 | 0 | { |
| 45 | 0 | if (!array.getClass().isArray()) |
| 46 | 0 | throw new IllegalArgumentException(array.getClass().getName() |
| 47 | + " is not an array."); | |
| 48 | 0 | this.a = array; |
| 49 | 0 | _size = java.lang.reflect.Array.getLength(a); |
| 50 | 0 | pos = 0; |
| 51 | 0 | } |
| 52 | ||
| 53 | /** | |
| 54 | * Return true if we have not yet reached the end of the enumeration. | |
| 55 | */ | |
| 56 | final public boolean hasNext () | |
| 57 | { | |
| 58 | 0 | return (pos < _size); |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * Advance the iterator and return the next value. Return null if we | |
| 63 | * reach the end of the enumeration. | |
| 64 | */ | |
| 65 | final public Object next () | |
| 66 | { | |
| 67 | 0 | if (pos < _size) |
| 68 | { | |
| 69 | 0 | return java.lang.reflect.Array.get(a, pos++); |
| 70 | } | |
| 71 | else | |
| 72 | { | |
| 73 | 0 | throw new NoSuchElementException("Advanced beyond end of array"); |
| 74 | } | |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * Unsupported. | |
| 79 | */ | |
| 80 | final public void remove () | |
| 81 | { | |
| 82 | 0 | throw new UnsupportedOperationException(); |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * Test harness | |
| 87 | */ | |
| 88 | static public void main (String arg[]) | |
| 89 | { | |
| 90 | ||
| 91 | try | |
| 92 | { | |
| 93 | 0 | Iterator i = new PrimitiveArrayIterator(arg); |
| 94 | 0 | while (i.hasNext()) |
| 95 | { | |
| 96 | 0 | System.out.println("item: " + i.next()); |
| 97 | } | |
| 98 | } | |
| 99 | 0 | catch (Exception e) |
| 100 | { | |
| 101 | 0 | e.printStackTrace(); |
| 102 | 0 | } |
| 103 | 0 | } |
| 104 | } | |
| 105 |