Fixes null captured parameters
[jpf-core.git] / src / main / gov / nasa / jpf / util / ObjArray.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18 package gov.nasa.jpf.util;
19
20 import java.util.Arrays;
21 import java.util.Iterator;
22 import java.util.NoSuchElementException;
23
24 /**
25  * Wrapper for arrays of objects which provides proper equals() and hashCode()
26  * methods, and behaves nicely with Java 1.5 generics. 
27  */
28 public final class ObjArray<E> implements ReadOnlyObjList<E>, Iterable<E>, Cloneable  {
29   final Object[] data;
30
31   public ObjArray(int size) {
32     data = new Object[size];
33   }
34   
35   public ObjArray(E[] data) {
36     this.data = data;
37   }
38
39   @Override
40   public ObjArray<E> clone() {
41     return new ObjArray( data.clone());
42   }
43
44
45   public E[] toArray (E[] a) {
46     if (a.length >= data.length) {
47       System.arraycopy(data,0,a,0,data.length);
48       return a;
49     } else {
50       return null;
51     }
52   }
53   
54   
55   @Override
56 @SuppressWarnings("unchecked")
57   public E get(int idx) {
58     return (E) data[idx];
59   }
60   
61   public void set(int idx, E e) {
62     data[idx] = e;
63   }
64   
65   @Override
66   public int length() {
67     return data.length;
68   }
69   
70   @Override
71   public int hashCode() {
72     return Arrays.hashCode(data);
73   }
74   
75   @Override
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;
81     
82     // could cause NullPointerException for non-robust .equals 
83     // return Arrays.equals(thisData, thatData);
84     
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])) {
89         return false;
90       }
91     }
92     return true;
93   }
94
95   public void fill(E e) {
96     Arrays.fill(data, e);
97   }
98   
99   public void nullify () {
100     Arrays.fill(data, null);
101   }
102   
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);
106   }
107
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;
112   }
113   
114   @Override
115   public Iterator<E> iterator () {
116     return new Iterator<E>() {
117       int idx = 0;
118
119       @Override
120         public boolean hasNext () {
121         return idx < data.length;
122       }
123
124       @Override
125         @SuppressWarnings("unchecked")
126       public E next () {
127         if (idx >= data.length) throw new NoSuchElementException();
128         return (E) data[idx++];
129       }
130
131       @Override
132         public void remove () {
133         throw new UnsupportedOperationException();
134       }
135     };
136   }
137 }