MCPcopy Create free account
hub / github.com/antlr/codebuff / bulkGet

Method bulkGet

corpus/java/training/guava/util/concurrent/Striped.java:142–185  ·  view source on GitHub ↗

Returns the stripes that correspond to the passed objects, in ascending (as per #getAt(int)) order. Thus, threads that use the stripes in the order returned by this method are guaranteed to not deadlock each other. It should be noted that using a Striped with relatively few st

(Iterable<?> keys)

Source from the content-addressed store, hash-verified

140 * {@link #get(Object)}; may contain duplicates), in an increasing index order.
141 */
142 public Iterable<L> bulkGet(Iterable<?> keys) {
143 // Initially using the array to store the keys, then reusing it to store the respective L's
144 final Object[] array = Iterables.toArray(keys, Object.class);
145 if (array.length == 0) {
146 return ImmutableList.of();
147 }
148 int[] stripes = new int[array.length];
149 for (int i = 0; i < array.length; i++) {
150 stripes[i] = indexFor(array[i]);
151 }
152 Arrays.sort(stripes);
153 // optimize for runs of identical stripes
154 int previousStripe = stripes[0];
155 array[0] = getAt(previousStripe);
156 for (int i = 1; i < array.length; i++) {
157 int currentStripe = stripes[i];
158 if (currentStripe == previousStripe) {
159 array[i] = array[i - 1];
160 } else {
161 array[i] = getAt(currentStripe);
162 previousStripe = currentStripe;
163 }
164 }
165 /*
166 * Note that the returned Iterable holds references to the returned stripes, to avoid
167 * error-prone code like:
168 *
169 * Striped<Lock> stripedLock = Striped.lazyWeakXXX(...)'
170 * Iterable<Lock> locks = stripedLock.bulkGet(keys);
171 * for (Lock lock : locks) {
172 * lock.lock();
173 * }
174 * operation();
175 * for (Lock lock : locks) {
176 * lock.unlock();
177 * }
178 *
179 * If we only held the int[] stripes, translating it on the fly to L's, the original locks might
180 * be garbage collected after locking them, ending up in a huge mess.
181 */
182 @SuppressWarnings("unchecked") // we carefully replaced all keys with their respective L's
183 List<L> asList = (List<L>) Arrays.asList(array);
184 return Collections.unmodifiableList(asList);
185 }
186
187 // Static factories
188

Callers

nothing calls this directly

Calls 6

toArrayMethod · 0.95
ofMethod · 0.95
indexForMethod · 0.95
getAtMethod · 0.95
sortMethod · 0.80
asListMethod · 0.45

Tested by

no test coverage detected