js.util.Dictionary.js代码

js.util = (js.util || {});

js.util.Dictionary = newjs.lang.Class() ({

/** Utility class providing common java-like dictionary functionality tohelp keep code D-R-Y. **/

__init__ : function( map ) {

/** Initializer for Dictionary **/

this.clear();

// NOTE: be very careful when using the constructor as a wrapper for anexisting dictionary;

// this action iterates theentire dictionary.

if( map ) {

this.putAll( map );

}

},

clear : function() {

/** Clear contents of this dict. **/

this._size = 0;

this._dictionary = {};

},

containsKey : function( key ) {

/** Check if given key exists in this dict. **/

return( this._dictionary.hasOwnProperty( key ) );

},

containsValue : function( value ){

/** Check if given value exists in this dict. **/

var key;

this.iterate(function( k, v ) {

if( value === v ) {

key = k;

return( true );

}

});

return( key !== undefined );

},

entrySet : function() {

/** Retrieve a unique list of values from this dict. **/

var items = [];

// acquire entries

this.iterate(function( k, v ) {

items.push( v );

});

return( new js.util.Set( items ) );

},

get : function( key ) {

/** Retrieve value associated with given key. **/

return( this._dictionary[ key ] );

},

isEmpty : function() {

/** Check if this dict contains zero elements. **/

return( this.size() === 0 );

},

keySet : function() {

/** Retrieve a unique list of keys from this dict. **/

var keys = [];

// acquire entries

this.iterate( function( k, v ) {

keys.push( k );

});

return( new js.util.Set( keys ) );

},

put : function( key, value ) {

/** Assign given value to given key within this dict. **/

if( ! this._dictionary.hasOwnProperty( key ) ) {

this._size++;

}

this._dictionary[ key ] = value;

return( this );

},

putAll : function( map ) {

/** Assign every value within given map to their associated keys, withinthis dict. **/

var that = this;

map.iterate( function( k, v ) {

that.put( k, v );

});

},

iterate : function( callback ) {

/** Convenient, unified method of iterating elements in this dict.

This pattern is common to all collection classes. **/

var dictionary = this._dictionary;

for( var property in dictionary ){

if( ! dictionary.hasOwnProperty( property ) ) { continue; }

if( callback( property, dictionary[ property ] ) ) {

break;

}

}

},

remove : function( key ) {

/** Remove key from this dict. **/

var success = false;

if( this._dictionary.hasOwnProperty( key ) ) {

success = delete( this._dictionary[ key ] );

this._size--;

}

return( success );

},

size : function() {

/** Retrieve the size of this dict. **/

return( this._size );

},

values : function() {

/** Retrieve a list of all values within this dict. **/

var values = [];

for( var key in this._dictionary ){

if( ! this._dictionary.hasOwnProperty( key ) ) { continue; }

values.push( key );

}

return( new js.util.ArrayList( values ) );

},

clone : function() {

/** Retrieve a deep-clone (if values implement a clone method), of thisdict. **/

var dictionary = new js.util.Dictionary();

this.iterate( function( k, v ) {

dictionary.put( k, (v && (v.clone instanceof Function) ?v.clone() : v) );

});

return( dictionary );

},

toDict : function() {

/** Retrieve a deep-clone (if values implement a clone method), of

this dict as an Object rather than a Dictionary. **/

return( this.clone()._dictionary );

}

})

.Static({

Iterate : function( dictionary, callback ) {

/** Iterate given dict, invoking given callback for each element.

Helper method to avoid the requirement to instantiate this class forone-off use-cases. **/

js.util.Dictionary.prototype.iterate.call( { _dictionary : dictionary },callback );

},

Iterator : function( dictionary ){

/** Retrieve an object which contains an 'iterate' method to be invokedat a later date. **/

// TODO: implement full iterator capabilities.

return({

iterate : function( callback ) {

js.util.Dictionary.prototype.iterate.call( { _dictionary : dictionary },callback );

}

});

}

});

转自http://code.google.com/p/javascript-classes/source/browse/trunk/js.util.Dictionary.js
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值