@Parameters public static Collection<Object[]> parameters() { Comparator<String> nullsLast = Ordering.natural().nullsLast(); return Arrays.asList( new Object[][] { {new MapIteratorCache<String, String>(new HashMap<String, String>())}, {new MapIteratorCache<String, String>(new TreeMap<String, String>(nullsLast))}, {new MapRetrievalCache<String, String>(new HashMap<String, String>())}, {new MapRetrievalCache<String, String>(new TreeMap<String, String>(nullsLast))} }); }
@SuppressWarnings("unchecked") // Safe because we only cast if key is found in map. @Override public V get(@Nullable Object key) { V value = getIfCached(key); if (value != null) { return value; } value = getWithoutCaching(key); if (value != null) { addToCache((K) key, value); } return value; }
private void addToCache(K key, V value) { addToCache(new CacheEntry<K, V>(key, value)); }
private void addToCache(K key, V value) { addToCache(new CacheEntry<K, V>(key, value)); }
@SuppressWarnings("unchecked") // Safe because we only cast if key is found in map. @Override public V get(@NullableDecl Object key) { V value = getIfCached(key); if (value != null) { return value; } value = getWithoutCaching(key); if (value != null) { addToCache((K) key, value); } return value; }
/** * Constructs a graph with the properties specified in {@code builder}, initialized with the given * node map. */ ConfigurableValueGraph( AbstractGraphBuilder<? super N> builder, Map<N, GraphConnections<N, V>> nodeConnections, long edgeCount) { this.isDirected = builder.directed; this.allowsSelfLoops = builder.allowsSelfLoops; this.nodeOrder = builder.nodeOrder.cast(); // Prefer the heavier "MapRetrievalCache" for nodes if lookup is expensive. this.nodeConnections = (nodeConnections instanceof TreeMap) ? new MapRetrievalCache<N, GraphConnections<N, V>>(nodeConnections) : new MapIteratorCache<N, GraphConnections<N, V>>(nodeConnections); this.edgeCount = checkNonNegative(edgeCount); }
private void addToCache(K key, V value) { addToCache(new CacheEntry<K, V>(key, value)); }
@SuppressWarnings("unchecked") // Safe because we only cast if key is found in map. @Override public V get(@NullableDecl Object key) { V value = getIfCached(key); if (value != null) { return value; } value = getWithoutCaching(key); if (value != null) { addToCache((K) key, value); } return value; }
/** * Constructs a graph with the properties specified in {@code builder}, initialized with the given * node and edge maps. */ ConfigurableNetwork( NetworkBuilder<? super N, ? super E> builder, Map<N, NetworkConnections<N, E>> nodeConnections, Map<E, N> edgeToReferenceNode) { this.isDirected = builder.directed; this.allowsParallelEdges = builder.allowsParallelEdges; this.allowsSelfLoops = builder.allowsSelfLoops; this.nodeOrder = builder.nodeOrder.cast(); this.edgeOrder = builder.edgeOrder.cast(); // Prefer the heavier "MapRetrievalCache" for nodes if lookup is expensive. This optimizes // methods that access the same node(s) repeatedly, such as Graphs.removeEdgesConnecting(). this.nodeConnections = (nodeConnections instanceof TreeMap) ? new MapRetrievalCache<N, NetworkConnections<N, E>>(nodeConnections) : new MapIteratorCache<N, NetworkConnections<N, E>>(nodeConnections); this.edgeToReferenceNode = new MapIteratorCache<>(edgeToReferenceNode); }
@Override protected V getIfCached(@Nullable Object key) { V value = super.getIfCached(key); if (value != null) { return value; } // Store a local reference to the cache entry. If the backing map is immutable, this, // in combination with immutable cache entries, will ensure a thread-safe cache. CacheEntry<K, V> entry; // Check cache. We use == on purpose because it's cheaper and a cache miss is ok. entry = cacheEntry1; if (entry != null && entry.key == key) { return entry.value; } entry = cacheEntry2; if (entry != null && entry.key == key) { // Promote second cache entry to first so the access pattern // [K1, K2, K1, K3, K1, K4...] still hits the cache half the time. addToCache(entry); return entry.value; } return null; }
@SuppressWarnings("unchecked") // Safe because we only cast if key is found in map. @Override public V get(@NullableDecl Object key) { V value = getIfCached(key); if (value != null) { return value; } value = getWithoutCaching(key); if (value != null) { addToCache((K) key, value); } return value; }
/** * Constructs a graph with the properties specified in {@code builder}, initialized with the given * node map. */ ConfigurableValueGraph( AbstractGraphBuilder<? super N> builder, Map<N, GraphConnections<N, V>> nodeConnections, long edgeCount) { this.isDirected = builder.directed; this.allowsSelfLoops = builder.allowsSelfLoops; this.nodeOrder = builder.nodeOrder.cast(); // Prefer the heavier "MapRetrievalCache" for nodes if lookup is expensive. this.nodeConnections = (nodeConnections instanceof TreeMap) ? new MapRetrievalCache<N, GraphConnections<N, V>>(nodeConnections) : new MapIteratorCache<N, GraphConnections<N, V>>(nodeConnections); this.edgeCount = checkNonNegative(edgeCount); }
@Override protected V getIfCached(@NullableDecl Object key) { V value = super.getIfCached(key); if (value != null) { return value; } // Store a local reference to the cache entry. If the backing map is immutable, this, // in combination with immutable cache entries, will ensure a thread-safe cache. CacheEntry<K, V> entry; // Check cache. We use == on purpose because it's cheaper and a cache miss is ok. entry = cacheEntry1; if (entry != null && entry.key == key) { return entry.value; } entry = cacheEntry2; if (entry != null && entry.key == key) { // Promote second cache entry to first so the access pattern // [K1, K2, K1, K3, K1, K4...] still hits the cache half the time. addToCache(entry); return entry.value; } return null; }
@SuppressWarnings("unchecked") // Safe because we only cast if key is found in map. @Override public V get(@Nullable Object key) { V value = getIfCached(key); if (value != null) { return value; } value = getWithoutCaching(key); if (value != null) { addToCache((K) key, value); } return value; }
/** * Constructs a graph with the properties specified in {@code builder}, initialized with the given * node map. */ ConfigurableValueGraph( AbstractGraphBuilder<? super N> builder, Map<N, GraphConnections<N, V>> nodeConnections, long edgeCount) { this.isDirected = builder.directed; this.allowsSelfLoops = builder.allowsSelfLoops; this.nodeOrder = builder.nodeOrder.cast(); // Prefer the heavier "MapRetrievalCache" for nodes if lookup is expensive. this.nodeConnections = (nodeConnections instanceof TreeMap) ? new MapRetrievalCache<N, GraphConnections<N, V>>(nodeConnections) : new MapIteratorCache<N, GraphConnections<N, V>>(nodeConnections); this.edgeCount = checkNonNegative(edgeCount); }
@Override protected V getIfCached(@NullableDecl Object key) { V value = super.getIfCached(key); if (value != null) { return value; } // Store a local reference to the cache entry. If the backing map is immutable, this, // in combination with immutable cache entries, will ensure a thread-safe cache. CacheEntry<K, V> entry; // Check cache. We use == on purpose because it's cheaper and a cache miss is ok. entry = cacheEntry1; if (entry != null && entry.key == key) { return entry.value; } entry = cacheEntry2; if (entry != null && entry.key == key) { // Promote second cache entry to first so the access pattern // [K1, K2, K1, K3, K1, K4...] still hits the cache half the time. addToCache(entry); return entry.value; } return null; }
/** * Constructs a graph with the properties specified in {@code builder}, initialized with the given * node and edge maps. */ ConfigurableNetwork( NetworkBuilder<? super N, ? super E> builder, Map<N, NetworkConnections<N, E>> nodeConnections, Map<E, N> edgeToReferenceNode) { this.isDirected = builder.directed; this.allowsParallelEdges = builder.allowsParallelEdges; this.allowsSelfLoops = builder.allowsSelfLoops; this.nodeOrder = builder.nodeOrder.cast(); this.edgeOrder = builder.edgeOrder.cast(); // Prefer the heavier "MapRetrievalCache" for nodes if lookup is expensive. This optimizes // methods that access the same node(s) repeatedly, such as Graphs.removeEdgesConnecting(). this.nodeConnections = (nodeConnections instanceof TreeMap) ? new MapRetrievalCache<N, NetworkConnections<N, E>>(nodeConnections) : new MapIteratorCache<N, NetworkConnections<N, E>>(nodeConnections); this.edgeToReferenceNode = new MapIteratorCache<>(edgeToReferenceNode); }
private void addToCache(K key, V value) { addToCache(new CacheEntry<K, V>(key, value)); }
/** * Constructs a graph with the properties specified in {@code builder}, initialized with the given * node and edge maps. */ ConfigurableNetwork( NetworkBuilder<? super N, ? super E> builder, Map<N, NetworkConnections<N, E>> nodeConnections, Map<E, N> edgeToReferenceNode) { this.isDirected = builder.directed; this.allowsParallelEdges = builder.allowsParallelEdges; this.allowsSelfLoops = builder.allowsSelfLoops; this.nodeOrder = builder.nodeOrder.cast(); this.edgeOrder = builder.edgeOrder.cast(); // Prefer the heavier "MapRetrievalCache" for nodes if lookup is expensive. This optimizes // methods that access the same node(s) repeatedly, such as Graphs.removeEdgesConnecting(). this.nodeConnections = (nodeConnections instanceof TreeMap) ? new MapRetrievalCache<N, NetworkConnections<N, E>>(nodeConnections) : new MapIteratorCache<N, NetworkConnections<N, E>>(nodeConnections); this.edgeToReferenceNode = new MapIteratorCache<>(edgeToReferenceNode); }
private void addToCache(K key, V value) { addToCache(new CacheEntry<K, V>(key, value)); }