Пример #1
0
 /**
  * Returns the hunspell dictionary for the given locale.
  *
  * @param locale The name of the locale
  */
 public Dictionary getDictionary(String locale) {
   Dictionary dictionary = knownDictionaries.get(locale);
   if (dictionary == null) {
     dictionary = dictionaries.computeIfAbsent(locale, loadingFunction);
   }
   return dictionary;
 }
  @RuntimeType
  public static Object intercept(
      @Origin Method method, @AllArguments Object[] arguments, @SuperCall Callable superCall) {

    val declaringClass = method.getDeclaringClass();

    val parameters = method.getParameters();

    val keyArguments =
        IntStream.range(0, parameters.length)
            .boxed()
            .map(i -> Argument.builder().type(parameters[i].getType()).value(arguments[i]).build())
            .collect(Collectors.toList());

    val cacheKey =
        CacheKey.builder().bucketName(declaringClass.getName()).arguments(keyArguments).build();

    return cache.computeIfAbsent(
        cacheKey,
        k -> {
          try {
            return superCall.call();
          } catch (Exception e) {
            return null;
          }
        });
  }
Пример #3
0
 @Test
 public void solution() throws Exception {
   ConcurrentHashMap<String, Long> map = new ConcurrentHashMap<>();
   TextReader.getWordsFromFile("/alice.txt")
       .stream()
       .forEach(w -> map.computeIfAbsent(w, k -> (long) k.length()));
   Map.Entry<String, Long> entry =
       map.reduceEntries(
           Runtime.getRuntime().availableProcessors(),
           (v1, v2) -> v1.getValue() > v2.getValue() ? v1 : v2);
   assertThat(entry.getKey()).isEqualTo("unenforceability");
 }
  /**
   * Examines the {@link RequestMessage} and extracts the session token. The session is then either
   * found or a new one is created.
   */
  protected static Session getSession(final Context context, final RequestMessage msg) {
    final String sessionId = (String) msg.getArgs().get(Tokens.ARGS_SESSION);

    logger.debug(
        "In-session request {} for eval for session {} in thread {}",
        msg.getRequestId(),
        sessionId,
        Thread.currentThread().getName());

    final Session session =
        sessions.computeIfAbsent(sessionId, k -> new Session(k, context, sessions));
    session.touch();
    return session;
  }
Пример #5
0
    public void addAll(
        NearlineStorage storage, Iterable<F> files, CompletionHandler<Void, K> callback) {
      List<R> newRequests = new ArrayList<>();
      for (F file : files) {
        K key = extractKey(file);
        R request =
            requests.computeIfAbsent(
                key,
                (k) -> {
                  if (!state.increment()) {
                    callback.failed(new CacheException("Nearline storage has been shut down."), k);
                    return null;
                  }
                  try {
                    R newRequest = createRequest(storage, file);
                    newRequests.add(newRequest);
                    return newRequest;
                  } catch (Exception e) {
                    state.decrement();
                    callback.failed(e, k);
                    return null;
                  } catch (Error e) {
                    state.decrement();
                    callback.failed(e, k);
                    throw e;
                  }
                });
        if (request != null) {
          request.addCallback(callback);
        }
      }
      submit(storage, newRequests);

      /* If the container shut down before the requests were added to the map,
       * the shutdown call might have missed them when cancelling requests.
       */
      if (state.isShutdown()) {
        cancelRequests();
      }
    }
 private V compute(K key) {
   return map.computeIfAbsent(key, computingFunctionAdapter);
 }
Пример #7
0
 static MethodHandle getMethodHandle(Method method) {
   return methodHandleCache.computeIfAbsent(method, MixinUtils::lookupSpecial);
 }