class GridClientFactory {
    /** Map that contain all opened clients. */
    private static ConcurrentMap<UUID, GridClientImpl> openClients = new ConcurrentHashMap<>();

    /** Lock to prevent concurrent adding of clients while stopAll is working. */
    private static final ReadWriteLock busyLock = new ReentrantReadWriteLock();

    /**
     * Ensure singleton.
     */
    private GridClientFactory() {
        // No-op.
    }

    /**
     * Starts a client with given configuration. Starting client will be assigned a randomly generated
     * UUID which can be obtained by {@link GridClient#id()} method.
     *
     * @param cfg Client configuration.
     * @return Started client.
     * @throws GridClientException If client could not be created.
     */
    public static GridClient start(GridClientConfiguration cfg) throws GridClientException {
        return start(cfg, false);
    }

    /**
     * Starts a client before node start with given configuration.
     * If node has already started, there will be an error.
     *
     * @param cfg Client configuration.
     * @return Started client.
     * @throws GridClientException If client could not be created.
     */
    public static GridClientBeforeNodeStart startBeforeNodeStart(
        GridClientConfiguration cfg
    ) throws GridClientException {
        return start(cfg, true);
    }

    /**
     * Starts a client with given configuration.
     *
     * @param cfg Client configuration.
     * @param beforeNodeStart Before node start.
     * @return Started client.
     * @throws GridClientException If client could not be created.
     */
    private static GridClientImpl start(
        GridClientConfiguration cfg,
        boolean beforeNodeStart
    ) throws GridClientException {
        busyLock.readLock().lock();

        try {
            UUID clientId = UUID.randomUUID();

            GridClientImpl client = new GridClientImpl(clientId, cfg, false, beforeNodeStart);

            GridClientImpl old = openClients.putIfAbsent(clientId, client);

            assert old == null : "Random unique UUID generation failed.";

            return client;
        }
        finally {
            busyLock.readLock().unlock();
        }
    }

    /**
     * Waits for all open clients to finish their operations and stops them, This method
     * is equivalent to {@code stopAll(true)} method invocation.
     *
     * @see #stopAll(boolean)
     */
    public static void stopAll() {
        stopAll(true);
    }

    /**
     * Stops all currently open clients.
     *
     * @param wait If {@code true} then each client will wait to finish all ongoing requests before
     *      closing (however, no new requests will be accepted). If {@code false}, clients will be
     *      closed immediately and all ongoing requests will be failed.
     */
    @SuppressWarnings("TooBroadScope")
    public static void stopAll(boolean wait) {<FILL_FUNCTION_BODY>}

    /**
     * Waits for all pending requests for a particular client to be completed (no new requests will be
     * accepted) and then closes the client. This method is equivalent to {@code stop(clientId, true)}.
     *
     * @param clientId Identifier of client to close.
     * @see #stop(UUID, boolean)
     */
    public static void stop(UUID clientId) {
        stop(clientId, true);
    }

    /**
     * Stops particular client.
     *
     * @param clientId Client identifier to close.
     * @param wait If {@code true} then client will wait to finish all ongoing requests before
     *      closing (however, no new requests will be accepted). If {@code false}, client will be
     *      closed immediately and all ongoing requests will be failed.
     */
    public static void stop(UUID clientId, boolean wait) {
        busyLock.readLock().lock();

        try {
            GridClientImpl client = openClients.remove(clientId);

            if (client != null)
                client.stop(wait);
        }
        finally {
            busyLock.readLock().unlock();
        }
    }
}

        ConcurrentMap<UUID, GridClientImpl> old;

        busyLock.writeLock().lock();

        try {
            old = openClients;

            openClients = new ConcurrentHashMap<>();
        }
        finally {
            busyLock.writeLock().unlock();
        }

        for (GridClientImpl client : old.values())
            client.stop(wait);
    
<no_super_class>