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
分享到:
相关推荐
Set<String> dictionary = new TreeSet(Arrays.asList(dictionaryStr.split(","))); List<String> segmentedWords = segment(sentence, dictionary); System.out.println(String.join(",", segmentedWords)); ...
这些类可以简化代码编写,提高效率,并且通常已经经过了大量的测试和优化,以确保其稳定性和性能。这里我们将深入探讨一下“常用类”这个概念,以及在不同编程语言中如何利用它们。 在Java中,`java.util`包就是一...
自Java 1.2起,`java.util.Map`接口和其实现如`HashMap`、`TreeMap`等提供了更现代和灵活的键值对存储方式。尽管`Hashtable`仍然可用,但在新代码中通常推荐使用`HashMap`,因为它不强制线程安全,允许更好的性能,...
The json module: JavaScript Object Notation The plistlib module: A Property-List Parser ctypes Enhancements Improved SSL Support Deprecations and Removals Build and C API Changes Port-Specific ...
<importresource= "classpath:alfresco/extension/templates/webscripts/org/alfresco/util/json.js"> var curNode = context.node; // 处理节点数据并返回 ``` #### 7. 内容管理与用户体验 - **内容管理和分类...
import java.util.Arrays; public class ActivitySelection { public static int minActivities(int[][] activities) { Arrays.sort(activities, (a, b) -> a[0] - b[0]); // 按开始时间排序 int count = 0; ...
日本文 ... dicDir : '../dict/' // copy kuromoji.js's dictionary from node_modules/kuromoji/dist/dict. } var text = 'タイトル\n' + '\n' + '1 これは前段です。これは中段(2文の場合は後
`java.util`提供了集合框架、日期时间、随机数等功能。`java.io`处理输入输出,而`java.net`则处理网络通信。 2. **Get和Post的区别** GET和POST是HTTP协议中的两种主要请求方法。GET将参数附在URL后面,适合获取...
2. **语言库**:这一层包含了类似于 Java 中 util 包的各种语言工具 API。语言库提供了诸如字符串处理、日期操作等功能,使得开发者无需从头编写这些基础功能。 3. **环境相关包**:这部分处理跨浏览器兼容性问题,...
2. **语言库**:这一层包含了各种语言工具API,类似于Java的util包,提供了一些基础的语言工具,帮助开发者进行开发工作。 3. **环境相关包**:用于处理跨浏览器兼容性问题,确保Dojo可以在不同的浏览器环境中正常...
- **语言库**:提供一系列语言工具API,类似于Java的util包,涵盖了字符串处理、日期操作、事件管理和数据访问等功能,极大提升了代码的可读性和维护性。 - **环境相关包**:专注于解决跨浏览器兼容性问题,确保...