/** * Load and instantiate the {@link CandidateComponentsIndex} from * {@value #COMPONENTS_RESOURCE_LOCATION}, using the given class loader. If no * index is available, return {@code null}. * @param classLoader the ClassLoader to use for loading (can be {@code null} to use the default) * @return the index to use or {@code null} if no index was found * @throws IllegalArgumentException if any module index cannot * be loaded or if an error occurs while creating {@link CandidateComponentsIndex} */ @Nullable public static CandidateComponentsIndex loadIndex(@Nullable ClassLoader classLoader) { ClassLoader classLoaderToUse = classLoader; if (classLoaderToUse == null) { classLoaderToUse = CandidateComponentsIndexLoader.class.getClassLoader(); } return cache.computeIfAbsent(classLoaderToUse, CandidateComponentsIndexLoader::doLoadIndex); }
int calculateCapacity(CharSequence sequence, Charset charset) { float maxBytesPerChar = this.charsetToMaxBytesPerChar .computeIfAbsent(charset, cs -> cs.newEncoder().maxBytesPerChar()); float maxBytesForSequence = sequence.length() * maxBytesPerChar; return (int) Math.ceil(maxBytesForSequence); }
private List<byte[]> getDelimiterBytes(@Nullable MimeType mimeType) { return this.delimitersCache.computeIfAbsent(getCharset(mimeType), charset -> this.delimiters.stream() .map(s -> s.getBytes(charset)) .collect(Collectors.toList())); }
/** * This implementation always returns a {@link Cache} implementation that will not store items. * Additionally, the request cache will be remembered by the manager for consistency. */ @Override @Nullable public Cache getCache(String name) { Cache cache = this.caches.get(name); if (cache == null) { this.caches.computeIfAbsent(name, key -> new NoOpCache(name)); synchronized (this.cacheNames) { this.cacheNames.add(name); } } return this.caches.get(name); }
/** * Get logger provider * * @param key the returned logger will be named after clazz * @return logger */ public static Logger getLogger(Class<?> key) { return LOGGERS.computeIfAbsent(key.getName(), name -> new FailsafeLogger(LOGGER_ADAPTER.getLogger(name))); }
/** * Get logger provider * * @param key the returned logger will be named after clazz * @return logger */ public static Logger getLogger(Class<?> key) { return LOGGERS.computeIfAbsent(key.getName(), name -> new FailsafeLogger(LOGGER_ADAPTER.getLogger(name))); }
/** * Get logger provider * * @param key the returned logger will be named after key * @return logger provider */ public static Logger getLogger(String key) { return LOGGERS.computeIfAbsent(key, k -> new FailsafeLogger(LOGGER_ADAPTER.getLogger(k))); }
@Override public void subscribe(URL url, NotifyListener listener) { if (url == null) { throw new IllegalArgumentException("subscribe url == null"); } if (listener == null) { throw new IllegalArgumentException("subscribe listener == null"); } if (logger.isInfoEnabled()) { logger.info("Subscribe: " + url); } Set<NotifyListener> listeners = subscribed.computeIfAbsent(url, n -> new ConcurrentHashSet<>()); listeners.add(listener); }
@Override public void subscribe(URL url, NotifyListener listener) { if (url == null) { throw new IllegalArgumentException("subscribe url == null"); } if (listener == null) { throw new IllegalArgumentException("subscribe listener == null"); } if (logger.isInfoEnabled()) { logger.info("Subscribe: " + url); } Set<NotifyListener> listeners = subscribed.computeIfAbsent(url, n -> new ConcurrentHashSet<>()); listeners.add(listener); }
/** * Get logger provider * * @param key the returned logger will be named after key * @return logger provider */ public static Logger getLogger(String key) { return LOGGERS.computeIfAbsent(key, k -> new FailsafeLogger(LOGGER_ADAPTER.getLogger(k))); }
@SuppressWarnings("unchecked") @Override @Nullable public <T> T get(Object key, Callable<T> valueLoader) { return (T) fromStoreValue(this.store.computeIfAbsent(key, r -> { try { return toStoreValue(valueLoader.call()); } catch (Throwable ex) { throw new ValueRetrievalException(key, valueLoader, ex); } })); }
/** * Since all governance rules will lay under dubbo group, this method now always uses the default dubboConfig and * ignores the group parameter. */ @Override public void addListener(String key, String group, ConfigurationListener listener) { ApolloListener apolloListener = listeners.computeIfAbsent(group + key, k -> createTargetListener(key, group)); apolloListener.addListener(listener); dubboConfig.addChangeListener(apolloListener, Collections.singleton(key)); }
/** * Since all governance rules will lay under dubbo group, this method now always uses the default dubboConfig and * ignores the group parameter. */ @Override public void addListener(String key, String group, ConfigurationListener listener) { ApolloListener apolloListener = listeners.computeIfAbsent(group + key, k -> createTargetListener(key, group)); apolloListener.addListener(listener); dubboConfig.addChangeListener(apolloListener, Collections.singleton(key)); }
@Override public Router getRouter(URL url) { routerMap.computeIfAbsent(url.getServiceKey(), k -> createRouter(url)); return routerMap.get(url.getServiceKey()); }
@Override public Router getRouter(URL url) { routerMap.computeIfAbsent(url.getServiceKey(), k -> createRouter(url)); return routerMap.get(url.getServiceKey()); }
public void testComputeIfAbsentEviction() { // b/80241237 Cache<String, String> c = CacheBuilder.newBuilder().maximumSize(1).build(); assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo(""); assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo(""); assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo(""); assertThat(c.size()).isEqualTo(1); assertThat(c.asMap().computeIfAbsent("hash-2", k -> "")).isEqualTo(""); }
public void testComputeIfAbsent_RemovalListener() { List<RemovalNotification<Object, Object>> notifications = new ArrayList<>(); RemovalListener<Object, Object> removalListener = new RemovalListener<Object, Object>() { @Override public void onRemoval(RemovalNotification<Object, Object> notification) { notifications.add(notification); } }; Cache<Object, Object> cache = CacheBuilder.newBuilder().removalListener(removalListener).build(); cache.put("a", "b"); cache.asMap().computeIfAbsent("a", k -> "c"); assertTrue(notifications.toString(), notifications.isEmpty()); }
public void testComputeEviction() { // b/80241237 Cache<String, String> c = CacheBuilder.newBuilder().maximumSize(1).build(); assertThat(c.asMap().compute("hash-1", (k, v) -> "a")).isEqualTo("a"); assertThat(c.asMap().compute("hash-1", (k, v) -> "b")).isEqualTo("b"); assertThat(c.asMap().compute("hash-1", (k, v) -> "c")).isEqualTo("c"); assertThat(c.size()).isEqualTo(1); assertThat(c.asMap().computeIfAbsent("hash-2", k -> "")).isEqualTo(""); }
@Override @SuppressWarnings("unchecked") public <K, V> void getAsyncMap(String name, Handler<AsyncResult<AsyncMap<K, V>>> resultHandler) { LocalAsyncMapImpl<K, V> asyncMap = asyncMaps.computeIfAbsent(name, n -> new LocalAsyncMapImpl(vertx)); vertx.runOnContext(v -> resultHandler.handle(Future.succeededFuture(asyncMap))); }
public void testComputeIfAbsent() { // simultaneous insertion for same key, expect 1 winner doParallelCacheOp( count, n -> { cache.asMap().computeIfAbsent(key, k -> "value" + n); }); assertEquals(1, cache.size()); }