2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.util;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Verify;
12 import com.google.common.cache.Cache;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.cache.CacheLoader;
15 import com.google.common.cache.LoadingCache;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.collect.ImmutableMap.Builder;
19 import com.google.common.collect.ImmutableSet;
20 import java.lang.reflect.Array;
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.List;
26 import javax.annotation.Nonnull;
28 final class OffsetMapCache {
30 * Cache for offsets where order matters. The key is a List, which defines the iteration order. Since we want
31 * to retain this order, it is okay to use a simple LoadingCache.
33 private static final LoadingCache<List<?>, Map<?, Integer>> ORDERED_CACHE =
34 CacheBuilder.newBuilder().weakValues().build(new CacheLoader<List<?>, Map<?, Integer>>() {
36 public Map<?, Integer> load(@Nonnull final List<?> key) {
37 return createMap(key);
41 * Cache for offsets where order does not mapper. The key is a Set of elements. We use manual two-stage loading
42 * because of the nature of the objects we store as values, which is ImmutableMaps. An ImmutableMap, when queried
43 * for keys (as is done in ImmutableOffsetMap.keySet()), will instantiate an ImmutableSet to hold these keys. It
44 * would be wasteful to use one Set for lookup only to have the map have an exact copy.
46 * We perform the first look up using a Set (which may come from the user, for example via
47 * ImmutableOffsetMap.unorderedCopyOf()), hence potentially saving a copy operation. If we fail to find an entry,
48 * we construct the map and put it conditionally with Map.keySet() as the key. This will detect concurrent loading
49 * and also lead to the cache and the map sharing the same Set.
51 private static final Cache<Set<?>, Map<?, Integer>> UNORDERED_CACHE =
52 CacheBuilder.newBuilder().weakValues().build();
54 private OffsetMapCache() {
55 throw new UnsupportedOperationException();
59 static void invalidateCache() {
60 ORDERED_CACHE.invalidateAll();
61 UNORDERED_CACHE.invalidateAll();
64 @SuppressWarnings("unchecked")
65 static <T> Map<T, Integer> orderedOffsets(final Collection<T> args) {
66 if (args.size() == 1) {
67 return unorderedOffsets(args);
70 return (Map<T, Integer>) ORDERED_CACHE.getUnchecked(ImmutableList.copyOf(args));
73 static <T> Map<T, Integer> unorderedOffsets(final Collection<T> args) {
74 return unorderedOffsets(args instanceof Set ? (Set<T>)args : ImmutableSet.copyOf(args));
77 static <K, V> V[] adjustedArray(final Map<K, Integer> offsets, final List<K> keys, final V[] array) {
78 Verify.verify(offsets.size() == keys.size(), "Offsets %s do not match keys %s", offsets, keys);
80 // This relies on the fact that offsets has an ascending iterator
81 final Iterator<K> oi = offsets.keySet().iterator();
82 final Iterator<K> ki = keys.iterator();
84 while (oi.hasNext()) {
85 final K o = oi.next();
86 final K k = ki.next();
88 return adjustArray(offsets, keys, array);
95 private static <T> Map<T, Integer> createMap(final Collection<T> keys) {
96 final Builder<T, Integer> b = ImmutableMap.builder();
106 @SuppressWarnings("unchecked")
107 private static <T> Map<T, Integer> unorderedOffsets(final Set<T> args) {
108 final Map<T, Integer> existing = (Map<T, Integer>) UNORDERED_CACHE.getIfPresent(args);
109 if (existing != null) {
113 final Map<T, Integer> newMap = createMap(args);
114 final Map<?, Integer> raced = UNORDERED_CACHE.asMap().putIfAbsent(newMap.keySet(), newMap);
115 return raced == null ? newMap : (Map<T, Integer>)raced;
118 private static <K, V> V[] adjustArray(final Map<K, Integer> offsets, final List<K> keys, final V[] array) {
119 @SuppressWarnings("unchecked")
120 final V[] ret = (V[]) Array.newInstance(array.getClass().getComponentType(), array.length);
123 for (final K k : keys) {
124 final Integer o = Verify.verifyNotNull(offsets.get(k), "Key %s not present in offsets %s", k, offsets);