123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /*
- *+------------------------------------------------------------------------+
- *| Licensed Materials - Property of IBM
- *| IBM Cognos Products: Viewer
- *| (C) Copyright IBM Corp. 2001, 2011
- *|
- *| US Government Users Restricted Rights - Use, duplication or
- *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- *|
- *+------------------------------------------------------------------------+
- */
- //
- // This class implement a generic Dictionary data structure.
- //
- var DICTIONARY_INVALID_KEY = -1;
- var DICTIONARY_SUCCESS = 1;
- /*
- Constructor
- Parameters:
- Returns:
- */
- function CDictionary()
- {
- // CDictionary uses an array to store key/value pairs.
- this.m_aValues = {};
- }
- /*
- Adds a key/value pair in the dictionary.
- If the key was used before, its value will be overwrite.
- Parameters:
- key (string) - The key to look for in the dictionary.
- value (object) - The value associated to the key
- Returns:
- */
- function CDictionary_add(sKey, oValue)
- {
- if (typeof sKey != "string" && typeof sKey != "number")
- {
- return DICTIONARY_INVALID_KEY;
- }
- this.m_aValues[sKey] = oValue;
- return DICTIONARY_SUCCESS;
- }
- /*
- Returns true if the keys is already used.
- Parameters:
- key (string) - The key to look for in the dictionary.
- Returns:
- a boolean.
- */
- function CDictionary_exists(sKey)
- {
- if (typeof sKey != "string" && typeof sKey != "number")
- {
- return false;
- }
- return (typeof this.m_aValues[sKey] != "undefined");
- }
- /*
- Returns the associated to a key in the dictionary.
- Returns null if the key doesn't exists.
- Parameters:
- key (string) - the key to look for
- Returns:
- an object, associated to the key.
- */
- function CDictionary_get(sKey)
- {
- if (typeof sKey != "string" && typeof sKey != "number")
- {
- return null;
- }
- if (this.exists(sKey) === true)
- {
- return this.m_aValues[sKey];
- }
- else
- {
- return null;
- }
- }
- /*
- Returns all keys used in this dictionary. Ordered alphabetically.
- Returns an empty array if the dictionary is empty.
- Parameters:
- Returns:
- a sorted array of strings.
- */
- function CDictionary_keys()
- {
- var aKeys = [];
- for (var idxValue in this.m_aValues)
- {
- aKeys.push(idxValue);
- }
- return aKeys.sort();
- }
- /*
- Remove a key/value pair from this dictionary.
- Parameters:
- key (string) to remove from the dictionary.
- Returns:
- Returns the object removed. Null if the key didn't exists.
- */
- function CDictionary_remove(sKey)
- {
- if (typeof sKey != "string" && typeof sKey != "number")
- {
- return DICTIONARY_INVALID_KEY;
- }
- var oRetVal = this.get(sKey);
- delete this.m_aValues[sKey];
- return oRetVal;
- }
- /*
- Removes all key/value pairs from this dictionary.
- Parameters:
- Returns:
- */
- function CDictionary_removeAll()
- {
- this.m_aValues = [];
- return DICTIONARY_SUCCESS;
- }
- /*
- Adds all of the entries from parameter oNewDictionary into this dictionary. Any duplicate
- values already in this dictionary will be overwritten by the new values in the new dictionary.
- Parameters:
- oNewDictionay (object - CDictionary) a new dictionary of >=1 length.
- Returns:
- */
- function CDictionary_append(oNewDictionary)
- {
- if (oNewDictionary instanceof CDictionary && oNewDictionary.keys().length > 0)
- {
- var aKeys = oNewDictionary.keys();
- for (var idxKeys = 0; idxKeys < aKeys.length; idxKeys++)
- {
- this.add(aKeys[idxKeys], oNewDictionary.get(aKeys[idxKeys]));
- }
- }
- }
- // Add functions to the CDictionary class
- CDictionary.prototype.add = CDictionary_add;
- CDictionary.prototype.exists = CDictionary_exists;
- CDictionary.prototype.get = CDictionary_get;
- CDictionary.prototype.keys = CDictionary_keys;
- CDictionary.prototype.remove = CDictionary_remove;
- CDictionary.prototype.removeAll = CDictionary_removeAll;
- CDictionary.prototype.append = CDictionary_append;
|