7 |
|
package java.util.concurrent; |
8 |
|
|
9 |
|
import java.util.AbstractCollection; |
10 |
< |
import java.util.ArrayList; |
10 |
> |
import java.util.Arrays; |
11 |
|
import java.util.Collection; |
12 |
|
import java.util.Deque; |
13 |
|
import java.util.Iterator; |
781 |
|
} |
782 |
|
|
783 |
|
/** |
784 |
– |
* Creates an array list and fills it with elements of this list. |
785 |
– |
* Used by toArray. |
786 |
– |
* |
787 |
– |
* @return the array list |
788 |
– |
*/ |
789 |
– |
private ArrayList<E> toArrayList() { |
790 |
– |
ArrayList<E> list = new ArrayList<E>(); |
791 |
– |
for (Node<E> p = first(); p != null; p = succ(p)) { |
792 |
– |
E item = p.item; |
793 |
– |
if (item != null) |
794 |
– |
list.add(item); |
795 |
– |
} |
796 |
– |
return list; |
797 |
– |
} |
798 |
– |
|
799 |
– |
/** |
784 |
|
* Constructs an empty deque. |
785 |
|
*/ |
786 |
|
public ConcurrentLinkedDeque() { |
1194 |
|
; |
1195 |
|
} |
1196 |
|
|
1197 |
+ |
public String toString() { |
1198 |
+ |
String[] a = null; |
1199 |
+ |
restartFromHead: for (;;) { |
1200 |
+ |
int charLength = 0; |
1201 |
+ |
int size = 0; |
1202 |
+ |
for (Node<E> p = first(); p != null;) { |
1203 |
+ |
E item = p.item; |
1204 |
+ |
if (item != null) { |
1205 |
+ |
if (a == null) |
1206 |
+ |
a = new String[4]; |
1207 |
+ |
else if (size == a.length) |
1208 |
+ |
a = Arrays.copyOf(a, 2 * size); |
1209 |
+ |
String s = item.toString(); |
1210 |
+ |
a[size++] = s; |
1211 |
+ |
charLength += s.length(); |
1212 |
+ |
} |
1213 |
+ |
if (p == (p = p.next)) |
1214 |
+ |
continue restartFromHead; |
1215 |
+ |
} |
1216 |
+ |
|
1217 |
+ |
if (size == 0) |
1218 |
+ |
return "[]"; |
1219 |
+ |
|
1220 |
+ |
// Copy each string into a perfectly sized char[] |
1221 |
+ |
final char[] chars = new char[charLength + 2 * size]; |
1222 |
+ |
chars[0] = '['; |
1223 |
+ |
int j = 1; |
1224 |
+ |
for (int i = 0; i < size; i++) { |
1225 |
+ |
if (i > 0) { |
1226 |
+ |
chars[j++] = ','; |
1227 |
+ |
chars[j++] = ' '; |
1228 |
+ |
} |
1229 |
+ |
String s = a[i]; |
1230 |
+ |
int len = s.length(); |
1231 |
+ |
s.getChars(0, len, chars, j); |
1232 |
+ |
j += len; |
1233 |
+ |
} |
1234 |
+ |
chars[j] = ']'; |
1235 |
+ |
return new String(chars); |
1236 |
+ |
} |
1237 |
+ |
} |
1238 |
+ |
|
1239 |
+ |
private Object[] toArrayInternal(Object[] a) { |
1240 |
+ |
Object[] x = a; |
1241 |
+ |
restartFromHead: for (;;) { |
1242 |
+ |
int size = 0; |
1243 |
+ |
for (Node<E> p = first(); p != null;) { |
1244 |
+ |
E item = p.item; |
1245 |
+ |
if (item != null) { |
1246 |
+ |
if (x == null) |
1247 |
+ |
x = new Object[4]; |
1248 |
+ |
else if (size == x.length) |
1249 |
+ |
x = Arrays.copyOf(x, 2 * (size + 4)); |
1250 |
+ |
x[size++] = item; |
1251 |
+ |
} |
1252 |
+ |
if (p == (p = p.next)) |
1253 |
+ |
continue restartFromHead; |
1254 |
+ |
} |
1255 |
+ |
if (x == null) |
1256 |
+ |
return new Object[0]; |
1257 |
+ |
else if (a != null && size <= a.length) { |
1258 |
+ |
if (a != x) |
1259 |
+ |
System.arraycopy(x, 0, a, 0, size); |
1260 |
+ |
if (size < a.length) |
1261 |
+ |
a[size] = null; |
1262 |
+ |
return a; |
1263 |
+ |
} |
1264 |
+ |
return (size == x.length) ? x : Arrays.copyOf(x, size); |
1265 |
+ |
} |
1266 |
+ |
} |
1267 |
+ |
|
1268 |
|
/** |
1269 |
|
* Returns an array containing all of the elements in this deque, in |
1270 |
|
* proper sequence (from first to last element). |
1279 |
|
* @return an array containing all of the elements in this deque |
1280 |
|
*/ |
1281 |
|
public Object[] toArray() { |
1282 |
< |
return toArrayList().toArray(); |
1282 |
> |
return toArrayInternal(null); |
1283 |
|
} |
1284 |
|
|
1285 |
|
/** |
1319 |
|
* this deque |
1320 |
|
* @throws NullPointerException if the specified array is null |
1321 |
|
*/ |
1322 |
+ |
@SuppressWarnings("unchecked") |
1323 |
|
public <T> T[] toArray(T[] a) { |
1324 |
< |
return toArrayList().toArray(a); |
1324 |
> |
if (a == null) throw new NullPointerException(); |
1325 |
> |
return (T[]) toArrayInternal(a); |
1326 |
|
} |
1327 |
|
|
1328 |
|
/** |