jsbeans

jsbeans  1.0.0

jsbeans > jsbeans > HashMap.js (source view)
Search:
 
Filters
/*!
 * Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
 *
 * This Software is licenced under the LGPL Licence (GNU Lesser General 
 * Public License).
 * In addition to the LGPL Licence the Software is subject to the 
 * following conditions:
 * 
 * 	i	every modification must be public and comunicated to the Author
 * 	ii	every "jsbean" added to this library must be self consistent 
 * 		except for the dependence from jsbeans-x.x.x.js
 * 	iii	copyright notice and this permission notice shall be included 
 * 		in all copies or substantial portions of the Software
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * Javascript implementation of <code>java.util.HashMap</code>.<br/>
 * An object that maps keys to values.  A map cannot contain duplicate keys;
 * each key can map to at most one value.
 * @namespace jsbeans
 * @class HashMap
 * @constructor
 * */
jsbeans.HashMap = function() {
	var _instance = new Array();
	var _size = 0;
	/**
	 * Returns <code>true</code> if <code class="this">this</code> map contains a mapping for the specified {@param} <code class="param">key</code>.
	 * @method containsKey
	 * @param key {String}
	 * @return {boolean}
	 * */
	this.containsKey = function(key) {
		return (typeof (_instance[key]) != "undefined");
	};
	/**
	 * Returns the value to which the specified {@param} <code class="param">key</code> is mapped, <code>null</code> if <code class="this">this</code> map contains no mapping for the {@param} <code class="param">key</code>.
	 * @method get
	 * @param key {String}
	 * @return {Object}
	 * */
	this.get = function(key) { 
		return typeof (_instance[key]) == "undefined" ? null : _instance[key]; 
	};
	/**
	 * Associates the specified {@param} <code class="param">value</code> with the specified {@param} <code class="param">key</code> in <code class="this">this</code> map
	 * (optional operation). If the map previously contained a mapping for
	 * the {@param} <code class="param">key</code>, the old value is replaced by the specified {@param} <code class="param">value</code>.
	 * @method put
	 * @param key {String}
	 * @param value {Object}
	 * @return {Object} the previous value associated with <code class="param">key</code>, or <code>null</code> if there was no mapping for <code class="param">key</code>.
	 * */
	this.put = function(key, val) {
		var res = null;
		if (typeof (_instance[key]) == "undefined") {
			_size++;
		} 
		else {
			res = _instance[key];
		}
		_instance[key] = val;
		return res;
	};
	/**
	 * Returns the number of key-value mappings in <code class="this">this</code> map.
	 * @method size
	 * @return {Integer}
	 * */
	this.size = function() { 
		return _size; 
	};
	/**
	 * Returns an Array view of the values contained in <code class="this">this</code> map.
	 * @method values
	 * @return {Array&lt;Object>}
	 * */
	this.values = function() {
		var res = new Array();
		for (var k in _instance) {
			res.push(_instance[k]);
		}
		return res;
	};
	/**
	 * Returns an Array view of the keys contained in <code class="this">this</code> map.
	 * @method keySet
	 * @return {Array&lt;String>}
	 * */
	this.keySet = function() {
		var res = new Array();
		for (var k in _instance) {
			res.push(k);
		}
		return res;
	};
	/**
	 * Removes the mapping for the specified key from <code class="this">this</code> map if present.
	 * @method remove
	 * @param key {String} key whose mapping is to be removed from the map
	 * @return {Object} the previous value associated with <code class="param">key</code>, or
	 *         <code>null</code> if there was no mapping for <code class="param">key</code>.
	 *         (A <code>null</code> return can also indicate that the map
	 *         previously associated <code>null</code> with <code class="param">key</code>.)
	 * */
	this.remove = function(key) {
		var res = null;
		var inst = new Array();
		var sz = 0;
		for (var k in _instance) {
			if (k == key) {
				res = _instance[k];
			}
			else {
				inst[k] = _instance[k];
				sz++;
			}
		}
		_instance = inst;
		_size = sz;
		return res;
	};
	/**
	 * Orders the key-value mapping by value.<br/>
	 * This method changes the actual order of elements in instance.
	 * @method order
	 * @param [caseSensitive] {boolean} default true
	 * */
	this.order = function() {
		var caseSentitive = arguments[0] || true;
		var values = this.values;
		var res = new Array();
		for (var k in _instance) {
			if (caseSentitive) {
				res.push(_instance[k]);
			} 
			else {
				res.push(_instance[k].toLowerCase());
			}
		}
		res.sort();
		var resInstance = new Array();
		for (var i = 0, r; r = res[i]; i++) {
			for (var o in _instance) {
				if (caseSentitive) {
					if (_instance[o] == r) {
						resInstance[o] = _instance[o];
						break;
					}
				} 
				else {
					if (_instance[o].toLowerCase() == r) {
						resInstance[o] = _instance[o];
						break;
					}
				}
			}
		}
		_instance = resInstance;
	};
	/**
	 * See {@method} <code class="methd">order</code>.
	 * @method orderByValue
	 * @param [caseSensitive] {boolean} default true
	 * */
	this.orderByValue = function() {
		this.order();
	};
	/**
	 * Orders the key-value mapping by key.<br/>
	 * This method changes the actual order of elements in instance.
	 * @method orderByKey
	 * @param [caseSensitive] {boolean} default true
	 * */
	this.orderByKey = function() {
		var caseSentitive = arguments[0] || true;
		var values = this.values;
		var res = new Array();
		for (var k in _instance) {
			if (caseSentitive) {
				res.push(k);
			} 
			else {
				res.push(k.toLowerCase());
			}
		}
		res.sort();
		var resInstance = new Array();
		for (var i = 0, r; r = res[i]; i++) {
			for (var o in _instance) {
				if (caseSentitive) {
					if (o == r) {
						resInstance[o] = _instance[o];
						break;
					}
				} 
				else {
					if (o.toLowerCase() == r) {
						resInstance[o] = _instance[o];
						break;
					}
				}
			}
		}
		_instance = resInstance;
	};
};

Copyright © 2016 Francesco Mele. All rights reserved.