All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.techshroom.lettar.collections.HttpMultimap Maven / Gradle / Ivy

There is a newer version: 0.5.1
Show newest version
/*
 * This file is part of lettar, licensed under the MIT License (MIT).
 *
 * Copyright (c) TechShroom Studios 
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.techshroom.lettar.collections;

import java.util.Map;
import java.util.Optional;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.techshroom.lettar.HttpUtil;

@AutoValue
public abstract class HttpMultimap {

    public static HttpMultimap of() {
        // if it's empty, it's always sorted :)
        return copyOfPreSorted(ImmutableListMultimap.of());
    }

    public static HttpMultimap copyOf(Map map) {
        return copyOfPreSorted(HttpUtil.headerMapBuilder().putAll(map.entrySet()).build());
    }

    public static HttpMultimap copyOf(Multimap map) {
        if (map instanceof ImmutableListMultimap) {
            // assume sorted properly
            return copyOfPreSorted((ImmutableListMultimap) map);
        }
        return copyOfPreSorted(HttpUtil.headerMapBuilder().putAll(map).build());
    }

    public static HttpMultimap copyOfPreSorted(ImmutableListMultimap map) {
        return new AutoValue_HttpMultimap(map);
    }

    HttpMultimap() {
    }

    public abstract ImmutableListMultimap getMultimap();

    public Optional getSingleValue(String key) {
        String value = getSingleValueOrDefault(key, null);
        return Optional.ofNullable(value);
    }

    public String getSingleValueOrDefault(String key, String defaultValue) {
        return Iterables.getFirst(getMultimap().get(key), defaultValue);
    }

    public HttpMultimap add(String key, String value) {
        return copyOfPreSorted(HttpUtil.headerMapBuilder()
                .putAll(getMultimap())
                .put(key, value)
                .build());
    }

    public HttpMultimap set(String key, String value) {
        return copyOfPreSorted(HttpUtil.headerMapBuilder()
                .putAll(Multimaps.filterKeys(getMultimap(), k -> !k.equalsIgnoreCase(key)))
                .put(key, value)
                .build());
    }

    public HttpMultimap setIfAbsent(String key, String value) {
        if (getMultimap().containsKey(key)) {
            return this;
        }
        // add is more efficient in this case
        return add(key, value);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy