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

org.fz.nettyx.util.ChannelStorage Maven / Gradle / Ivy

package org.fz.nettyx.util;

import cn.hutool.core.map.SafeConcurrentHashMap;
import io.netty.channel.Channel;
import lombok.experimental.Delegate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
 * Used to store channels, using key-length pairs
 *
 * @param  the type parameter
 * @author fengbinbin
 * @version 1.0
 * @since 2021 /4/29 10:19
 */
public class ChannelStorage {

    @Delegate
    private final Map storage;

    /**
     * Instantiates a new Channel storage.
     */
    public ChannelStorage() {
        this.storage = new SafeConcurrentHashMap<>(8);
    }

    /**
     * Instantiates a new Channel storage.
     *
     * @param initialCapacity the initial capacity
     */
    public ChannelStorage(int initialCapacity) {
        this.storage = new SafeConcurrentHashMap<>(initialCapacity);
    }

    /**
     * Instantiates a new Channel storage.
     *
     * @param channelMap the channel map
     */
    public ChannelStorage(Map channelMap) {
        this.storage = new SafeConcurrentHashMap<>(channelMap);
    }

    /**
     * Instantiates a new Channel storage.
     *
     * @param initialCapacity the initial capacity
     * @param loadFactor      the load factor
     */
    public ChannelStorage(int initialCapacity, float loadFactor) {
        this(initialCapacity, loadFactor, 1);
    }

    /**
     * Instantiates a new Channel storage.
     *
     * @param initialCapacity  the initial capacity
     * @param loadFactor       the load factor
     * @param concurrencyLevel the concurrency level
     */
    public ChannelStorage(int initialCapacity, float loadFactor, int concurrencyLevel) {
        this.storage = new SafeConcurrentHashMap<>(initialCapacity, loadFactor, concurrencyLevel);
    }

    /**
     * Is all active boolean.
     *
     * @return the boolean
     */
    public boolean isAllActive() {
        return isAll(Channel::isActive);
    }

    /**
     * Is all writable boolean.
     *
     * @return the boolean
     */
    public boolean isAllWritable() {
        return isAll(Channel::isWritable);
    }

    /**
     * Is all registered boolean.
     *
     * @return the boolean
     */
    public boolean isAllRegistered() {
        return isAll(Channel::isRegistered);
    }

    /**
     * Is all open boolean.
     *
     * @return the boolean
     */
    public boolean isAllOpen() {
        return isAll(Channel::isOpen);
    }

    /**
     * Find all active list.
     *
     * @return the list
     */
    public List findAllActive() {
        return findAll(Channel::isActive);
    }

    /**
     * Find all writable list.
     *
     * @return the list
     */
    public List findAllWritable() {
        return findAll(Channel::isWritable);
    }

    /**
     * Find all registered list.
     *
     * @return the list
     */
    public List findAllRegistered() {
        return findAll(Channel::isRegistered);
    }

    /**
     * Find all open list.
     *
     * @return the list
     */
    public List findAllOpen() {
        return findAll(Channel::isOpen);
    }

    /**
     * Find all list.
     *
     * @param channelPredicate the channel predicate
     * @return the list
     */
    public List findAll(Predicate channelPredicate) {
        List channels = new ArrayList<>(10);
        for (Channel channel : storage.values()) {
            if (channelPredicate.test(channel)) channels.add(channel);
        }
        return channels;
    }

    /**
     * Is all boolean.
     *
     * @param channelPredicate the channel predicate
     * @return the boolean
     */
    public boolean isAll(Predicate channelPredicate) {
        for (Channel channel : storage.values()) {
            if (channelPredicate.negate().test(channel)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public String toString() {
        return this.storage.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy