1 dojo.provide("calitha.collections._base");
  2 dojo.require("calitha.collections.base.ReverseComparator");
  3 dojo.require("calitha.collections.base.ReverseNaturalComparator");
  4 dojo.require("calitha.collections.base.NumberComparator");
  5 dojo.require("calitha.collections.base.StringComparator");
  6 
  7 /**
  8  * @name calitha
  9  * @namespace
 10  */
 11 
 12 /**
 13  * @name calitha.collections
 14  * @namespace
 15  * @version 1.1
 16  * @description The collections namespace consists of static methods that supports the use of the collections framework.
 17  * The methods operate or return collections or comparators. Probably the most important methods are those
 18  * that make integer-based numbers and strings compatible as keys in hash-based collections.
 19  */
 20 
 21 calitha.collections._NUMBER_COMPARATOR = null;
 22 calitha.collections._REVERSE_NATURAL_COMPARATOR = null;
 23 calitha.collections._STRING_COMPARATOR = null;
 24 calitha.collections._CASE_INSENSITIVE_STRING_COMPARATOR = null;
 25 calitha.collections._OBJECT_AS_STRING_COMPARATOR = null;
 26 calitha.collections._OBJECT_AS_CASE_INSENSITIVE_STRING_COMPARATOR = null;
 27 
 28 /**
 29  * @function
 30  * @param cmp comparator optional comparator
 31  * @returns {calitha.collections.IComparator} comparator with reverse ordering
 32  * @description Returns a comparator that imposes reverse ordering. If a comparator is used, then the ordering
 33  * is reversed on that comparator. Otherwise the elements must be IComparable and the natural ordering is reversed.
 34  */
 35 calitha.collections.reverseOrder = function(/**calitha.collections.IComparator?*/ cmp)
 36 {
 37     if (cmp == null)
 38     {
 39         return calitha.collections.reverseNaturalOrder();
 40     }
 41     return calitha.collections.base.ReverseComparator(cmp);
 42 };
 43 
 44 /**
 45  * @function
 46  * @returns a comparator that imposes the reverse of the natural ordering
 47  * @description Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that
 48  * implement the IComparable interface.
 49  */
 50 calitha.collections.reverseNaturalOrder = function()
 51 {
 52     if (calitha.collections._REVERSE_NATURAL_COMPARATOR === null)
 53     {
 54         calitha.collections._REVERSE_NATURAL_COMPARATOR = new calitha.collections.base.ReverseNaturalComparator();
 55     }
 56     return calitha.collections._REVERSE_NATURAL_COMPARATOR;
 57 };
 58 
 59 /**
 60  * @function
 61  * @param number number to make compatible
 62  * @description Makes a number with integer value compatible with hash-based collections. This is done by
 63  * extending the object to include an equals and hashCode value. You can pass the Number.prototype to make all existing
 64  * and new numbers compatible.
 65  */
 66 calitha.collections.makeNumberHashCompatible = function(/**Number*/ number)
 67 {
 68     if (number.equals == null)
 69     {
 70         number.equals = function(obj)
 71         {
 72             return obj === this;
 73         };
 74     }
 75     if (number.hashCode == null)
 76     {
 77         number.hashCode = function()
 78         {
 79             return this;
 80         };
 81     }
 82 };
 83 
 84 /**
 85  * @function
 86  * @param str string to make compatible
 87  * @description Makes a string compativle with hash-based collections. This is done by extending the object to
 88  * include an equals and hashCode value. You can pass String.prototype to make all existing and new strings compatible.
 89  */
 90 calitha.collections.makeStringHashCompatible = function(/**String*/ str)
 91 {
 92     if (str.equals == null)
 93     {
 94         str.equals = function(obj)
 95         {
 96             return obj === this;
 97         };
 98     }
 99     if (str.hashCode == null)
100     {
101         str.hashCode = function()
102         {
103             if (this._hash == null)
104             {
105                 var result = 0;
106                 for (var i = 0; i < this.length; i++)
107                 {
108                     result = 31 * result + this.charCodeAt(i);
109                 }
110                 this._hash = result;
111             }
112             return this._hash;
113         };
114     }
115 };
116 
117 /**
118  * @function
119  * @returns {calitha.collections.IComparator} comparator
120  * @description returns a comparator to compare numbers. The comparator can be used for tree-based collections.
121  */
122 calitha.collections.numberComparator = function()
123 {
124     if (calitha.collections._NUMBER_COMPARATOR === null)
125     {
126         calitha.collections._NUMBER_COMPARATOR = new calitha.collections.base.NumberComparator();
127     }
128     return calitha.collections._NUMBER_COMPARATOR;
129 };
130 
131 /**
132  * @function
133  * @returns {calitha.collections.IComparator} comparator
134  * @description returns a comparator to compare string (only objects of type string).
135  * The comparator can be used for tree-based collections.
136  */
137 calitha.collections.stringComparator = function()
138 {
139     if (calitha.collections._STRING_COMPARATOR === null)
140     {
141         calitha.collections._STRING_COMPARATOR = new calitha.collections.base.StringComparator(false, true);
142     }
143     return calitha.collections._STRING_COMPARATOR;
144 };
145 
146 /**
147  * @function
148  * @returns {calitha.collections.IComparator} comparator
149  * @description returns a comparator to compare string in a case insensitive way (only objects of type string).
150  * The comparator can be used for tree-based collections.
151  */
152 calitha.collections.caseInsenstiveStringComparator = function()
153 {
154     if (calitha.collections._CASE_INSENSITIVE_STRING_COMPARATOR === null)
155     {
156         calitha.collections._CASE_INSENSITIVE_STRING_COMPARATOR = new calitha.collections.base.StringComparator(true, true);
157     }
158     return calitha.collections._CASE_INSENSITIVE_STRING_COMPARATOR;
159 };
160 
161 /**
162  * @function
163  * @returns {calitha.collections.IComparator} comparator
164  * @description returns a comparator to compare any object by getting its string representation.
165  * The comparator can be used for tree-based collections.
166  */
167 calitha.collections.objectAsStringComparator = function()
168 {
169     if (calitha.collections._OBJECT_AS_STRING_COMPARATOR === null)
170     {
171         calitha.collections._OBJECT_AS_STRING_COMPARATOR = new calitha.collections.base.StringComparator(false, false);
172     }
173     return calitha.collections._OBJECT_AS_STRING_COMPARATOR;
174 };
175 
176 /**
177  * @function
178  * @returns {calitha.collections.IComparator} comparator
179  * @description returns a comparator to compare case insensitive any object by getting its string representation.
180  * The comparator can be used for tree-based collections.
181  */
182 calitha.collections.objectAsCaseInsensitiveStringComparator = function()
183 {
184     if (calitha.collections._OBJECT_AS_CASE_INSENSITIVE_STRING_COMPARATOR === null)
185     {
186         calitha.collections._OBJECT_AS_CASE_INSENSITIVE_STRING_COMPARATOR = new calitha.collections.base.StringComparator(false, false);
187     }
188     return calitha.collections._OBJECT_AS_CASE_INSENSITIVE_STRING_COMPARATOR;
189 };
190 
191 /**
192  * @function
193  * @param list list
194  * @param comparator optional comparator
195  * @description Sorts a list by using the comparator object/function. Or if the comparator is not specified, it uses
196  * the natural ordering which means the elements must be IComparable objects.
197  */
198 calitha.collections.sort = function(/**calitha.collections.IList*/ list, /**(calitha.collections.IComparator|Function)?*/ comparator)
199 {
200     var array = list.toArray();
201     calitha.collections.arrays.sort(array, comparator);
202     var it = list.listIterator();
203     for (var index = 0; index < array.length; index++)
204     {
205         it.next();
206         it.set(array[index]);
207     }
208 };
209