Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / docs / source / HashMap.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-util-HashMap'>/**
19 </span> * @class Ext.util.HashMap
20  * &lt;p&gt;
21  * Represents a collection of a set of key and value pairs. Each key in the HashMap
22  * must be unique, the same key cannot exist twice. Access to items is provided via
23  * the key only. Sample usage:
24  * &lt;pre&gt;&lt;code&gt;
25 var map = new Ext.util.HashMap();
26 map.add('key1', 1);
27 map.add('key2', 2);
28 map.add('key3', 3);
29
30 map.each(function(key, value, length){
31     console.log(key, value, length);
32 });
33  * &lt;/code&gt;&lt;/pre&gt;
34  * &lt;/p&gt;
35  *
36  * &lt;p&gt;The HashMap is an unordered class,
37  * there is no guarantee when iterating over the items that they will be in any particular
38  * order. If this is required, then use a {@link Ext.util.MixedCollection}.
39  * &lt;/p&gt;
40  */
41 Ext.define('Ext.util.HashMap', {
42
43 <span id='Ext-util-HashMap-cfg-keyFn'>    /**
44 </span>     * @cfg {Function} keyFn A function that is used to retrieve a default key for a passed object.
45      * A default is provided that returns the &lt;b&gt;id&lt;/b&gt; property on the object. This function is only used
46      * if the add method is called with a single argument.
47      */
48
49     mixins: {
50         observable: 'Ext.util.Observable'
51     },
52
53 <span id='Ext-util-HashMap-method-constructor'>    /**
54 </span>     * Creates new HashMap.
55      * @param {Object} config (optional) Config object.
56      */
57     constructor: function(config) {
58         config = config || {};
59         
60         var me = this,
61             keyFn = config.keyFn;
62
63         me.addEvents(
64 <span id='Ext-util-HashMap-event-add'>            /**
65 </span>             * @event add
66              * Fires when a new item is added to the hash
67              * @param {Ext.util.HashMap} this.
68              * @param {String} key The key of the added item.
69              * @param {Object} value The value of the added item.
70              */
71             'add',
72 <span id='Ext-util-HashMap-event-clear'>            /**
73 </span>             * @event clear
74              * Fires when the hash is cleared.
75              * @param {Ext.util.HashMap} this.
76              */
77             'clear',
78 <span id='Ext-util-HashMap-event-remove'>            /**
79 </span>             * @event remove
80              * Fires when an item is removed from the hash.
81              * @param {Ext.util.HashMap} this.
82              * @param {String} key The key of the removed item.
83              * @param {Object} value The value of the removed item.
84              */
85             'remove',
86 <span id='Ext-util-HashMap-event-replace'>            /**
87 </span>             * @event replace
88              * Fires when an item is replaced in the hash.
89              * @param {Ext.util.HashMap} this.
90              * @param {String} key The key of the replaced item.
91              * @param {Object} value The new value for the item.
92              * @param {Object} old The old value for the item.
93              */
94             'replace'
95         );
96
97         me.mixins.observable.constructor.call(me, config);
98         me.clear(true);
99         
100         if (keyFn) {
101             me.getKey = keyFn;
102         }
103     },
104
105 <span id='Ext-util-HashMap-method-getCount'>    /**
106 </span>     * Gets the number of items in the hash.
107      * @return {Number} The number of items in the hash.
108      */
109     getCount: function() {
110         return this.length;
111     },
112
113 <span id='Ext-util-HashMap-method-getData'>    /**
114 </span>     * Implementation for being able to extract the key from an object if only
115      * a single argument is passed.
116      * @private
117      * @param {String} key The key
118      * @param {Object} value The value
119      * @return {Array} [key, value]
120      */
121     getData: function(key, value) {
122         // if we have no value, it means we need to get the key from the object
123         if (value === undefined) {
124             value = key;
125             key = this.getKey(value);
126         }
127
128         return [key, value];
129     },
130
131 <span id='Ext-util-HashMap-method-getKey'>    /**
132 </span>     * Extracts the key from an object. This is a default implementation, it may be overridden
133      * @param {Object} o The object to get the key from
134      * @return {String} The key to use.
135      */
136     getKey: function(o) {
137         return o.id;
138     },
139
140 <span id='Ext-util-HashMap-method-add'>    /**
141 </span>     * Adds an item to the collection. Fires the {@link #add} event when complete.
142      * @param {String} key &lt;p&gt;The key to associate with the item, or the new item.&lt;/p&gt;
143      * &lt;p&gt;If a {@link #getKey} implementation was specified for this HashMap,
144      * or if the key of the stored items is in a property called &lt;tt&gt;&lt;b&gt;id&lt;/b&gt;&lt;/tt&gt;,
145      * the HashMap will be able to &lt;i&gt;derive&lt;/i&gt; the key for the new item.
146      * In this case just pass the new item in this parameter.&lt;/p&gt;
147      * @param {Object} o The item to add.
148      * @return {Object} The item added.
149      */
150     add: function(key, value) {
151         var me = this,
152             data;
153
154         if (arguments.length === 1) {
155             value = key;
156             key = me.getKey(value);
157         }
158
159         if (me.containsKey(key)) {
160             me.replace(key, value);
161         }
162
163         data = me.getData(key, value);
164         key = data[0];
165         value = data[1];
166         me.map[key] = value;
167         ++me.length;
168         me.fireEvent('add', me, key, value);
169         return value;
170     },
171
172 <span id='Ext-util-HashMap-method-replace'>    /**
173 </span>     * Replaces an item in the hash. If the key doesn't exist, the
174      * {@link #add} method will be used.
175      * @param {String} key The key of the item.
176      * @param {Object} value The new value for the item.
177      * @return {Object} The new value of the item.
178      */
179     replace: function(key, value) {
180         var me = this,
181             map = me.map,
182             old;
183
184         if (!me.containsKey(key)) {
185             me.add(key, value);
186         }
187         old = map[key];
188         map[key] = value;
189         me.fireEvent('replace', me, key, value, old);
190         return value;
191     },
192
193 <span id='Ext-util-HashMap-method-remove'>    /**
194 </span>     * Remove an item from the hash.
195      * @param {Object} o The value of the item to remove.
196      * @return {Boolean} True if the item was successfully removed.
197      */
198     remove: function(o) {
199         var key = this.findKey(o);
200         if (key !== undefined) {
201             return this.removeAtKey(key);
202         }
203         return false;
204     },
205
206 <span id='Ext-util-HashMap-method-removeAtKey'>    /**
207 </span>     * Remove an item from the hash.
208      * @param {String} key The key to remove.
209      * @return {Boolean} True if the item was successfully removed.
210      */
211     removeAtKey: function(key) {
212         var me = this,
213             value;
214
215         if (me.containsKey(key)) {
216             value = me.map[key];
217             delete me.map[key];
218             --me.length;
219             me.fireEvent('remove', me, key, value);
220             return true;
221         }
222         return false;
223     },
224
225 <span id='Ext-util-HashMap-method-get'>    /**
226 </span>     * Retrieves an item with a particular key.
227      * @param {String} key The key to lookup.
228      * @return {Object} The value at that key. If it doesn't exist, &lt;tt&gt;undefined&lt;/tt&gt; is returned.
229      */
230     get: function(key) {
231         return this.map[key];
232     },
233
234 <span id='Ext-util-HashMap-method-clear'>    /**
235 </span>     * Removes all items from the hash.
236      * @return {Ext.util.HashMap} this
237      */
238     clear: function(/* private */ initial) {
239         var me = this;
240         me.map = {};
241         me.length = 0;
242         if (initial !== true) {
243             me.fireEvent('clear', me);
244         }
245         return me;
246     },
247
248 <span id='Ext-util-HashMap-method-containsKey'>    /**
249 </span>     * Checks whether a key exists in the hash.
250      * @param {String} key The key to check for.
251      * @return {Boolean} True if they key exists in the hash.
252      */
253     containsKey: function(key) {
254         return this.map[key] !== undefined;
255     },
256
257 <span id='Ext-util-HashMap-method-contains'>    /**
258 </span>     * Checks whether a value exists in the hash.
259      * @param {Object} value The value to check for.
260      * @return {Boolean} True if the value exists in the dictionary.
261      */
262     contains: function(value) {
263         return this.containsKey(this.findKey(value));
264     },
265
266 <span id='Ext-util-HashMap-method-getKeys'>    /**
267 </span>     * Return all of the keys in the hash.
268      * @return {Array} An array of keys.
269      */
270     getKeys: function() {
271         return this.getArray(true);
272     },
273
274 <span id='Ext-util-HashMap-method-getValues'>    /**
275 </span>     * Return all of the values in the hash.
276      * @return {Array} An array of values.
277      */
278     getValues: function() {
279         return this.getArray(false);
280     },
281
282 <span id='Ext-util-HashMap-method-getArray'>    /**
283 </span>     * Gets either the keys/values in an array from the hash.
284      * @private
285      * @param {Boolean} isKey True to extract the keys, otherwise, the value
286      * @return {Array} An array of either keys/values from the hash.
287      */
288     getArray: function(isKey) {
289         var arr = [],
290             key,
291             map = this.map;
292         for (key in map) {
293             if (map.hasOwnProperty(key)) {
294                 arr.push(isKey ? key: map[key]);
295             }
296         }
297         return arr;
298     },
299
300 <span id='Ext-util-HashMap-method-each'>    /**
301 </span>     * Executes the specified function once for each item in the hash.
302      * Returning false from the function will cease iteration.
303      *
304      * The paramaters passed to the function are:
305      * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
306      * &lt;li&gt;&lt;b&gt;key&lt;/b&gt; : String&lt;p class=&quot;sub-desc&quot;&gt;The key of the item&lt;/p&gt;&lt;/li&gt;
307      * &lt;li&gt;&lt;b&gt;value&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The value of the item&lt;/p&gt;&lt;/li&gt;
308      * &lt;li&gt;&lt;b&gt;length&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The total number of items in the hash&lt;/p&gt;&lt;/li&gt;
309      * &lt;/ul&gt;&lt;/div&gt;
310      * @param {Function} fn The function to execute.
311      * @param {Object} scope The scope to execute in. Defaults to &lt;tt&gt;this&lt;/tt&gt;.
312      * @return {Ext.util.HashMap} this
313      */
314     each: function(fn, scope) {
315         // copy items so they may be removed during iteration.
316         var items = Ext.apply({}, this.map),
317             key,
318             length = this.length;
319
320         scope = scope || this;
321         for (key in items) {
322             if (items.hasOwnProperty(key)) {
323                 if (fn.call(scope, key, items[key], length) === false) {
324                     break;
325                 }
326             }
327         }
328         return this;
329     },
330
331 <span id='Ext-util-HashMap-method-clone'>    /**
332 </span>     * Performs a shallow copy on this hash.
333      * @return {Ext.util.HashMap} The new hash object.
334      */
335     clone: function() {
336         var hash = new this.self(),
337             map = this.map,
338             key;
339
340         hash.suspendEvents();
341         for (key in map) {
342             if (map.hasOwnProperty(key)) {
343                 hash.add(key, map[key]);
344             }
345         }
346         hash.resumeEvents();
347         return hash;
348     },
349
350 <span id='Ext-util-HashMap-method-findKey'>    /**
351 </span>     * @private
352      * Find the key for a value.
353      * @param {Object} value The value to find.
354      * @return {Object} The value of the item. Returns &lt;tt&gt;undefined&lt;/tt&gt; if not found.
355      */
356     findKey: function(value) {
357         var key,
358             map = this.map;
359
360         for (key in map) {
361             if (map.hasOwnProperty(key) &amp;&amp; map[key] === value) {
362                 return key;
363             }
364         }
365         return undefined;
366     }
367 });
368 </pre>
369 </body>
370 </html>