~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to local/in/dhis-web-ga/javascript/hashtable.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * <p>Implements a sortable hashtable which accepts non-<code>null</code> String
 
3
 * keys and non-<code>null</code> values.</p>
 
4
 *
 
5
 * <p>This object is primarily intended to address the shortcomings of using an
 
6
 * Object as a hashtable, by managing the names of the properties which are
 
7
 * stored in an Object.</p>
 
8
 *
 
9
 * <p>HashTable does not define a hashing function, as Object's built-in
 
10
 * hashing is used for storing and retrieving items.</p>
 
11
 *
 
12
 * <p>Initial values are taken from any Objects passed into the constructor:
 
13
 * if another HashTable is given as an argument, its contents will be taken.</p>
 
14
 *
 
15
 * @constructor
 
16
 */
 
17
var HashTable = function()
 
18
{
 
19
   /**
 
20
    * Storage object - values are stored as properties whose names are hashtable
 
21
    * keys.
 
22
    *
 
23
    * @type Object
 
24
    * @private
 
25
    */
 
26
    this._store = {};
 
27
 
 
28
   /**
 
29
    * A list of hashtable keys.
 
30
    *
 
31
    * @type Array
 
32
    * @private
 
33
    */
 
34
    this._keys = [];
 
35
 
 
36
    for (var i = 0; i < arguments.length; i++)
 
37
    {
 
38
        this._putFromObject(arguments[i]);
 
39
    }
 
40
};
 
41
 
 
42
/**
 
43
 * Puts all properties from a given HashTable or Object into the hashtable.
 
44
 *
 
45
 * @param {Object} source an object whose values will be put into the hashtable.
 
46
 * @private
 
47
 */
 
48
HashTable.prototype._putFromObject = function(source)
 
49
{
 
50
    if (source.constructor == HashTable)
 
51
    {
 
52
        source.forEach(function(k, v)
 
53
        {
 
54
            this.put(k, v);
 
55
        }, this);
 
56
    }
 
57
    else
 
58
    {
 
59
        for (property in source)
 
60
        {
 
61
            if (source.hasOwnProperty(property))
 
62
            {
 
63
                this.put(property, source[property]);
 
64
            }
 
65
        }
 
66
    }
 
67
};
 
68
 
 
69
/**
 
70
 * Clears the hashtable.
 
71
 */
 
72
HashTable.prototype.clear = function()
 
73
{
 
74
    this._store = {};
 
75
    this._keys = [];
 
76
};
 
77
 
 
78
/**
 
79
 * An iterator which calls the given function, passing key, value, item index and a
 
80
 * reference to this Hashtable for each item in the hashtable.
 
81
 *
 
82
 * @param {Function} callback the function to be called for each item.
 
83
 * @param {Object} context an optional context object for the calls to block.
 
84
 *
 
85
 * @see "Enumerating Javascript Objects", http://dean.edwards.name/weblog/2006/07/enum/
 
86
 */
 
87
HashTable.prototype.forEach = function(callback, context)
 
88
{
 
89
    for (var i = 0, l = this._keys.length; i < l; i++)
 
90
    {
 
91
        callback.call(context, this._keys[i], this._store[this._keys[i]], i, this);
 
92
    }
 
93
};
 
94
 
 
95
/**
 
96
 * Retrieves the item with the given key.
 
97
 *
 
98
 * @param {String} key the key for the item to be retrieved.
 
99
 * @return the item stored in this HashTable with the given key if one exists,
 
100
 *         <code>null</code> otherwise.
 
101
 */
 
102
HashTable.prototype.get = function(key)
 
103
{
 
104
    var result = null;
 
105
    if (typeof(this._store[key]) != "undefined")
 
106
    {
 
107
        result = this._store[key];
 
108
    }
 
109
    return result;
 
110
};
 
111
 
 
112
/**
 
113
 * Determines if the hashtable contains the given key.
 
114
 *
 
115
 * @param {String} key the key to be searched for.
 
116
 * @return <code>true</code> if this HashTable contains the given key,
 
117
 *         <code>false</code> otherwise.
 
118
 * @type Boolean
 
119
 */
 
120
HashTable.prototype.hasKey = function(key)
 
121
{
 
122
    var result = false;
 
123
    this.forEach(function(k)
 
124
    {
 
125
        if (key == k)
 
126
        {
 
127
            result = true;
 
128
            return true;
 
129
        }
 
130
    });
 
131
    return result;
 
132
};
 
133
 
 
134
/**
 
135
 * Determines if the hashtable contains the given value.
 
136
 *
 
137
 * @param {Object} value the value to be searched for.
 
138
 * @return <code>true</code> if this HashTable contains the given value,
 
139
 *         <code>false</code> otherwise.
 
140
 * @type Boolean
 
141
 */
 
142
HashTable.prototype.hasValue = function(value)
 
143
{
 
144
    var result = false;
 
145
    this.forEach(function(k, v)
 
146
    {
 
147
        if (value == v)
 
148
        {
 
149
            result = true;
 
150
            return true;
 
151
        }
 
152
    });
 
153
    return result;
 
154
};
 
155
 
 
156
/**
 
157
 * Creates Object representations of the items in the hashtable.
 
158
 *
 
159
 * @return the items in the hashtable, represented as Objects with "key"
 
160
 *         and "value" properties.
 
161
 * @type Array
 
162
 */
 
163
HashTable.prototype.items = function()
 
164
{
 
165
    var items = [];
 
166
    this.forEach(function(k, v)
 
167
    {
 
168
        items.push({"key": k, "value": v});
 
169
    });
 
170
    return items;
 
171
};
 
172
 
 
173
/**
 
174
 * Retrieves the hashtable's keys.
 
175
 *
 
176
 * @return the hashtable's keys.
 
177
 * @type Array
 
178
 */
 
179
HashTable.prototype.keys = function()
 
180
{
 
181
    var keys = [];
 
182
    this.forEach(function(key)
 
183
    {
 
184
        keys.push(key);
 
185
    });
 
186
    return keys;
 
187
};
 
188
 
 
189
/**
 
190
 * Puts an item into the hashtable.
 
191
 *
 
192
 * @param {String} key the key under which the item should be stored.
 
193
 * @param {Object} value the item to be stored.
 
194
 */
 
195
HashTable.prototype.put = function(key, value)
 
196
{
 
197
    if (key == undefined || key == null || typeof(key) != "string"
 
198
        || value == undefined || value == null)
 
199
    {
 
200
        return;
 
201
    }
 
202
 
 
203
    if (typeof(this._store[key]) == "undefined")
 
204
    {
 
205
        this._keys.push(key);
 
206
    }
 
207
 
 
208
    this._store[key] = value;
 
209
};
 
210
 
 
211
/**
 
212
 * Removes an item from the hashtable and returns it.
 
213
 *
 
214
 * @param {String} key the key for the item to be removed.
 
215
 * @return the item which was removed, or <code>null</code> if the item did not
 
216
 *         exist.
 
217
 */
 
218
HashTable.prototype.remove = function(key)
 
219
{
 
220
    var result = null;
 
221
    for (var i = 0, l = this._keys.length; i < l; i++)
 
222
    {
 
223
        if (key == this._keys[i])
 
224
        {
 
225
            result = this._store[key];
 
226
            delete(this._store[key]);
 
227
            this._keys.splice(i, 1);
 
228
            break;
 
229
        }
 
230
    }
 
231
    return result;
 
232
};
 
233
 
 
234
/**
 
235
 * Determines the number of entries in the hashtable.
 
236
 *
 
237
 * @return the number of entries in this HashTable.
 
238
 * @type Number
 
239
 */
 
240
HashTable.prototype.size = function()
 
241
{
 
242
    return this._keys.length;
 
243
};
 
244
 
 
245
/**
 
246
 * Sorts the keys of the hashtable.
 
247
 *
 
248
 * @param {Function} comparator an optional function which will be used to sort
 
249
 *                              the keys - if not provided, they will be sorted
 
250
 *                              lexographically (in dictionary order).
 
251
 */
 
252
HashTable.prototype.sort = function(comparator)
 
253
{
 
254
    if (typeof(comparator) == "function")
 
255
    {
 
256
        this._keys.sort(comparator);
 
257
    }
 
258
    else
 
259
    {
 
260
        this._keys.sort();
 
261
    }
 
262
};
 
263
 
 
264
/**
 
265
 * Creates a String representation of the hashtable.
 
266
 *
 
267
 * @return a String representation of this {@link HashTable}.
 
268
 * @type String
 
269
 */
 
270
HashTable.prototype.toString = function()
 
271
{
 
272
    var result = "{";
 
273
    this.forEach(function(key, value, index)
 
274
    {
 
275
        if (index != 0)
 
276
        {
 
277
            result += ", ";
 
278
        }
 
279
        result += key + ": " + value;
 
280
    });
 
281
    result += "}";
 
282
    return result;
 
283
};
 
284
 
 
285
/**
 
286
 * Updates the hashtable with the values contained in a given {@link HashTable}
 
287
 * or Object.
 
288
 *
 
289
 * @param {Object} source an object whose values will be put into the hashtable.
 
290
 */
 
291
HashTable.prototype.update = function(source)
 
292
{
 
293
    this._putFromObject(source);
 
294
};
 
295
 
 
296
/**
 
297
 * Retrieves the hashtable's values.
 
298
 *
 
299
 * @return the hashtable's values.
 
300
 * @type Array
 
301
 */
 
302
HashTable.prototype.values = function()
 
303
{
 
304
    var values = [];
 
305
    this.forEach(function(key, value)
 
306
    {
 
307
        values.push(value);
 
308
    });
 
309
    return values;
 
310
};
 
 
b'\\ No newline at end of file'