it.unimi.dsi.fastutil.objects.Object2ObjectMaps Java Examples

The following examples show how to use it.unimi.dsi.fastutil.objects.Object2ObjectMaps. 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
/**
 * 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 #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} 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 #3
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Unbinds the specified request-scoped {@link MDC} property from the specified {@link RequestContext}.
 *
 * @param ctx the {@link RequestContext}
 * @param key the key of the request-scoped {@link MDC} property to unbind
 */
public static void remove(RequestContext ctx, String key) {
    requireNonNull(ctx, "ctx");
    requireNonNull(key, "key");

    synchronized (ctx) {
        final Object2ObjectMap<String, String> oldMap = getMap(ctx);
        if (!oldMap.containsKey(key)) {
            return;
        }

        final Object2ObjectMap<String, String> newMap;
        if (oldMap.size() == 1) {
            newMap = Object2ObjectMaps.emptyMap();
        } else {
            final Object2ObjectOpenHashMap<String, String> tmp = new Object2ObjectOpenHashMap<>(oldMap);
            tmp.remove(key);
            newMap = Object2ObjectMaps.unmodifiable(tmp);
        }
        ctx.setAttr(MAP, 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());
        }
    }
}
 
Example #5
Source File: SyntaxAttributeTyping.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
public SyntaxAttributeTyping build() {
	return new SyntaxAttributeTyping(
			Object2ObjectMaps.unmodifiable(constraints));
}
 
Example #6
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static Object2ObjectMap<String, String> getMap(RequestContext ctx) {
    final Object2ObjectMap<String, String> map = ctx.ownAttr(MAP);
    return firstNonNull(map, Object2ObjectMaps.emptyMap());
}
 
Example #7
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, String> getCopyOfContextMap() {
    final Map<String, String> threadLocalMap = getDelegateContextMap();
    final RequestContext ctx = RequestContext.currentOrNull();
    if (ctx == null) {
        // No context available
        if (threadLocalMap != null) {
            return maybeCloneThreadLocalMap(threadLocalMap);
        } else {
            return Object2ObjectMaps.emptyMap();
        }
    }

    // Retrieve the request-scoped properties.
    // Note that this map is 1) unmodifiable and shared 2) or modifiable yet unshared,
    // which means it's OK to return as it is or mutate it.
    final Map<String, String> requestScopedMap = getAll(ctx);
    if (threadLocalMap == null || threadLocalMap.isEmpty()) {
        // No thread-local map available
        return requestScopedMap;
    }

    // Thread-local map available
    if (requestScopedMap.isEmpty()) {
        // Only thread-local map available
        return maybeCloneThreadLocalMap(threadLocalMap);
    }

    // Both thread-local and request-scoped map available
    final Object2ObjectOpenHashMap<String, String> merged;
    if (requestScopedMap instanceof Object2ObjectOpenHashMap) {
        // Reuse the mutable copy returned by getAll() for less memory footprint.
        merged = (Object2ObjectOpenHashMap<String, String>) requestScopedMap;
        threadLocalMap.forEach(merged::putIfAbsent);
    } else {
        merged = new Object2ObjectOpenHashMap<>(threadLocalMap.size() + requestScopedMap.size());
        merged.putAll(threadLocalMap);
        merged.putAll(requestScopedMap);
    }
    return merged;
}
 
Example #8
Source File: MockFetchedResponses.java    From BUbiNG with Apache License 2.0 votes vote down vote up
public Map<String, String> headers() { return Object2ObjectMaps.singleton("content-type", "text/html"); }