+ @VisibleForTesting
+ static void invalidateCache() {
+ ORDERED_CACHE.invalidateAll();
+ UNORDERED_CACHE.invalidateAll();
+ }
+
+ @SuppressWarnings("unchecked")
+ static <T> Map<T, Integer> orderedOffsets(final Collection<T> args) {
+ if (args.size() == 1) {
+ return unorderedOffsets(args);
+ }
+
+ return (Map<T, Integer>) ORDERED_CACHE.getUnchecked(ImmutableList.copyOf(args));
+ }
+
+ static <T> Map<T, Integer> unorderedOffsets(final Collection<T> args) {
+ return unorderedOffsets(args instanceof Set ? (Set<T>)args : ImmutableSet.copyOf(args));
+ }
+
+ static <K, V> V[] adjustedArray(final Map<K, Integer> offsets, final List<K> keys, final V[] array) {
+ Verify.verify(offsets.size() == keys.size(), "Offsets %s do not match keys %s", offsets, keys);
+
+ // This relies on the fact that offsets has an ascending iterator
+ final Iterator<K> oi = offsets.keySet().iterator();
+ final Iterator<K> ki = keys.iterator();
+
+ while (oi.hasNext()) {
+ final K o = oi.next();
+ final K k = ki.next();
+ if (!k.equals(o)) {
+ return adjustArray(offsets, keys, array);
+ }
+ }
+
+ return array;
+ }
+
+ private static <T> Map<T, Integer> createMap(final Collection<T> keys) {
+ final Builder<T, Integer> b = ImmutableMap.builder();
+ int i = 0;
+
+ for (T arg : keys) {
+ b.put(arg, i++);
+ }
+
+ return b.build();
+ }
+