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

Class ArbitraryOrdering

corpus/java/training/guava/collect/Ordering.java:306–361  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

304 }
305
306 @VisibleForTesting
307 static class ArbitraryOrdering extends Ordering<Object> {
308
309 @SuppressWarnings("deprecation") // TODO(kevinb): ?
310 private final Map<Object, Integer> uids =
311 Platform.tryWeakKeys(new MapMaker())
312 .makeComputingMap(
313 new Function<Object, Integer>() {
314 final AtomicInteger counter = new AtomicInteger(0);
315
316 @Override
317 public Integer apply(Object from) {
318 return counter.getAndIncrement();
319 }
320 });
321
322 @Override
323 public int compare(Object left, Object right) {
324 if (left == right) {
325 return 0;
326 } else if (left == null) {
327 return -1;
328 } else if (right == null) {
329 return 1;
330 }
331 int leftCode = identityHashCode(left);
332 int rightCode = identityHashCode(right);
333 if (leftCode != rightCode) {
334 return leftCode < rightCode ? -1 : 1;
335 }
336
337 // identityHashCode collision (rare, but not as rare as you'd think)
338 int result = uids.get(left).compareTo(uids.get(right));
339 if (result == 0) {
340 throw new AssertionError(); // extremely, extremely unlikely.
341 }
342 return result;
343 }
344
345 @Override
346 public String toString() {
347 return "Ordering.arbitrary()";
348 }
349
350 /*
351 * We need to be able to mock identityHashCode() calls for tests, because it
352 * can take 1-10 seconds to find colliding objects. Mocking frameworks that
353 * can do magic to mock static method calls still can't do so for a system
354 * class, so we need the indirection. In production, Hotspot should still
355 * recognize that the call is 1-morphic and should still be willing to
356 * inline it if necessary.
357 */
358 int identityHashCode(Object object) {
359 return System.identityHashCode(object);
360 }
361 }
362
363 // Constructor

Callers

nothing calls this directly

Calls 2

tryWeakKeysMethod · 0.95
makeComputingMapMethod · 0.45

Tested by

no test coverage detected