32991
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
9 |
*
|
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
14 |
* accompanied this code).
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License version
|
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
|
22 |
* questions.
|
|
23 |
*/
|
|
24 |
|
|
25 |
/*
|
|
26 |
* This file is available under and governed by the GNU General Public
|
|
27 |
* License version 2 only, as published by the Free Software Foundation.
|
|
28 |
* However, the following notice accompanied the original version of this
|
|
29 |
* file:
|
|
30 |
*
|
|
31 |
* Written by Martin Buchholz with assistance from members of JCP
|
|
32 |
* JSR-166 Expert Group and released to the public domain, as
|
|
33 |
* explained at http://creativecommons.org/publicdomain/zero/1.0/
|
|
34 |
*/
|
|
35 |
|
|
36 |
package java.util.concurrent;
|
|
37 |
|
|
38 |
import java.util.Collection;
|
|
39 |
|
|
40 |
/** Shared implementation code for java.util.concurrent. */
|
|
41 |
class Helpers {
|
|
42 |
private Helpers() {} // non-instantiable
|
|
43 |
|
|
44 |
/**
|
|
45 |
* An implementation of Collection.toString() suitable for classes
|
|
46 |
* with locks. Instead of holding a lock for the entire duration of
|
|
47 |
* toString(), or acquiring a lock for each call to Iterator.next(),
|
|
48 |
* we hold the lock only during the call to toArray() (less
|
|
49 |
* disruptive to other threads accessing the collection) and follows
|
|
50 |
* the maxim "Never call foreign code while holding a lock".
|
|
51 |
*/
|
|
52 |
static String collectionToString(Collection<?> c) {
|
|
53 |
final Object[] a = c.toArray();
|
|
54 |
final int size = a.length;
|
|
55 |
if (size == 0)
|
|
56 |
return "[]";
|
|
57 |
int charLength = 0;
|
|
58 |
|
|
59 |
// Replace every array element with its string representation
|
|
60 |
for (int i = 0; i < size; i++) {
|
|
61 |
Object e = a[i];
|
|
62 |
// Extreme compatibility with AbstractCollection.toString()
|
|
63 |
String s = (e == c) ? "(this Collection)" : objectToString(e);
|
|
64 |
a[i] = s;
|
|
65 |
charLength += s.length();
|
|
66 |
}
|
|
67 |
|
|
68 |
return toString(a, size, charLength);
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Like Arrays.toString(), but caller guarantees that size > 0,
|
|
73 |
* each element with index 0 <= i < size is a non-null String,
|
|
74 |
* and charLength is the sum of the lengths of the input Strings.
|
|
75 |
*/
|
|
76 |
static String toString(Object[] a, int size, int charLength) {
|
|
77 |
// assert a != null;
|
|
78 |
// assert size > 0;
|
|
79 |
|
|
80 |
// Copy each string into a perfectly sized char[]
|
|
81 |
// Length of [ , , , ] == 2 * size
|
|
82 |
final char[] chars = new char[charLength + 2 * size];
|
|
83 |
chars[0] = '[';
|
|
84 |
int j = 1;
|
|
85 |
for (int i = 0; i < size; i++) {
|
|
86 |
if (i > 0) {
|
|
87 |
chars[j++] = ',';
|
|
88 |
chars[j++] = ' ';
|
|
89 |
}
|
|
90 |
String s = (String) a[i];
|
|
91 |
int len = s.length();
|
|
92 |
s.getChars(0, len, chars, j);
|
|
93 |
j += len;
|
|
94 |
}
|
|
95 |
chars[j] = ']';
|
|
96 |
// assert j == chars.length - 1;
|
|
97 |
return new String(chars);
|
|
98 |
}
|
|
99 |
|
|
100 |
/** Optimized form of: key + "=" + val */
|
|
101 |
static String mapEntryToString(Object key, Object val) {
|
|
102 |
final String k, v;
|
|
103 |
final int klen, vlen;
|
|
104 |
final char[] chars =
|
|
105 |
new char[(klen = (k = objectToString(key)).length()) +
|
|
106 |
(vlen = (v = objectToString(val)).length()) + 1];
|
|
107 |
k.getChars(0, klen, chars, 0);
|
|
108 |
chars[klen] = '=';
|
|
109 |
v.getChars(0, vlen, chars, klen + 1);
|
|
110 |
return new String(chars);
|
|
111 |
}
|
|
112 |
|
|
113 |
private static String objectToString(Object x) {
|
|
114 |
// Extreme compatibility with StringBuilder.append(null)
|
|
115 |
String s;
|
|
116 |
return (x == null || (s = x.toString()) == null) ? "null" : s;
|
|
117 |
}
|
|
118 |
}
|