mabello
20 Feb 2008, 4:42 PM
Some years ago, when the spectacular framework ExtJS was maybe a thought in the mind of Jack and all the great people following him in the development of the framework,
I was looking for an Hastable in javascript, and I found one thanks to Michael Synovic.
I have to say that the original code was buggy (actually I don't remember where exactly),
so I fixed this simple javascript code with my friend Stefano; I have to say that
I have used this Hastable in different projects and finding it very useful, I decided to issue this small class.
/**
Created by: Michael Synovic
on: 01/12/2003
This is a Javascript implementation of the Java Hashtable object,
revisioned by Marco Bellocchi and Stefano Sambi
Contructor(s):
Ext.Hashtable
Creates a new, empty hashtable
Method(s):
void clear()
Clears this hashtable so that it contains no keys.
boolean containsKey(String key)
Tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
Returns true if this TI.Core.Hashtable maps one or more keys to this value.
Object get(String key)
Returns the value to which the specified key is mapped in this hashtable.
boolean isEmpty()
Tests if this hashtable maps no keys to values.
Array keys()
Returns an array of the keys in this hashtable.
void put(String key, Object value)
Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
Object remove(String key)
Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
int size()
Returns the number of keys in this hashtable.
String toString()
Returns a string representation of this TI.Core.Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).
Array values()
Returns a array view of the values contained in this TI.Core.Hashtable.
*/
/**
Created by: Michael Synovic
on: 01/12/2003
This is a Javascript implementation of the Java Hashtable object.
Contructor(s):
Ext.ux.Hashtable()
Creates a new, empty hashtable
Method(s):
void clear()
Clears this hashtable so that it contains no keys.
boolean containsKey(String key)
Tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
Returns true if this TI.Core.Hashtable maps one or more keys to this value.
Object get(String key)
Returns the value to which the specified key is mapped in this hashtable.
boolean isEmpty()
Tests if this hashtable maps no keys to values.
Array keys()
Returns an array of the keys in this hashtable.
void put(String key, Object value)
Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
Object remove(String key)
Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
int size()
Returns the number of keys in this hashtable.
String toString()
Returns a string representation of this TI.Core.Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).
Array values()
Returns a array view of the values contained in this TI.Core.Hashtable.
*/
//Get rid of these 2 line when using with Ext
//******
Ext = {};
Ext.ux = {};
//******
Ext.ux.Hashtable = function()
{
this.clear = hashtable_clear;
this.containsKey = hashtable_containsKey;
this.containsValue = hashtable_containsValue;
this.get = hashtable_get;
this.isEmpty = hashtable_isEmpty;
this.keys = hashtable_keys;
this.put = hashtable_put;
this.remove = hashtable_remove;
this.size = hashtable_size;
this.toString = hashtable_toString;
this.values = hashtable_values;
var items = {};
var count = 0;
// serve per confrontare che non sia una proprieta nativa
var testObject = {};
/*=======Private methods for internal use only========*/
function hashtable_clear(){
items = {};
count = 0;
}
function hashtable_containsKey(key){
if (testObject[key]) {
return false;
}
return (items[key] != null);
}
function hashtable_containsValue(value){
var a = getValueArray();
for (var htIndex = 0; htIndex < a.length; htIndex++) {
if (a[htIndex] == value) {
return true;
}
}
return false;
}
function hashtable_get(key){
if(key in items){
return items[key]; // object
}
return undefined;
}
function hashtable_isEmpty(){
return (count == 0);
}
function hashtable_keys(){
return getKeyArray();
}
function hashtable_put(key, value){
if (key == null || value == null) {
throw "NullPointerException {" + key + "},{" + value + "}";
}else{
var alreadyExists = (key in items);
items[key] = value;
if(!alreadyExists){
count++;
}
}
}
function hashtable_remove(key){
if(key in items && !testObject[key]){
delete items[key];
count--;
return true;
}
return false;
}
function hashtable_size(){
return count;
}
function hashtable_toString(){
var result = '';
var arrKeys = getKeyArray();
for (var i = 0; i < arrKeys.length; i++)
{
if (TI.Util.isNullOrUndefined(items[arrKeys[i]]))
result += "{" + arrKeys[i] + "},{" + items[arrKeys[i]] + "}\n";
}
return result;
}
function hashtable_values(){
return getValueArray();
}
function getValueArray() {
var a = [];
for(var p in items){
if(!testObject[p]){
a.push(items[p]);
}
}
return a;
}
function getKeyArray() {
var a = [];
for(var p in items){
if(!testObject[p]){
a.push(p);
}
}
return a;
}
}
//TEST
function testHashtableCreation() {
var ht = new Ext.ux.Hashtable();
assertNotNull("the new Hashtable instance is not null", ht);
}
function testClear() {
var ht = new Ext.ux.Hashtable();
ht.put("a", "a");
assertFalse("Hashtable is not empty", ht.isEmpty());
ht.clear();
assertTrue("Hashtable is empty", ht.isEmpty());
}
function testContainsKey() {
var ht = new Ext.ux.Hashtable();
assertFalse("Hashtable not contains key 'theKey'", ht.containsKey("theKey"));
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains key 1'theKey'", ht.containsKey("theKey"));
assertTrue("Hashtable contains key 2'theKey'", ht.containsKey(key));
}
function testContainsValue() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertFalse("Hashtable contains value b", ht.containsValue({}));
}
function testGet() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key , b);
var obj = ht.get(key);
assertNotNull("the obj is not null", obj);
assertEquals("key and returned key are the same reference", obj, b);
}
function testIsEmpty() {
var ht = new Ext.ux.Hashtable();
assertTrue("Hashtable is empty", ht.isEmpty());
}
function testPut() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertTrue("Hashtable contains value b", ht.containsKey(key));
}
function testRemove() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertTrue("Hashtable contains value b", ht.containsKey(key));
ht.remove(key);
assertFalse("Hashtable contains value b", ht.containsValue(b));
assertFalse("Hashtable contains value b", ht.containsKey(key));
}
function testSize() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertEquals("Hashtable size is 1", ht.size(), 1);
}
function testKeys() {
var ht = new Ext.ux.Hashtable();
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
var arrayKey = ht.keys();
assertNotNull("the keys are not null", arrayKey);
assertEquals("the keys array contains one element", arrayKey.length, 1);
}
function testValues() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
var arrayValue = ht.values();
assertNotNull("the values are not null", arrayValue);
assertEquals("the values array contains one element", arrayValue.length, 1);
}
</script>
Then if someone is curious, I have made some simple test using JSUnit, but you need to download JSUnit to work with it.
Anyway, this is the test page using JSUnit:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Hashtable</title>
<link rel="stylesheet" type="text/css" href="../css/jsUnitStyle.css">
<script language="JavaScript" type="text/javascript" src="../app/jsUnitCore.js"></script>
<script language="javascript">
/**
Created by: Michael Synovic
on: 01/12/2003
This is a Javascript implementation of the Java Hashtable object,
revisioned by Marco Bellocchi and Stefano Sambi
Contructor(s):
Ext.Hashtable
Creates a new, empty hashtable
Method(s):
void clear()
Clears this hashtable so that it contains no keys.
boolean containsKey(String key)
Tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
Returns true if this TI.Core.Hashtable maps one or more keys to this value.
Object get(String key)
Returns the value to which the specified key is mapped in this hashtable.
boolean isEmpty()
Tests if this hashtable maps no keys to values.
Array keys()
Returns an array of the keys in this hashtable.
void put(String key, Object value)
Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
Object remove(String key)
Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
int size()
Returns the number of keys in this hashtable.
String toString()
Returns a string representation of this TI.Core.Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).
Array values()
Returns a array view of the values contained in this TI.Core.Hashtable.
*/
/**
Created by: Michael Synovic
on: 01/12/2003
This is a Javascript implementation of the Java TI.Core.Hashtable object.
Contructor(s):
TI.Core.Hashtable()
Creates a new, empty hashtable
Method(s):
void clear()
Clears this hashtable so that it contains no keys.
boolean containsKey(String key)
Tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
Returns true if this TI.Core.Hashtable maps one or more keys to this value.
Object get(String key)
Returns the value to which the specified key is mapped in this hashtable.
boolean isEmpty()
Tests if this hashtable maps no keys to values.
Array keys()
Returns an array of the keys in this hashtable.
void put(String key, Object value)
Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
Object remove(String key)
Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
int size()
Returns the number of keys in this hashtable.
String toString()
Returns a string representation of this TI.Core.Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).
Array values()
Returns a array view of the values contained in this TI.Core.Hashtable.
*/
Ext = {};
Ext.ux = {};
Ext.ux.Hashtable = function()
{
this.clear = hashtable_clear;
this.containsKey = hashtable_containsKey;
this.containsValue = hashtable_containsValue;
this.get = hashtable_get;
this.isEmpty = hashtable_isEmpty;
this.keys = hashtable_keys;
this.put = hashtable_put;
this.remove = hashtable_remove;
this.size = hashtable_size;
this.toString = hashtable_toString;
this.values = hashtable_values;
var items = {};
var count = 0;
// serve per confrontare che non sia una proprieta nativa
var testObject = {};
/*=======Private methods for internal use only========*/
function hashtable_clear(){
items = {};
count = 0;
}
function hashtable_containsKey(key){
if (testObject[key]) {
return false;
}
return (items[key] != null);
}
function hashtable_containsValue(value){
var a = getValueArray();
for (var htIndex = 0; htIndex < a.length; htIndex++) {
if (a[htIndex] == value) {
return true;
}
}
return false;
}
function hashtable_get(key){
if(key in items){
return items[key]; // object
}
return undefined;
}
function hashtable_isEmpty(){
return (count == 0);
}
function hashtable_keys(){
return getKeyArray();
}
function hashtable_put(key, value){
if (key == null || value == null) {
throw "NullPointerException {" + key + "},{" + value + "}";
}else{
var alreadyExists = (key in items);
items[key] = value;
if(!alreadyExists){
count++;
}
}
}
function hashtable_remove(key){
if(key in items && !testObject[key]){
delete items[key];
count--;
return true;
}
return false;
}
function hashtable_size(){
return count;
}
function hashtable_toString(){
var result = '';
var arrKeys = getKeyArray();
for (var i = 0; i < arrKeys.length; i++)
{
if (TI.Util.isNullOrUndefined(items[arrKeys[i]]))
result += "{" + arrKeys[i] + "},{" + items[arrKeys[i]] + "}\n";
}
return result;
}
function hashtable_values(){
return getValueArray();
}
function getValueArray() {
var a = [];
for(var p in items){
if(!testObject[p]){
a.push(items[p]);
}
}
return a;
}
function getKeyArray() {
var a = [];
for(var p in items){
if(!testObject[p]){
a.push(p);
}
}
return a;
}
}
//TEST
function testHashtableCreation() {
var ht = new Ext.ux.Hashtable();
assertNotNull("the new Hashtable instance is not null", ht);
}
function testClear() {
var ht = new Ext.ux.Hashtable();
ht.put("a", "a");
assertFalse("Hashtable is not empty", ht.isEmpty());
ht.clear();
assertTrue("Hashtable is empty", ht.isEmpty());
}
function testContainsKey() {
var ht = new Ext.ux.Hashtable();
assertFalse("Hashtable not contains key 'theKey'", ht.containsKey("theKey"));
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains key 1'theKey'", ht.containsKey("theKey"));
assertTrue("Hashtable contains key 2'theKey'", ht.containsKey(key));
}
function testContainsValue() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertFalse("Hashtable contains value b", ht.containsValue({}));
}
function testGet() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key , b);
var obj = ht.get(key);
assertNotNull("the obj is not null", obj);
assertEquals("key and returned key are the same reference", obj, b);
}
function testIsEmpty() {
var ht = new Ext.ux.Hashtable();
assertTrue("Hashtable is empty", ht.isEmpty());
}
function testPut() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertTrue("Hashtable contains value b", ht.containsKey(key));
}
function testRemove() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertTrue("Hashtable contains value b", ht.containsKey(key));
ht.remove(key);
assertFalse("Hashtable contains value b", ht.containsValue(b));
assertFalse("Hashtable contains value b", ht.containsKey(key));
}
function testSize() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertEquals("Hashtable size is 1", ht.size(), 1);
}
function testKeys() {
var ht = new Ext.ux.Hashtable();
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
var arrayKey = ht.keys();
assertNotNull("the keys are not null", arrayKey);
assertEquals("the keys array contains one element", arrayKey.length, 1);
}
function testValues() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
var arrayValue = ht.values();
assertNotNull("the values are not null", arrayValue);
assertEquals("the values array contains one element", arrayValue.length, 1);
}
</script>
</head>
<body>
<h1>Hastable test</h1>
<p>This page contains tests for the Hashtable.</p>
</body>
</html>
Keep up the great work!!
Marco
I was looking for an Hastable in javascript, and I found one thanks to Michael Synovic.
I have to say that the original code was buggy (actually I don't remember where exactly),
so I fixed this simple javascript code with my friend Stefano; I have to say that
I have used this Hastable in different projects and finding it very useful, I decided to issue this small class.
/**
Created by: Michael Synovic
on: 01/12/2003
This is a Javascript implementation of the Java Hashtable object,
revisioned by Marco Bellocchi and Stefano Sambi
Contructor(s):
Ext.Hashtable
Creates a new, empty hashtable
Method(s):
void clear()
Clears this hashtable so that it contains no keys.
boolean containsKey(String key)
Tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
Returns true if this TI.Core.Hashtable maps one or more keys to this value.
Object get(String key)
Returns the value to which the specified key is mapped in this hashtable.
boolean isEmpty()
Tests if this hashtable maps no keys to values.
Array keys()
Returns an array of the keys in this hashtable.
void put(String key, Object value)
Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
Object remove(String key)
Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
int size()
Returns the number of keys in this hashtable.
String toString()
Returns a string representation of this TI.Core.Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).
Array values()
Returns a array view of the values contained in this TI.Core.Hashtable.
*/
/**
Created by: Michael Synovic
on: 01/12/2003
This is a Javascript implementation of the Java Hashtable object.
Contructor(s):
Ext.ux.Hashtable()
Creates a new, empty hashtable
Method(s):
void clear()
Clears this hashtable so that it contains no keys.
boolean containsKey(String key)
Tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
Returns true if this TI.Core.Hashtable maps one or more keys to this value.
Object get(String key)
Returns the value to which the specified key is mapped in this hashtable.
boolean isEmpty()
Tests if this hashtable maps no keys to values.
Array keys()
Returns an array of the keys in this hashtable.
void put(String key, Object value)
Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
Object remove(String key)
Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
int size()
Returns the number of keys in this hashtable.
String toString()
Returns a string representation of this TI.Core.Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).
Array values()
Returns a array view of the values contained in this TI.Core.Hashtable.
*/
//Get rid of these 2 line when using with Ext
//******
Ext = {};
Ext.ux = {};
//******
Ext.ux.Hashtable = function()
{
this.clear = hashtable_clear;
this.containsKey = hashtable_containsKey;
this.containsValue = hashtable_containsValue;
this.get = hashtable_get;
this.isEmpty = hashtable_isEmpty;
this.keys = hashtable_keys;
this.put = hashtable_put;
this.remove = hashtable_remove;
this.size = hashtable_size;
this.toString = hashtable_toString;
this.values = hashtable_values;
var items = {};
var count = 0;
// serve per confrontare che non sia una proprieta nativa
var testObject = {};
/*=======Private methods for internal use only========*/
function hashtable_clear(){
items = {};
count = 0;
}
function hashtable_containsKey(key){
if (testObject[key]) {
return false;
}
return (items[key] != null);
}
function hashtable_containsValue(value){
var a = getValueArray();
for (var htIndex = 0; htIndex < a.length; htIndex++) {
if (a[htIndex] == value) {
return true;
}
}
return false;
}
function hashtable_get(key){
if(key in items){
return items[key]; // object
}
return undefined;
}
function hashtable_isEmpty(){
return (count == 0);
}
function hashtable_keys(){
return getKeyArray();
}
function hashtable_put(key, value){
if (key == null || value == null) {
throw "NullPointerException {" + key + "},{" + value + "}";
}else{
var alreadyExists = (key in items);
items[key] = value;
if(!alreadyExists){
count++;
}
}
}
function hashtable_remove(key){
if(key in items && !testObject[key]){
delete items[key];
count--;
return true;
}
return false;
}
function hashtable_size(){
return count;
}
function hashtable_toString(){
var result = '';
var arrKeys = getKeyArray();
for (var i = 0; i < arrKeys.length; i++)
{
if (TI.Util.isNullOrUndefined(items[arrKeys[i]]))
result += "{" + arrKeys[i] + "},{" + items[arrKeys[i]] + "}\n";
}
return result;
}
function hashtable_values(){
return getValueArray();
}
function getValueArray() {
var a = [];
for(var p in items){
if(!testObject[p]){
a.push(items[p]);
}
}
return a;
}
function getKeyArray() {
var a = [];
for(var p in items){
if(!testObject[p]){
a.push(p);
}
}
return a;
}
}
//TEST
function testHashtableCreation() {
var ht = new Ext.ux.Hashtable();
assertNotNull("the new Hashtable instance is not null", ht);
}
function testClear() {
var ht = new Ext.ux.Hashtable();
ht.put("a", "a");
assertFalse("Hashtable is not empty", ht.isEmpty());
ht.clear();
assertTrue("Hashtable is empty", ht.isEmpty());
}
function testContainsKey() {
var ht = new Ext.ux.Hashtable();
assertFalse("Hashtable not contains key 'theKey'", ht.containsKey("theKey"));
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains key 1'theKey'", ht.containsKey("theKey"));
assertTrue("Hashtable contains key 2'theKey'", ht.containsKey(key));
}
function testContainsValue() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertFalse("Hashtable contains value b", ht.containsValue({}));
}
function testGet() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key , b);
var obj = ht.get(key);
assertNotNull("the obj is not null", obj);
assertEquals("key and returned key are the same reference", obj, b);
}
function testIsEmpty() {
var ht = new Ext.ux.Hashtable();
assertTrue("Hashtable is empty", ht.isEmpty());
}
function testPut() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertTrue("Hashtable contains value b", ht.containsKey(key));
}
function testRemove() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertTrue("Hashtable contains value b", ht.containsKey(key));
ht.remove(key);
assertFalse("Hashtable contains value b", ht.containsValue(b));
assertFalse("Hashtable contains value b", ht.containsKey(key));
}
function testSize() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertEquals("Hashtable size is 1", ht.size(), 1);
}
function testKeys() {
var ht = new Ext.ux.Hashtable();
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
var arrayKey = ht.keys();
assertNotNull("the keys are not null", arrayKey);
assertEquals("the keys array contains one element", arrayKey.length, 1);
}
function testValues() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
var arrayValue = ht.values();
assertNotNull("the values are not null", arrayValue);
assertEquals("the values array contains one element", arrayValue.length, 1);
}
</script>
Then if someone is curious, I have made some simple test using JSUnit, but you need to download JSUnit to work with it.
Anyway, this is the test page using JSUnit:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Hashtable</title>
<link rel="stylesheet" type="text/css" href="../css/jsUnitStyle.css">
<script language="JavaScript" type="text/javascript" src="../app/jsUnitCore.js"></script>
<script language="javascript">
/**
Created by: Michael Synovic
on: 01/12/2003
This is a Javascript implementation of the Java Hashtable object,
revisioned by Marco Bellocchi and Stefano Sambi
Contructor(s):
Ext.Hashtable
Creates a new, empty hashtable
Method(s):
void clear()
Clears this hashtable so that it contains no keys.
boolean containsKey(String key)
Tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
Returns true if this TI.Core.Hashtable maps one or more keys to this value.
Object get(String key)
Returns the value to which the specified key is mapped in this hashtable.
boolean isEmpty()
Tests if this hashtable maps no keys to values.
Array keys()
Returns an array of the keys in this hashtable.
void put(String key, Object value)
Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
Object remove(String key)
Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
int size()
Returns the number of keys in this hashtable.
String toString()
Returns a string representation of this TI.Core.Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).
Array values()
Returns a array view of the values contained in this TI.Core.Hashtable.
*/
/**
Created by: Michael Synovic
on: 01/12/2003
This is a Javascript implementation of the Java TI.Core.Hashtable object.
Contructor(s):
TI.Core.Hashtable()
Creates a new, empty hashtable
Method(s):
void clear()
Clears this hashtable so that it contains no keys.
boolean containsKey(String key)
Tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
Returns true if this TI.Core.Hashtable maps one or more keys to this value.
Object get(String key)
Returns the value to which the specified key is mapped in this hashtable.
boolean isEmpty()
Tests if this hashtable maps no keys to values.
Array keys()
Returns an array of the keys in this hashtable.
void put(String key, Object value)
Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
Object remove(String key)
Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
int size()
Returns the number of keys in this hashtable.
String toString()
Returns a string representation of this TI.Core.Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).
Array values()
Returns a array view of the values contained in this TI.Core.Hashtable.
*/
Ext = {};
Ext.ux = {};
Ext.ux.Hashtable = function()
{
this.clear = hashtable_clear;
this.containsKey = hashtable_containsKey;
this.containsValue = hashtable_containsValue;
this.get = hashtable_get;
this.isEmpty = hashtable_isEmpty;
this.keys = hashtable_keys;
this.put = hashtable_put;
this.remove = hashtable_remove;
this.size = hashtable_size;
this.toString = hashtable_toString;
this.values = hashtable_values;
var items = {};
var count = 0;
// serve per confrontare che non sia una proprieta nativa
var testObject = {};
/*=======Private methods for internal use only========*/
function hashtable_clear(){
items = {};
count = 0;
}
function hashtable_containsKey(key){
if (testObject[key]) {
return false;
}
return (items[key] != null);
}
function hashtable_containsValue(value){
var a = getValueArray();
for (var htIndex = 0; htIndex < a.length; htIndex++) {
if (a[htIndex] == value) {
return true;
}
}
return false;
}
function hashtable_get(key){
if(key in items){
return items[key]; // object
}
return undefined;
}
function hashtable_isEmpty(){
return (count == 0);
}
function hashtable_keys(){
return getKeyArray();
}
function hashtable_put(key, value){
if (key == null || value == null) {
throw "NullPointerException {" + key + "},{" + value + "}";
}else{
var alreadyExists = (key in items);
items[key] = value;
if(!alreadyExists){
count++;
}
}
}
function hashtable_remove(key){
if(key in items && !testObject[key]){
delete items[key];
count--;
return true;
}
return false;
}
function hashtable_size(){
return count;
}
function hashtable_toString(){
var result = '';
var arrKeys = getKeyArray();
for (var i = 0; i < arrKeys.length; i++)
{
if (TI.Util.isNullOrUndefined(items[arrKeys[i]]))
result += "{" + arrKeys[i] + "},{" + items[arrKeys[i]] + "}\n";
}
return result;
}
function hashtable_values(){
return getValueArray();
}
function getValueArray() {
var a = [];
for(var p in items){
if(!testObject[p]){
a.push(items[p]);
}
}
return a;
}
function getKeyArray() {
var a = [];
for(var p in items){
if(!testObject[p]){
a.push(p);
}
}
return a;
}
}
//TEST
function testHashtableCreation() {
var ht = new Ext.ux.Hashtable();
assertNotNull("the new Hashtable instance is not null", ht);
}
function testClear() {
var ht = new Ext.ux.Hashtable();
ht.put("a", "a");
assertFalse("Hashtable is not empty", ht.isEmpty());
ht.clear();
assertTrue("Hashtable is empty", ht.isEmpty());
}
function testContainsKey() {
var ht = new Ext.ux.Hashtable();
assertFalse("Hashtable not contains key 'theKey'", ht.containsKey("theKey"));
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains key 1'theKey'", ht.containsKey("theKey"));
assertTrue("Hashtable contains key 2'theKey'", ht.containsKey(key));
}
function testContainsValue() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertFalse("Hashtable contains value b", ht.containsValue({}));
}
function testGet() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key , b);
var obj = ht.get(key);
assertNotNull("the obj is not null", obj);
assertEquals("key and returned key are the same reference", obj, b);
}
function testIsEmpty() {
var ht = new Ext.ux.Hashtable();
assertTrue("Hashtable is empty", ht.isEmpty());
}
function testPut() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertTrue("Hashtable contains value b", ht.containsKey(key));
}
function testRemove() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertTrue("Hashtable contains value b", ht.containsValue(b));
assertTrue("Hashtable contains value b", ht.containsKey(key));
ht.remove(key);
assertFalse("Hashtable contains value b", ht.containsValue(b));
assertFalse("Hashtable contains value b", ht.containsKey(key));
}
function testSize() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
assertEquals("Hashtable size is 1", ht.size(), 1);
}
function testKeys() {
var ht = new Ext.ux.Hashtable();
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
var arrayKey = ht.keys();
assertNotNull("the keys are not null", arrayKey);
assertEquals("the keys array contains one element", arrayKey.length, 1);
}
function testValues() {
var ht = new Ext.ux.Hashtable();
var key = "theKey";
var b = {};
ht.put(key, b);
var arrayValue = ht.values();
assertNotNull("the values are not null", arrayValue);
assertEquals("the values array contains one element", arrayValue.length, 1);
}
</script>
</head>
<body>
<h1>Hastable test</h1>
<p>This page contains tests for the Hashtable.</p>
</body>
</html>
Keep up the great work!!
Marco