2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.util;
20 import java.util.Arrays;
21 import java.util.Iterator;
22 import java.util.NoSuchElementException;
25 * Wrapper for arrays of objects which provides proper equals() and hashCode()
26 * methods, and behaves nicely with Java 1.5 generics.
28 public final class ObjArray<E> implements ReadOnlyObjList<E>, Iterable<E>, Cloneable {
31 public ObjArray(int size) {
32 data = new Object[size];
35 public ObjArray(E[] data) {
40 public ObjArray<E> clone() {
41 return new ObjArray( data.clone());
45 public E[] toArray (E[] a) {
46 if (a.length >= data.length) {
47 System.arraycopy(data,0,a,0,data.length);
56 @SuppressWarnings("unchecked")
57 public E get(int idx) {
61 public void set(int idx, E e) {
71 public int hashCode() {
72 return Arrays.hashCode(data);
76 public boolean equals(Object o) {
77 if (this == o) return true;
78 if (! (o instanceof ObjArray)) return false;
79 Object[] thatData = ((ObjArray)o).data;
80 Object[] thisData = this.data;
82 // could cause NullPointerException for non-robust .equals
83 // return Arrays.equals(thisData, thatData);
85 if (thisData == thatData) return true;
86 if (thisData.length != thatData.length) return false;
87 for (int i = 0; i < thisData.length; i++) {
88 if (!Misc.equals(thisData[i], thatData[i])) {
95 public void fill(E e) {
99 public void nullify () {
100 Arrays.fill(data, null);
103 public static <T> void copy(ObjArray<? extends T> src, int srcPos,
104 ObjArray<T> dst, int dstPos, int len) {
105 System.arraycopy(src.data, srcPos, dst.data, dstPos, len);
108 static final ObjArray<Object> zero = new ObjArray<Object>(0);
109 @SuppressWarnings("unchecked")
110 public static <T> ObjArray<T> zeroLength() {
111 return (ObjArray<T>) zero;
115 public Iterator<E> iterator () {
116 return new Iterator<E>() {
120 public boolean hasNext () {
121 return idx < data.length;
125 @SuppressWarnings("unchecked")
127 if (idx >= data.length) throw new NoSuchElementException();
128 return (E) data[idx++];
132 public void remove () {
133 throw new UnsupportedOperationException();