1 dojo.provide("calitha.collections.AbstractCollection"); 2 dojo.require("calitha.collections.ICollection"); 3 dojo.require("calitha.collections.StringBuilder"); 4 dojo.require("calitha.exception.UnsupportedOperationException"); 5 6 dojo.declare("calitha.collections.AbstractCollection", calitha.collections.ICollection, 7 /** @lends calitha.collections.AbstractCollection# */ 8 { 9 /** 10 * @constructs 11 * @extends calitha.collections.ICollection 12 */ 13 constructor: function() 14 { 15 } 16 , 17 add: function(/*Object*/ element) 18 { 19 throw new calitha.exception.UnsupportedOperationException(Error()); 20 } 21 , 22 addAll: function(/**calitha.collections.ICollection*/ collection) 23 { 24 var modified = false; 25 var it = collection.iterator(); 26 while (it.hasNext()) 27 { 28 if (this.add(it.next())) 29 { 30 modified = true; 31 } 32 } 33 return modified; 34 } 35 , 36 clear: function() 37 { 38 var it = this.iterator(); 39 while (it.hasNext()) 40 { 41 it.next(); 42 it.remove(); 43 } 44 } 45 , 46 contains: function(/**Object*/ element) 47 { 48 var it = this.iterator(); 49 while (it.hasNext()) 50 { 51 if (calitha.collections.util.equals(it.next(), element)) 52 return true; 53 } 54 return false; 55 } 56 , 57 containsAll: function(/**calitha.collections.ICollection*/ collection) 58 { 59 var it = collection.iterator(); 60 while (it.hasNext()) 61 { 62 if (!this.contains(it.next())) 63 { 64 return false; 65 } 66 } 67 return true; 68 } 69 , 70 forEach: function(/**Function*/ func, /**Object?*/ scope) 71 { 72 if (scope == null) 73 { 74 scope = dojo.global; 75 } 76 var it = this.iterator(); 77 while (it.hasNext()) 78 { 79 func.call(scope, it.next()); 80 } 81 } 82 , 83 isEmpty: function() 84 { 85 return this.size() === 0; 86 } 87 , 88 remove: function(/**Object*/ element) 89 { 90 var it = this.iterator(); 91 while (it.hasNext()) 92 { 93 if (calitha.collections.util.equals(it.next(), element)) 94 { 95 it.remove(); 96 return true; 97 } 98 } 99 return false; 100 } 101 , 102 removeAll: function(/**calitha.collections.ICollection*/ collection) 103 { 104 var modified = false; 105 106 if (this.size() > collection.size()) 107 { 108 var collectionIterator = collection.iterator(); 109 while (collectionIterator.hasNext()) 110 { 111 var objInCollection = collectionIterator.next(); 112 modified |= this.remove(objInCollection); 113 } 114 } 115 else 116 { 117 var thisIterator = this.iterator(); 118 while (thisIterator.hasNext()) 119 { 120 var objInThis = thisIterator.next(); 121 if (collection.contains(objInThis)) 122 { 123 thisIterator.remove(); 124 modified = true; 125 } 126 } 127 } 128 return modified; 129 } 130 , 131 retainAll: function(/**calitha.collections.ICollection*/ collection) 132 { 133 var modified = false; 134 var it = this.iterator(); 135 while (it.hasNext()) 136 { 137 if (!collection.contains(it.next())) 138 { 139 it.remove(); 140 modified = true; 141 } 142 } 143 return modified; 144 } 145 , 146 toArray: function() 147 { 148 var result = new Array(this.size()); 149 var it = this.iterator(); 150 for (var i = 0; it.hasNext(); i++) 151 { 152 result[i] = it.next(); 153 } 154 return result; 155 } 156 , 157 toString: function() 158 { 159 var buf = new calitha.collections.StringBuilder(); 160 buf.append("["); 161 var it = this.iterator(); 162 while (it.hasNext()) 163 { 164 var o = it.next(); 165 buf.append(o === this ? "(this Collection)" : "" + o); 166 if (it.hasNext()) 167 buf.append(", "); 168 } 169 buf.append("]"); 170 return buf.toString(); 171 } 172 }); 173