Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dfunction InstantiationException(stackMessage) {
- throw new Error("InstantiationException: "+ stackMessage);
- }
- var _extends = (this && this._extends) || function (d, b) {
- for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
- function _() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (_.prototype = b.prototype, new _());
- };
- function Cloneable() {
- this.clone = function() {
- return this;
- }
- }
- /**
- *
- * @param {function} K
- * @param {function} V
- * @typedef {{map: object}}
- */
- function HashMap(K,V, preMap = undefined) {
- if (typeof K != 'function' || typeof V != 'function')
- throw new InstantiationException("K and V need to be typeof Function");
- var map = preMap || new Object();
- /**
- * @return {void}
- */
- this.put = function(key,value) {
- map[`${key}`] = value;
- }
- /**
- * @param {object} key
- * @return {any} value
- */
- this.get = function(key) {
- return map[key];
- }
- /**
- * @return {Array} values of the map
- */
- this.values = function() {
- return Object.values(map);
- }
- /**
- * @return keys
- */
- this.keySet = function() {
- return Object.keys(map);
- }
- this.forEach = function(callback) {
- console.log(map);
- var iteratorable = [];
- for (var [key,value] in (this.values(),this.keySet())) {
- iteratorable.push([key,value])
- }
- for (var [key,val] of iteratorable) {
- console.log(key);
- console.log(val);
- callback.call(key,val);
- }
- }
- /**
- * Removes the key and the value.
- * @param {any} key
- *
- */
- this.remove = function(key) {
- delete map[key];
- return true;
- }
- /**
- * If the map value is empty, return true
- *
- * (think of it as an entry set being empty)
- * @return {boolean}
- */
- this.isEmpty = function() {
- return !(this.keys().length > 0 && this.values().length > 0);
- }
- /**
- * @param {any} key The key
- * @param {any} value the value to replace the current value inside the key.
- * @typedef {{key key, Object value}}
- * @return {Object} null if the key is not set, return the original value if it is set.
- */
- this.replace = function(key, value) {
- if (map[key]) {
- var old = map[key];
- map[key] = value;
- return old;
- }
- return null;
- }
- this.clone = function() {
- return new HashMap(K,V,map) /* ;p */;
- }
- // return map;
- }
- var map = new HashMap(String, Number);
- map.put("test", "banana");
- map.get("test");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement