CDictionary.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2011
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. //
  13. // This class implement a generic Dictionary data structure.
  14. //
  15. var DICTIONARY_INVALID_KEY = -1;
  16. var DICTIONARY_SUCCESS = 1;
  17. /*
  18. Constructor
  19. Parameters:
  20. Returns:
  21. */
  22. function CDictionary()
  23. {
  24. // CDictionary uses an array to store key/value pairs.
  25. this.m_aValues = {};
  26. }
  27. /*
  28. Adds a key/value pair in the dictionary.
  29. If the key was used before, its value will be overwrite.
  30. Parameters:
  31. key (string) - The key to look for in the dictionary.
  32. value (object) - The value associated to the key
  33. Returns:
  34. */
  35. function CDictionary_add(sKey, oValue)
  36. {
  37. if (typeof sKey != "string" && typeof sKey != "number")
  38. {
  39. return DICTIONARY_INVALID_KEY;
  40. }
  41. this.m_aValues[sKey] = oValue;
  42. return DICTIONARY_SUCCESS;
  43. }
  44. /*
  45. Returns true if the keys is already used.
  46. Parameters:
  47. key (string) - The key to look for in the dictionary.
  48. Returns:
  49. a boolean.
  50. */
  51. function CDictionary_exists(sKey)
  52. {
  53. if (typeof sKey != "string" && typeof sKey != "number")
  54. {
  55. return false;
  56. }
  57. return (typeof this.m_aValues[sKey] != "undefined");
  58. }
  59. /*
  60. Returns the associated to a key in the dictionary.
  61. Returns null if the key doesn't exists.
  62. Parameters:
  63. key (string) - the key to look for
  64. Returns:
  65. an object, associated to the key.
  66. */
  67. function CDictionary_get(sKey)
  68. {
  69. if (typeof sKey != "string" && typeof sKey != "number")
  70. {
  71. return null;
  72. }
  73. if (this.exists(sKey) === true)
  74. {
  75. return this.m_aValues[sKey];
  76. }
  77. else
  78. {
  79. return null;
  80. }
  81. }
  82. /*
  83. Returns all keys used in this dictionary. Ordered alphabetically.
  84. Returns an empty array if the dictionary is empty.
  85. Parameters:
  86. Returns:
  87. a sorted array of strings.
  88. */
  89. function CDictionary_keys()
  90. {
  91. var aKeys = [];
  92. for (var idxValue in this.m_aValues)
  93. {
  94. aKeys.push(idxValue);
  95. }
  96. return aKeys.sort();
  97. }
  98. /*
  99. Remove a key/value pair from this dictionary.
  100. Parameters:
  101. key (string) to remove from the dictionary.
  102. Returns:
  103. Returns the object removed. Null if the key didn't exists.
  104. */
  105. function CDictionary_remove(sKey)
  106. {
  107. if (typeof sKey != "string" && typeof sKey != "number")
  108. {
  109. return DICTIONARY_INVALID_KEY;
  110. }
  111. var oRetVal = this.get(sKey);
  112. delete this.m_aValues[sKey];
  113. return oRetVal;
  114. }
  115. /*
  116. Removes all key/value pairs from this dictionary.
  117. Parameters:
  118. Returns:
  119. */
  120. function CDictionary_removeAll()
  121. {
  122. this.m_aValues = [];
  123. return DICTIONARY_SUCCESS;
  124. }
  125. /*
  126. Adds all of the entries from parameter oNewDictionary into this dictionary. Any duplicate
  127. values already in this dictionary will be overwritten by the new values in the new dictionary.
  128. Parameters:
  129. oNewDictionay (object - CDictionary) a new dictionary of >=1 length.
  130. Returns:
  131. */
  132. function CDictionary_append(oNewDictionary)
  133. {
  134. if (oNewDictionary instanceof CDictionary && oNewDictionary.keys().length > 0)
  135. {
  136. var aKeys = oNewDictionary.keys();
  137. for (var idxKeys = 0; idxKeys < aKeys.length; idxKeys++)
  138. {
  139. this.add(aKeys[idxKeys], oNewDictionary.get(aKeys[idxKeys]));
  140. }
  141. }
  142. }
  143. // Add functions to the CDictionary class
  144. CDictionary.prototype.add = CDictionary_add;
  145. CDictionary.prototype.exists = CDictionary_exists;
  146. CDictionary.prototype.get = CDictionary_get;
  147. CDictionary.prototype.keys = CDictionary_keys;
  148. CDictionary.prototype.remove = CDictionary_remove;
  149. CDictionary.prototype.removeAll = CDictionary_removeAll;
  150. CDictionary.prototype.append = CDictionary_append;