airbug/bugcore

View on GitHub
libraries/bugcore/js/src/concurrent/LockMap.js

Summary

Maintainability
A
3 hrs
Test Coverage
/*
 * Copyright (c) 2016 airbug Inc. http://airbug.com
 *
 * bugcore may be freely distributed under the MIT license.
 */


//-------------------------------------------------------------------------------
// Annotations
//-------------------------------------------------------------------------------

//@Export('LockMap')

//@Require('ArgumentBug')
//@Require('Class')
//@Require('Lock')
//@Require('Map')
//@Require('Obj')


//-------------------------------------------------------------------------------
// Context
//-------------------------------------------------------------------------------

require('bugpack').context("*", function(bugpack) {

    //-------------------------------------------------------------------------------
    // BugPack
    //-------------------------------------------------------------------------------

    var ArgumentBug     = bugpack.require('ArgumentBug');
    var Class           = bugpack.require('Class');
    var Lock            = bugpack.require('Lock');
    var Map             = bugpack.require('Map');
    var Obj             = bugpack.require('Obj');


    //-------------------------------------------------------------------------------
    // Declare Class
    //-------------------------------------------------------------------------------

    /**
     * @class
     * @extends {Map}
     */
    var LockMap = Class.extend(Map, /** @lends {LockMap.prototype} */ {

        _name: "LockMap",


        //-------------------------------------------------------------------------------
        // Obj Methods
        //-------------------------------------------------------------------------------

        /**
         * @param {boolean=} deep
         * @return {LockMap}
         */
        clone: function(deep) {
            var cloneMap = new LockMap();
            if (deep) {
                this.forEach(function(value, key) {
                    cloneMap.put(Obj.clone(key, deep), Obj.clone(value, deep));
                });
            } else {
                cloneMap.putAll(this);
            }
            return cloneMap;
        },


        //-------------------------------------------------------------------------------
        // Map Methods
        //-------------------------------------------------------------------------------

        /**
         * @param {*} key
         * @return {V} Returns undefined if no value is found.
         */
        get: function(key) {
            var lock = this.getHashTable().get(key);
            if (!lock) {
                lock = new Lock();
                this.put(key, lock);
            }
            return lock;
        },

        /**
         * @param {K} key
         * @param {V} value
         * @return {V}
         */
        put: function(key, value) {
            if (!Class.doesExtend(value, Lock)) {
                throw new ArgumentBug(ArgumentBug.ILLEGAL, "value", value, "parameter must extend Lock");
            }
            this._super(key, value);
        }
    });


    //-------------------------------------------------------------------------------
    // Exports
    //-------------------------------------------------------------------------------

    bugpack.export('LockMap', LockMap);
});