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

com.github.lontime.exthttp.common.ConfigKeys Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.github.lontime.exthttp.common;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import io.netty.channel.ChannelOption;
import io.netty.channel.epoll.EpollChannelOption;
import reactor.util.function.Tuple2;

/**
 * ConfigKeyEnum.
 * @author lontime
 * @since 1.0
 */
public enum ConfigKeys {
    /**
     * CONNECT_TIMEOUT_MILLIS.
     */
    CONNECT_TIMEOUT_MILLIS(ConfigKey.of(ChannelOption.CONNECT_TIMEOUT_MILLIS.name(), Type.INT)),
    /**
     * SO_KEEPALIVE.
     */
    SO_KEEPALIVE(ConfigKey.of(ChannelOption.SO_KEEPALIVE.name(), Type.BOOLEAN)),
    /**
     * TCP_KEEPIDLE.
     */
    TCP_KEEPIDLE(ConfigKey.of(EpollChannelOption.TCP_KEEPIDLE.name(), Type.INT)),
    /**
     * TCP_KEEPINTVL.
     */
    TCP_KEEPINTVL(ConfigKey.of(EpollChannelOption.TCP_KEEPINTVL.name(), Type.INT)),
    /**
     * TCP_KEEPCNT.
     */
    TCP_KEEPCNT(ConfigKey.of(EpollChannelOption.TCP_KEEPCNT.name(), Type.INT));

    private final ConfigKey key;

    ConfigKeys(ConfigKey key) {
        this.key = key;
    }

    public ConfigKey getKey() {
        return key;
    }

    public static Optional resolve(String name) {
        return Arrays.stream(ConfigKeys.values()).filter(s -> s.name().equalsIgnoreCase(name)).map(s -> s.getKey()).findFirst();
    }

    public static Map resolve(Map map) {
        return resolve(map, true);
    }

    public static Map resolve(Map map, boolean strict) {
        final Map newMap = new HashMap<>(map.size());
        for (Map.Entry entry : map.entrySet()) {
            final String actualKey = entry.getKey().replace('-', '_');
            final Optional key = ConfigKeys.resolve(actualKey);
            if (key.isPresent()) {
                newMap.put(key.get(), entry.getValue());
            } else if (!strict) {
                final Tuple2 maybeType = Type.maybe(entry.getValue());
                newMap.put(ConfigKey.of(actualKey, maybeType.getT1()), maybeType.getT2());
            }
        }
        return newMap;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy