1 dojo.provide("calitha.collections.SubList"); 2 dojo.require("calitha.collections.AbstractList"); 3 dojo.require("calitha.collections.sublist.ListIterator"); 4 dojo.require("calitha.exception.IndexOutOfBoundsException"); 5 6 dojo.declare("calitha.collections.SubList", calitha.collections.AbstractList, 7 /** @lends calitha.collections.SubList# */ 8 { 9 /** 10 * @constructs 11 * @param list list 12 * @param fromIndex from index 13 * @param toIndex to index (not inclusive) 14 * @extends calitha.collections.AbstractList 15 * @description a view on a part of a list 16 */ 17 constructor: function(/**calitha.collections.IList*/ list, /**Number*/ fromIndex, /**Number*/ toIndex) 18 { 19 if (fromIndex < 0) 20 { 21 throw new calitha.exception.IndexOutOfBoundsException(Error(), fromIndex, list.size()); 22 } 23 if (toIndex > list.size()) 24 { 25 throw new calitha.exception.IndexOutOfBoundsException(Error(), toIndex, list.size()); 26 } 27 this._list = list; 28 this._offset = fromIndex; 29 this._size = toIndex - fromIndex; 30 } 31 , 32 addAll: function(/**calitha.collections.ICollection*/ collection) 33 { 34 return this.insertAll(this._size, collection); 35 } 36 , 37 get: function(/**Number*/ index) 38 { 39 this._rangeCheck(index); 40 return this._list.get(index + this._offset); 41 } 42 , 43 insert: function(/**Number*/ index, /**Object*/ element) 44 { 45 if (index < 0 || index > this._size) 46 { 47 throw new calitha.exception.IndexOutOfBoundsException(Error(), index, this._size); 48 } 49 this._list.insert(index + this._offset, element); 50 this._size++; 51 } 52 , 53 insertAll: function(/**Number*/ index, /**calitha.collections.ICollection*/ collection) 54 { 55 if (index < 0 || index > this._size) 56 { 57 throw new calitha.collections.IndexOutOfBoundsException(Error(), index, this._size); 58 } 59 var collectionSize = collection.size(); 60 if (collectionSize === 0) 61 { 62 return false; 63 } 64 65 this._list.insertAll(this._offset + index, collection); 66 this._size += collectionSize; 67 return true; 68 } 69 , 70 iterator: function() 71 { 72 return this.listIterator(); 73 } 74 , 75 listIterator: function(/**Number*/ index) 76 { 77 if (index == null) 78 { 79 index = 0; 80 } 81 if (index < 0 || index > this._size) 82 { 83 throw new calitha.exception.IndexOutOfBoundsException(Error(), index, this._size); 84 } 85 return calitha.collections.sublist.ListIterator(index); 86 } 87 , 88 remove: function(/**Number*/ index) 89 { 90 this._rangeCheck(index); 91 var result = this._list.remove(index + this._offset); 92 this._size--; 93 return result; 94 } 95 , 96 set: function(/**Number*/ index, /**Object*/ element) 97 { 98 this._rangeCheck(index); 99 return this._list.set(index + this._offset, element); 100 } 101 , 102 size: function() 103 { 104 return this._size; 105 } 106 , 107 subList: function(/**Number*/ fromIndex, /**Number*/ toIndex) 108 { 109 return new calitha.collections.SubList(this, fromIndex, toIndex); 110 } 111 , 112 _rangeCheck: function(/**Number*/ index) 113 { 114 if (index < 0 || index >= this._size) 115 { 116 throw new calitha.exception.IndexOutOfBoundsException(Error(), index, this._size); 117 } 118 } 119 , 120 _removeRange: function(/**Number*/ fromIndex, /**Number*/ toIndex) 121 { 122 this._list.removeRange(fromIndex + this._offset, toIndex + this._offset); 123 this._size -= (toIndex - fromIndex); 124 } 125 126 }); 127