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

se.laz.casual.jca.pool.ConnectionContainer Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 * Copyright (c) 2022, The casual project. All rights reserved.
 *
 * This software is licensed under the MIT license, https://opensource.org/licenses/MIT
 */
package se.laz.casual.jca.pool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class ConnectionContainer
{
    private final List connections = new ArrayList<>();
    private final Object lock = new Object();


    public static ConnectionContainer of()
    {
        return new ConnectionContainer();
    }

    public int size()
    {
        synchronized (lock)
        {
            return connections.size();
        }
    }

    public void addConnection(ReferenceCountedNetworkConnection connection)
    {
        synchronized (lock)
        {
            connections.add(connection);
        }
    }

    public void removeConnection(ReferenceCountedNetworkConnection connection)
    {
        synchronized (lock)
        {
            connections.remove(connection);
        }
    }

    public ReferenceCountedNetworkConnection get()
    {
        synchronized (lock)
        {
            if(connections.size() == 1)
            {
                return connections.get(0);
            }
            return connections.get(getRandomNumber(0, connections.size()));
        }
    }

    @Override
    public String toString()
    {
        return "ConnectionContainer{" +
                "connections=" + connections +
                ", lock=" + lock +
                '}';
    }

    // pseudorandom is good enough here
    @SuppressWarnings("java:S2245")
    // max - exclusive upper limit
    private static int getRandomNumber(int min, int max)
    {
        return ThreadLocalRandom.current().nextInt(min, max);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy