コード例 #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;
 }
コード例 #2
0
  @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
ファイル: Exercise_07.java プロジェクト: BartCr/Java8Workshop
 @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");
 }
コード例 #4
0
  /**
   * 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();
      }
    }
コード例 #6
0
 private V compute(K key) {
   return map.computeIfAbsent(key, computingFunctionAdapter);
 }
コード例 #7
0
ファイル: MixinUtils.java プロジェクト: kawasima/enkan
 static MethodHandle getMethodHandle(Method method) {
   return methodHandleCache.computeIfAbsent(method, MixinUtils::lookupSpecial);
 }