| 1 | /* |
| 2 | * JTiger Unit Testing Framework for J2SE 1.5 |
| 3 | * Copyright (C) 2005 Tony Morris |
| 4 | * |
| 5 | * This software is licenced under the |
| 6 | * Common Public Licence version 1.0 |
| 7 | * http://www.opensource.org/licenses/cpl1.0.php |
| 8 | * |
| 9 | * You received a copy of this licence with this software. |
| 10 | */ |
| 11 | package org.jtiger.framework; |
| 12 | |
| 13 | import java.util.Arrays; |
| 14 | import java.util.Iterator; |
| 15 | import java.util.NoSuchElementException; |
| 16 | import java.util.List; |
| 17 | |
| 18 | /** |
| 19 | * Returns instances of {@link ReadOnlyArray} which are backed by a raw array that cannot be modified. |
| 20 | * |
| 21 | * @deprecated Use {@link SequenceFactory} instead. |
| 22 | * @see SequenceFactory |
| 23 | * @author %javadoc.author.tag% |
| 24 | * @version %version%<br/> |
| 25 | * <i>Build Number %build.number%</i><br/> |
| 26 | * <i>Build Time %build.time% CET (GMT + 1)</i> |
| 27 | */ |
| 28 | @Deprecated |
| 29 | public final class ReadOnlyArrayFactory |
| 30 | { |
| 31 | private ReadOnlyArrayFactory() |
| 32 | { |
| 33 | |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Returns a new instance of {@link ReadOnlyArray}. |
| 38 | * |
| 39 | * @deprecated Use {@link SequenceFactory#newSequence} instead. |
| 40 | * @param a The array that backs the returned {@link ReadOnlyArray}. |
| 41 | * @return A new instance of {@link ReadOnlyArray}. |
| 42 | * @throws NullPointerException If <code>a</code> is <code>null</code>. |
| 43 | */ |
| 44 | @Deprecated |
| 45 | public static <E> ReadOnlyArray<E> newReadOnlyArray(final E... a) throws NullPointerException |
| 46 | { |
| 47 | if(a == null) |
| 48 | { |
| 49 | throw new NullPointerException(); |
| 50 | } |
| 51 | |
| 52 | return new ReadOnlyArrayImpl<E>(a); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns a new instance of {@link ReadOnlyArray}. |
| 57 | * |
| 58 | * @param l The {@link List} that backs the returned {@link ReadOnlyArray}. |
| 59 | * @return A new instance of {@link ReadOnlyArray}. |
| 60 | * @throws NullPointerException If <code>l</code> is <code>null</code>. |
| 61 | */ |
| 62 | public static <E> ReadOnlyArray<E> newReadOnlyArray(final List<E> l) throws NullPointerException |
| 63 | { |
| 64 | if(l == null) |
| 65 | { |
| 66 | throw new NullPointerException(); |
| 67 | } |
| 68 | |
| 69 | return new ListReadOnlyArray<E>(l); |
| 70 | } |
| 71 | |
| 72 | private static final class ReadOnlyArrayImpl<E> implements ReadOnlyArray<E> |
| 73 | { |
| 74 | private static final long serialVersionUID = 2L; |
| 75 | |
| 76 | private final E[] a; |
| 77 | |
| 78 | ReadOnlyArrayImpl(final E... a) |
| 79 | { |
| 80 | this.a = a; |
| 81 | } |
| 82 | |
| 83 | public E get(final int index) throws ArrayIndexOutOfBoundsException |
| 84 | { |
| 85 | if(index < 0 || index >= a.length) |
| 86 | { |
| 87 | throw new ArrayIndexOutOfBoundsException(index); |
| 88 | } |
| 89 | |
| 90 | return a[index]; |
| 91 | } |
| 92 | |
| 93 | public int length() |
| 94 | { |
| 95 | return a.length; |
| 96 | } |
| 97 | |
| 98 | public Iterator<E> iterator() |
| 99 | { |
| 100 | return new IteratorImpl(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String toString() |
| 105 | { |
| 106 | return Arrays.toString(a); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public boolean equals(final Object o) |
| 111 | { |
| 112 | if(this == o) |
| 113 | { |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | if(o == null || o.getClass() != ReadOnlyArrayImpl.class) |
| 118 | { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | return Arrays.equals(((ReadOnlyArrayImpl)o).a, a); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public int hashCode() |
| 127 | { |
| 128 | return Arrays.hashCode(a); |
| 129 | } |
| 130 | |
| 131 | public ReadOnlyArray<E> clone() |
| 132 | { |
| 133 | return new ReadOnlyArrayImpl<E>(a.clone()); |
| 134 | } |
| 135 | |
| 136 | private final class IteratorImpl implements Iterator<E> |
| 137 | { |
| 138 | private int counter; |
| 139 | |
| 140 | IteratorImpl() |
| 141 | { |
| 142 | |
| 143 | } |
| 144 | |
| 145 | public boolean hasNext() |
| 146 | { |
| 147 | return counter < a.length; |
| 148 | } |
| 149 | |
| 150 | public E next() |
| 151 | { |
| 152 | if(counter < a.length) |
| 153 | { |
| 154 | return a[counter++]; |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | throw new NoSuchElementException(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | public void remove() |
| 163 | { |
| 164 | throw new UnsupportedOperationException(); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private static final class ListReadOnlyArray<E> implements ReadOnlyArray<E> |
| 170 | { |
| 171 | private static final long serialVersionUID = 2L; |
| 172 | |
| 173 | private List<E> l; |
| 174 | |
| 175 | ListReadOnlyArray(final List<E> l) |
| 176 | { |
| 177 | this.l = l; |
| 178 | } |
| 179 | |
| 180 | public E get(final int index) throws ArrayIndexOutOfBoundsException |
| 181 | { |
| 182 | if(index < 0 || index >= l.size()) |
| 183 | { |
| 184 | throw new ArrayIndexOutOfBoundsException(index); |
| 185 | } |
| 186 | |
| 187 | return l.get(index); |
| 188 | } |
| 189 | |
| 190 | public int length() |
| 191 | { |
| 192 | return l.size(); |
| 193 | } |
| 194 | |
| 195 | public ReadOnlyArray<E> clone() |
| 196 | { |
| 197 | return newReadOnlyArray(l); |
| 198 | } |
| 199 | |
| 200 | public Iterator<E> iterator() |
| 201 | { |
| 202 | return l.iterator(); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public String toString() |
| 207 | { |
| 208 | return Arrays.toString(l.toArray()); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public boolean equals(final Object o) |
| 213 | { |
| 214 | if(this == o) |
| 215 | { |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | if(o == null || o.getClass() != ListReadOnlyArray.class) |
| 220 | { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | return ((ListReadOnlyArray)o).l.equals(l); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public int hashCode() |
| 229 | { |
| 230 | return l.hashCode(); |
| 231 | } |
| 232 | } |
| 233 | } |