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

com.azure.cosmos.implementation.MacPool Maven / Gradle / Ivy

Go to download

This Package contains Microsoft Azure Cosmos SDK (with Reactive Extension Reactor support) for Azure Cosmos DB SQL API

There is a newer version: 4.63.3
Show newest version
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.cosmos.implementation;

import javax.crypto.Mac;
import java.util.concurrent.ConcurrentLinkedQueue;

class MacPool {
    final Mac macInstance;
    final ConcurrentLinkedQueue pool;

    public MacPool(Mac rootMac) {
        if (rootMac == null) {
            throw new IllegalArgumentException("rootMac");
        }

        this.macInstance = rootMac;
        this.pool = new ConcurrentLinkedQueue<>();
    }

    public ReUsableMac take() {
        try {
            Mac cachedInstance = pool.poll();
            if (cachedInstance == null) {
                cachedInstance = (Mac) this.macInstance.clone();
            }

            return new ReUsableMac(cachedInstance, this);
        } catch (CloneNotSupportedException e) {
            throw new IllegalStateException(e);
        }
    }

    public void give(ReUsableMac closableMac) {
        this.pool.add(closableMac.macInstance);
    }

    /*
     * Closable contract forces to add the Throws Exception contract unnecessarily
     */
    static class ReUsableMac {
        final Mac macInstance;
        final MacPool pool;

        public ReUsableMac(Mac macInstance, MacPool pool) {
            this.macInstance = macInstance;
            this.pool = pool;
        }

        public Mac get() {
            return this.macInstance;
        }

        public void close() {
            pool.give(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy