Java Code Examples for it.unimi.dsi.fastutil.objects.Object2ObjectMap#isEmpty()

The following examples show how to use it.unimi.dsi.fastutil.objects.Object2ObjectMap#isEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the {@link Map} of all request-scoped {@link MDC} properties bound to the specified
 * {@link RequestContext}.
 *
 * @param ctx the {@link RequestContext}
 *
 * @return the {@link Map} that contains all request-scoped {@link MDC} properties.
 *         An empty {@link Map} if there are no request-scoped {@link MDC} properties.
 */
public static Map<String, String> getAll(RequestContext ctx) {
    requireNonNull(ctx, "ctx");

    final Object2ObjectMap<String, String> map = getMap(ctx);
    final RequestContext rootCtx = ctx.root();
    if (rootCtx == null || rootCtx == ctx) {
        return map;
    }

    final Object2ObjectMap<String, String> rootMap = getMap(rootCtx);
    if (rootMap.isEmpty()) {
        return map;
    }

    if (map.isEmpty()) {
        return rootMap;
    }

    final Object2ObjectMap<String, String> merged =
            new Object2ObjectOpenHashMap<>(rootMap.size() + map.size());
    merged.putAll(rootMap);
    merged.putAll(map);
    return merged;
}
 
Example 2
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Binds the specified request-scoped {@link MDC} property to the specified {@link RequestContext}.
 *
 * @param ctx   the {@link RequestContext}
 * @param key   the key of the request-scoped {@link MDC} property
 * @param value the value of the request-scoped {@link MDC} property
 */
public static void put(RequestContext ctx, String key, @Nullable String value) {
    requireNonNull(ctx, "ctx");
    requireNonNull(key, "key");

    synchronized (ctx) {
        final Object2ObjectMap<String, String> oldMap = getMap(ctx);
        final Object2ObjectMap<String, String> newMap;
        if (oldMap.isEmpty()) {
            newMap = Object2ObjectMaps.singleton(key, value);
        } else {
            final Object2ObjectMap<String, String> tmp =
                    new Object2ObjectOpenHashMap<>(oldMap.size() + 1);
            tmp.putAll(oldMap);
            tmp.put(key, value);
            newMap = Object2ObjectMaps.unmodifiable(tmp);
        }
        ctx.setAttr(MAP, newMap);
    }
}
 
Example 3
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Binds the specified request-scoped {@link MDC} properties to the specified {@link RequestContext}.
 *
 * @param ctx the {@link RequestContext}
 * @param map the {@link Map} that contains the request-scoped {@link MDC} properties
 */
public static void putAll(RequestContext ctx, Map<String, String> map) {
    requireNonNull(ctx, "ctx");
    requireNonNull(map, "map");
    if (map.isEmpty()) {
        return;
    }

    synchronized (ctx) {
        final Object2ObjectMap<String, String> oldMap = getMap(ctx);
        final Object2ObjectMap<String, String> newMap;
        if (oldMap.isEmpty()) {
            newMap = new Object2ObjectOpenHashMap<>(map);
        } else {
            newMap = new Object2ObjectOpenHashMap<>(oldMap.size() + map.size());
            newMap.putAll(oldMap);
            newMap.putAll(map);
        }
        ctx.setAttr(MAP, Object2ObjectMaps.unmodifiable(newMap));
    }
}
 
Example 4
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Unbinds all request-scoped {@link MDC} properties from the specified {@link RequestContext}.
 *
 * @param ctx the {@link RequestContext}
 */
public static void clear(RequestContext ctx) {
    requireNonNull(ctx, "ctx");

    synchronized (ctx) {
        final Object2ObjectMap<String, String> oldMap = getMap(ctx);
        if (!oldMap.isEmpty()) {
            ctx.setAttr(MAP, Object2ObjectMaps.emptyMap());
        }
    }
}