1 dojo.provide("calitha.collections.Dictionary");
  2 dojo.require("calitha.collections.AbstractMap");
  3 dojo.require("calitha.collections.util");
  4 dojo.require("calitha.collections.dictionary.Entry");
  5 dojo.require("calitha.collections.dictionary.EntrySet");
  6 dojo.require("calitha.collections.dictionary.KeySet");
  7 dojo.require("calitha.collections.dictionary.Values");
  8 dojo.require("calitha.collections.dictionary.EntryIterator");
  9 dojo.require("calitha.collections.dictionary.KeyIterator");
 10 dojo.require("calitha.collections.dictionary.ValueIterator");
 11 
 12 dojo.declare("calitha.collections.Dictionary", calitha.collections.AbstractMap,
 13 /** @lends calitha.collections.Dictionary#*/
 14 {
 15     /**
 16      * @constructs
 17      * @extends calitha.collections.AbstractMap
 18      * @description This is a map implementation backed by an object hash literal.
 19      * <p>Only integer numbers and strings
 20      * are allowed as keys in this map. There is no similar Java Class.
 21      */
 22     constructor: function()
 23     {
 24         this._items = {};
 25         this._size = 0;
 26         this._entrySet = null;
 27         this._modCount = 0;
 28     }
 29     ,
 30     clear: function()
 31     {
 32         this._modCount++;
 33         this._items = {};
 34         this._size = 0;
 35     }
 36     ,
 37     containsKey: function(/**Object*/ key)
 38     {
 39         return (key in this._items);
 40     }
 41     ,
 42     containsValue: function(/**Object*/ value)
 43     {
 44         for (var p in this._items)
 45         {
 46             if (calitha.collections.util.equals(value, this._items[p].getValue()))
 47             {
 48                 return this._items[p] != null;
 49             }
 50         }
 51         return false;
 52     }
 53     ,
 54     entrySet: function()
 55     {
 56         if (this._entrySet === null)
 57         {
 58             this._entrySet = new calitha.collections.dictionary.EntrySet(this);
 59         }
 60         return this._entrySet;
 61     }
 62     ,
 63     get: function(/**Object*/ key)
 64     {
 65         var entry = this._getEntry(key);
 66         return entry === null ? null : entry.getValue();
 67     }
 68     ,
 69     isEmpty: function()
 70     {
 71         return this._size === 0;
 72     }
 73     ,
 74     keySet: function()
 75     {
 76         if (this._keySet === null)
 77         {
 78             this._keySet = new calitha.collections.dictionary.KeySet(this);
 79         }
 80         return this._keySet;
 81     }
 82     ,
 83     put: function(/**Object*/ key, /**Object*/ value)
 84     {
 85         this._modCount++;
 86         var oldValue = (key in this._items) ? this._items[key].getValue() : null;
 87         this._items[key] = new calitha.collections.dictionary.Entry(key, value);
 88         this._size++;
 89         return oldValue;
 90     }
 91     ,
 92     putAll: function(/**calitha.collections.IMap*/ map)
 93     {
 94         var it = map.entrySet().iterator();
 95         while (it.hasNext())
 96         {
 97             var entry = it.next();
 98             this.put(entry.getKey(), entry.getValue());
 99         }
100     }
101     ,
102     remove: function(/**Object*/ key)
103     {
104         var entry = this._removeEntryForKey(key);
105         return entry === null ? null : entry.getValue();
106     }
107     ,
108     size: function()
109     {
110         return this._size;
111     }
112     ,
113     values: function()
114     {
115         if (this._values === null)
116         {
117             this._values = new calitha.collections.dictionary.Values(this);
118         }
119         return this._values;
120     }
121     ,
122     _getEntry: function(/**Object*/ key)
123     {
124         if (key in this._items)
125         {
126             return this._items[key];
127         }
128         return null;
129     }
130     ,
131     _removeEntryForKey: function(/**Object*/ key)
132     {
133         this._modCount++;
134         if (key in this._items)
135         {
136             var entry = this._items[key];
137             delete this._items[key];
138             this._size--;
139             return entry;
140         }
141         return null;
142     }
143     ,
144     _newEntryIterator: function()
145     {
146         return new calitha.collections.dictionary.EntryIterator(this);
147     }
148     ,
149     _newKeyIterator: function()
150     {
151         return new calitha.collections.dictionary.KeyIterator(this);
152     }
153     ,
154     _newValueIterator: function()
155     {
156         return new calitha.collections.dictionary.ValueIterator(this);
157     }
158 
159 });
160