Rename ol.Array to ol.Collection

This commit is contained in:
Tom Payne
2012-07-30 09:14:53 +02:00
parent f39a09807b
commit a5b0fb4908
5 changed files with 272 additions and 272 deletions

View File

@@ -1,215 +0,0 @@
goog.require('goog.array');
goog.require('goog.testing.jsunit');
goog.require('ol.Array');
goog.require('ol.ArrayEventType');
function testEmpty() {
var a = new ol.Array();
assertEquals(0, a.getLength());
assertTrue(goog.array.equals(a.getArray(), []));
assertUndefined(a.getAt(0));
}
function testConstruct() {
var array = [0, 1, 2];
var a = new ol.Array(array);
assertEquals(0, a.getAt(0));
assertEquals(1, a.getAt(1));
assertEquals(2, a.getAt(2));
}
function testPush() {
var a = new ol.Array();
a.push(1);
assertEquals(1, a.getLength());
assertTrue(goog.array.equals(a.getArray(), [1]));
assertEquals(1, a.getAt(0));
}
function testPushPop() {
var a = new ol.Array();
a.push(1);
a.pop();
assertEquals(0, a.getLength());
assertTrue(goog.array.equals(a.getArray(), []));
assertUndefined(a.getAt(0));
}
function testInsertAt() {
var a = new ol.Array([0, 2]);
a.insertAt(1, 1);
assertEquals(0, a.getAt(0));
assertEquals(1, a.getAt(1));
assertEquals(2, a.getAt(2));
}
function testSetAt() {
var a = new ol.Array();
a.setAt(1, 1);
assertEquals(2, a.getLength());
assertUndefined(a.getAt(0));
assertEquals(1, a.getAt(1));
}
function testRemoveAt() {
var a = new ol.Array([0, 1, 2]);
a.removeAt(1);
assertEquals(0, a.getAt(0));
assertEquals(2, a.getAt(1));
}
function testForEachEmpty() {
var a = new ol.Array();
var forEachCalled = false;
a.forEach(function() {
forEachCalled = true;
});
assertFalse(forEachCalled);
}
function testForEachPopulated() {
var a = new ol.Array();
a.push(1);
a.push(2);
var forEachCount = 0;
a.forEach(function() {
++forEachCount;
});
assertEquals(2, forEachCount);
}
function testSetAtEvent() {
var a = new ol.Array(['a', 'b']);
var index, prev;
goog.events.listen(a, ol.ArrayEventType.SET_AT, function(e) {
index = e.index;
prev = e.prev;
});
a.setAt(1, 1);
assertEquals(1, index);
assertEquals('b', prev);
}
function testRemoveAtEvent() {
var a = new ol.Array(['a']);
var index, prev;
goog.events.listen(a, ol.ArrayEventType.REMOVE_AT, function(e) {
index = e.index;
prev = e.prev;
});
a.pop();
assertEquals(0, index);
assertEquals('a', prev);
}
function testInsertAtEvent() {
var a = new ol.Array([0, 2]);
var index;
goog.events.listen(a, ol.ArrayEventType.INSERT_AT, function(e) {
index = e.index;
});
a.insertAt(1, 1);
assertEquals(1, index);
}
function testSetAtBeyondEnd() {
var a = new ol.Array();
var inserts = [];
a.insert_at = function(index) {
inserts.push(index);
};
a.setAt(2, 0);
assertEquals(3, a.getLength());
assertUndefined(a.getAt(0));
assertUndefined(a.getAt(1));
assertEquals(0, a.getAt(2));
assertEquals(3, inserts.length);
assertEquals(0, inserts[0]);
assertEquals(1, inserts[1]);
assertEquals(2, inserts[2]);
}
function testCreateFromArray() {
var a = [0, 1, 2];
var array = ol.Array.create(a);
assertTrue(array instanceof ol.Array);
assertEquals(3, array.getLength());
assertEquals(0, array.getAt(0));
assertEquals(1, array.getAt(1));
assertEquals(2, array.getAt(2));
}
function testCreateFromArray() {
var array1 = new ol.Array();
var array2 = ol.Array.create(array1);
assertTrue(array1 === array2);
}
function testLengthChangeInsertAt() {
var array = ol.Array.create([0, 1, 2]);
var lengthChangedCalled;
array.length_changed = function() {
lengthChangedCalled = true;
};
array.insertAt(2, 3);
assertTrue(lengthChangedCalled);
}
function testLengthChangeRemoveAt() {
var array = ol.Array.create([0, 1, 2]);
var lengthChangedCalled;
array.length_changed = function() {
lengthChangedCalled = true;
};
array.removeAt(0);
assertTrue(lengthChangedCalled);
}
function testLengthChangeSetAt() {
var array = ol.Array.create([0, 1, 2]);
var lengthChangedCalled;
array.length_changed = function() {
lengthChangedCalled = true;
};
array.setAt(1, 1);
assertUndefined(lengthChangedCalled);
}
function testForEach() {
var array = ol.Array.create([1, 2, 4]);
var sum = 0;
array.forEach(function(elem) {
sum += elem;
});
assertEquals(7, sum);
}
function testForEachScope() {
var array = ol.Array.create([0]);
var that;
var uniqueObj = {};
array.forEach(function(elem) {
that = this;
}, uniqueObj);
assertTrue(that === uniqueObj);
}

View File

@@ -4,9 +4,9 @@
* @see https://developers.google.com/maps/documentation/javascript/reference
*/
goog.provide('ol.Array');
goog.provide('ol.ArrayEvent');
goog.provide('ol.ArrayEventType');
goog.provide('ol.Collection');
goog.provide('ol.CollectionEvent');
goog.provide('ol.CollectionEventType');
goog.require('goog.array');
goog.require('goog.asserts');
@@ -17,7 +17,7 @@ goog.require('ol.Object');
/**
* @enum {string}
*/
ol.ArrayEventType = {
ol.CollectionEventType = {
INSERT_AT: 'insert_at',
REMOVE_AT: 'remove_at',
SET_AT: 'set_at'
@@ -28,12 +28,12 @@ ol.ArrayEventType = {
/**
* @constructor
* @extends {goog.events.Event}
* @param {ol.ArrayEventType} type Type.
* @param {ol.CollectionEventType} type Type.
* @param {number} index Index.
* @param {*=} opt_prev Value.
* @param {Object=} opt_target Target.
*/
ol.ArrayEvent = function(type, index, opt_prev, opt_target) {
ol.CollectionEvent = function(type, index, opt_prev, opt_target) {
goog.base(this, type, opt_target);
@@ -48,13 +48,13 @@ ol.ArrayEvent = function(type, index, opt_prev, opt_target) {
this.prev = opt_prev;
};
goog.inherits(ol.ArrayEvent, goog.events.Event);
goog.inherits(ol.CollectionEvent, goog.events.Event);
/**
* @enum {string}
*/
ol.ArrayProperty = {
ol.CollectionProperty = {
LENGTH: 'length'
};
@@ -65,7 +65,7 @@ ol.ArrayProperty = {
* @extends {ol.Object}
* @param {Array=} opt_array Array.
*/
ol.Array = function(opt_array) {
ol.Collection = function(opt_array) {
goog.base(this);
@@ -78,26 +78,26 @@ ol.Array = function(opt_array) {
this.updateLength_();
};
goog.inherits(ol.Array, ol.Object);
goog.inherits(ol.Collection, ol.Object);
/**
* @param {ol.Array|Array} arg Argument.
* @return {ol.Array} Array.
* @param {ol.Collection|Array} arg Argument.
* @return {ol.Collection} Collection.
*/
ol.Array.create = function(arg) {
if (arg instanceof ol.Array) {
ol.Collection.create = function(arg) {
if (arg instanceof ol.Collection) {
return arg;
} else {
return new ol.Array(arg);
return new ol.Collection(arg);
}
};
/**
*/
ol.Array.prototype.clear = function() {
while (this[ol.ArrayProperty.LENGTH]) {
ol.Collection.prototype.clear = function() {
while (this[ol.CollectionProperty.LENGTH]) {
this.pop();
}
};
@@ -108,7 +108,7 @@ ol.Array.prototype.clear = function() {
* @param {T=} opt_obj The object to be used for the value of 'this' within f.
* @template T
*/
ol.Array.prototype.forEach = function(f, opt_obj) {
ol.Collection.prototype.forEach = function(f, opt_obj) {
goog.array.forEach(this.array_, f, opt_obj);
};
@@ -116,7 +116,7 @@ ol.Array.prototype.forEach = function(f, opt_obj) {
/**
* @return {Array} Array.
*/
ol.Array.prototype.getArray = function() {
ol.Collection.prototype.getArray = function() {
return this.array_;
};
@@ -125,7 +125,7 @@ ol.Array.prototype.getArray = function() {
* @param {number} index Index.
* @return {*} Element.
*/
ol.Array.prototype.getAt = function(index) {
ol.Collection.prototype.getAt = function(index) {
return this.array_[index];
};
@@ -133,8 +133,8 @@ ol.Array.prototype.getAt = function(index) {
/**
* @return {number} Length.
*/
ol.Array.prototype.getLength = function() {
return /** @type {number} */ this.get(ol.ArrayProperty.LENGTH);
ol.Collection.prototype.getLength = function() {
return /** @type {number} */ this.get(ol.CollectionProperty.LENGTH);
};
@@ -142,13 +142,13 @@ ol.Array.prototype.getLength = function() {
* @param {number} index Index.
* @param {*} elem Element.
*/
ol.Array.prototype.insertAt = function(index, elem) {
ol.Collection.prototype.insertAt = function(index, elem) {
goog.array.insertAt(this.array_, elem, index);
this.updateLength_();
this.dispatchEvent(new ol.ArrayEvent(
ol.ArrayEventType.INSERT_AT, index, undefined, this));
if (this[ol.ArrayEventType.INSERT_AT]) {
this[ol.ArrayEventType.INSERT_AT](index);
this.dispatchEvent(new ol.CollectionEvent(
ol.CollectionEventType.INSERT_AT, index, undefined, this));
if (this[ol.CollectionEventType.INSERT_AT]) {
this[ol.CollectionEventType.INSERT_AT](index);
}
};
@@ -156,7 +156,7 @@ ol.Array.prototype.insertAt = function(index, elem) {
/**
* @return {*} Element.
*/
ol.Array.prototype.pop = function() {
ol.Collection.prototype.pop = function() {
return this.removeAt(this.getLength() - 1);
};
@@ -165,7 +165,7 @@ ol.Array.prototype.pop = function() {
* @param {*} elem Element.
* @return {number} Length.
*/
ol.Array.prototype.push = function(elem) {
ol.Collection.prototype.push = function(elem) {
var n = this.array_.length;
this.insertAt(n, elem);
return n;
@@ -176,14 +176,14 @@ ol.Array.prototype.push = function(elem) {
* @param {number} index Index.
* @return {*} Value.
*/
ol.Array.prototype.removeAt = function(index) {
ol.Collection.prototype.removeAt = function(index) {
var prev = this.array_[index];
goog.array.removeAt(this.array_, index);
this.updateLength_();
this.dispatchEvent(new ol.ArrayEvent(ol.ArrayEventType.REMOVE_AT,
this.dispatchEvent(new ol.CollectionEvent(ol.CollectionEventType.REMOVE_AT,
index, prev, this));
if (this[ol.ArrayEventType.REMOVE_AT]) {
this[ol.ArrayEventType.REMOVE_AT](index);
if (this[ol.CollectionEventType.REMOVE_AT]) {
this[ol.CollectionEventType.REMOVE_AT](index);
}
return prev;
};
@@ -193,15 +193,15 @@ ol.Array.prototype.removeAt = function(index) {
* @param {number} index Index.
* @param {*} elem Element.
*/
ol.Array.prototype.setAt = function(index, elem) {
var n = this[ol.ArrayProperty.LENGTH];
ol.Collection.prototype.setAt = function(index, elem) {
var n = this[ol.CollectionProperty.LENGTH];
if (index < n) {
var prev = this.array_[index];
this.array_[index] = elem;
this.dispatchEvent(new ol.ArrayEvent(ol.ArrayEventType.SET_AT,
this.dispatchEvent(new ol.CollectionEvent(ol.CollectionEventType.SET_AT,
index, prev, this));
if (this[ol.ArrayEventType.SET_AT]) {
this[ol.ArrayEventType.SET_AT](index, prev);
if (this[ol.CollectionEventType.SET_AT]) {
this[ol.CollectionEventType.SET_AT](index, prev);
}
} else {
var j;
@@ -216,6 +216,6 @@ ol.Array.prototype.setAt = function(index, elem) {
/**
* @private
*/
ol.Array.prototype.updateLength_ = function() {
ol.Collection.prototype.updateLength_ = function() {
this.set('length', this.array_.length);
};

View File

@@ -0,0 +1,215 @@
goog.require('goog.array');
goog.require('goog.testing.jsunit');
goog.require('ol.Collection');
goog.require('ol.CollectionEventType');
function testEmpty() {
var collection = new ol.Collection();
assertEquals(0, collection.getLength());
assertTrue(goog.array.equals(collection.getArray(), []));
assertUndefined(collection.getAt(0));
}
function testConstruct() {
var array = [0, 1, 2];
var collection = new ol.Collection(array);
assertEquals(0, collection.getAt(0));
assertEquals(1, collection.getAt(1));
assertEquals(2, collection.getAt(2));
}
function testPush() {
var collection = new ol.Collection();
collection.push(1);
assertEquals(1, collection.getLength());
assertTrue(goog.array.equals(collection.getArray(), [1]));
assertEquals(1, collection.getAt(0));
}
function testPushPop() {
var collection = new ol.Collection();
collection.push(1);
collection.pop();
assertEquals(0, collection.getLength());
assertTrue(goog.array.equals(collection.getArray(), []));
assertUndefined(collection.getAt(0));
}
function testInsertAt() {
var collection = new ol.Collection([0, 2]);
collection.insertAt(1, 1);
assertEquals(0, collection.getAt(0));
assertEquals(1, collection.getAt(1));
assertEquals(2, collection.getAt(2));
}
function testSetAt() {
var collection = new ol.Collection();
collection.setAt(1, 1);
assertEquals(2, collection.getLength());
assertUndefined(collection.getAt(0));
assertEquals(1, collection.getAt(1));
}
function testRemoveAt() {
var collection = new ol.Collection([0, 1, 2]);
collection.removeAt(1);
assertEquals(0, collection.getAt(0));
assertEquals(2, collection.getAt(1));
}
function testForEachEmpty() {
var collection = new ol.Collection();
var forEachCalled = false;
collection.forEach(function() {
forEachCalled = true;
});
assertFalse(forEachCalled);
}
function testForEachPopulated() {
var collection = new ol.Collection();
collection.push(1);
collection.push(2);
var forEachCount = 0;
collection.forEach(function() {
++forEachCount;
});
assertEquals(2, forEachCount);
}
function testSetAtEvent() {
var collection = new ol.Collection(['a', 'b']);
var index, prev;
goog.events.listen(collection, ol.CollectionEventType.SET_AT, function(e) {
index = e.index;
prev = e.prev;
});
collection.setAt(1, 1);
assertEquals(1, index);
assertEquals('b', prev);
}
function testRemoveAtEvent() {
var collection = new ol.Collection(['a']);
var index, prev;
goog.events.listen(collection, ol.CollectionEventType.REMOVE_AT, function(e) {
index = e.index;
prev = e.prev;
});
collection.pop();
assertEquals(0, index);
assertEquals('a', prev);
}
function testInsertAtEvent() {
var collection = new ol.Collection([0, 2]);
var index;
goog.events.listen(collection, ol.CollectionEventType.INSERT_AT, function(e) {
index = e.index;
});
collection.insertAt(1, 1);
assertEquals(1, index);
}
function testSetAtBeyondEnd() {
var collection = new ol.Collection();
var inserts = [];
collection.insert_at = function(index) {
inserts.push(index);
};
collection.setAt(2, 0);
assertEquals(3, collection.getLength());
assertUndefined(collection.getAt(0));
assertUndefined(collection.getAt(1));
assertEquals(0, collection.getAt(2));
assertEquals(3, inserts.length);
assertEquals(0, inserts[0]);
assertEquals(1, inserts[1]);
assertEquals(2, inserts[2]);
}
function testCreateFromArray() {
var array = [0, 1, 2];
var collection = ol.Collection.create(array);
assertTrue(collection instanceof ol.Collection);
assertEquals(3, collection.getLength());
assertEquals(0, collection.getAt(0));
assertEquals(1, collection.getAt(1));
assertEquals(2, collection.getAt(2));
}
function testCreateFromCollection() {
var collection1 = new ol.Collection();
var collection2 = ol.Collection.create(collection1);
assertTrue(collection1 === collection2);
}
function testLengthChangeInsertAt() {
var collection = ol.Collection.create([0, 1, 2]);
var lengthChangedCalled;
collection.length_changed = function() {
lengthChangedCalled = true;
};
collection.insertAt(2, 3);
assertTrue(lengthChangedCalled);
}
function testLengthChangeRemoveAt() {
var collection = ol.Collection.create([0, 1, 2]);
var lengthChangedCalled;
collection.length_changed = function() {
lengthChangedCalled = true;
};
collection.removeAt(0);
assertTrue(lengthChangedCalled);
}
function testLengthChangeSetAt() {
var collection = ol.Collection.create([0, 1, 2]);
var lengthChangedCalled;
collection.length_changed = function() {
lengthChangedCalled = true;
};
collection.setAt(1, 1);
assertUndefined(lengthChangedCalled);
}
function testForEach() {
var collection = ol.Collection.create([1, 2, 4]);
var sum = 0;
collection.forEach(function(elem) {
sum += elem;
});
assertEquals(7, sum);
}
function testForEachScope() {
var collection = ol.Collection.create([0]);
var that;
var uniqueObj = {};
collection.forEach(function(elem) {
that = this;
}, uniqueObj);
assertTrue(that === uniqueObj);
}

View File

@@ -2,7 +2,7 @@ goog.provide('ol.RendererHint');
goog.provide('ol.createMap');
goog.require('goog.object');
goog.require('ol.Array');
goog.require('ol.Collection');
goog.require('ol.Map');
goog.require('ol.MapProperty');
goog.require('ol.Projection');
@@ -76,7 +76,7 @@ ol.createMap = function(target, opt_values, opt_rendererHints) {
}
if (!goog.object.containsKey(values, ol.MapProperty.CONTROLS)) {
var controls = new ol.Array();
var controls = new ol.Collection();
controls.push(new ol.control.DblClickZoom());
controls.push(new ol.control.DragPan());
controls.push(new ol.control.KeyboardPan());
@@ -88,7 +88,7 @@ ol.createMap = function(target, opt_values, opt_rendererHints) {
}
if (!goog.object.containsKey(values, ol.MapProperty.LAYERS)) {
values[ol.MapProperty.LAYERS] = new ol.Array();
values[ol.MapProperty.LAYERS] = new ol.Collection();
}
if (!goog.object.containsKey(values, ol.MapProperty.PROJECTION)) {

View File

@@ -28,7 +28,7 @@ goog.require('goog.fx.anim');
goog.require('goog.fx.anim.Animated');
goog.require('goog.object');
goog.require('goog.vec.Mat4');
goog.require('ol.Array');
goog.require('ol.Collection');
goog.require('ol.Color');
goog.require('ol.Control');
goog.require('ol.Coordinate');
@@ -332,10 +332,10 @@ ol.Map.prototype.getCenter = function() {
/**
* @return {ol.Array} Controls.
* @return {ol.Collection} Controls.
*/
ol.Map.prototype.getControls = function() {
return /** @type {ol.Array} */ this.get(ol.MapProperty.CONTROLS);
return /** @type {ol.Collection} */ this.get(ol.MapProperty.CONTROLS);
};
@@ -388,10 +388,10 @@ ol.Map.prototype.getLayerRenderer = function(layer) {
/**
* @return {ol.Array} Layers.
* @return {ol.Collection} Layers.
*/
ol.Map.prototype.getLayers = function() {
return /** @type {ol.Array} */ (this.get(ol.MapProperty.LAYERS));
return /** @type {ol.Collection} */ (this.get(ol.MapProperty.LAYERS));
};
@@ -589,18 +589,18 @@ ol.Map.prototype.handleLayerRemove = function(layer) {
/**
* @param {ol.ArrayEvent} event Event.
* @param {ol.CollectionEvent} event Event.
* @protected
*/
ol.Map.prototype.handleLayersInsertAt = function(event) {
var layers = /** @type {ol.Array} */ event.target;
var layers = /** @type {ol.Collection} */ event.target;
var layer = /** @type {ol.Layer} */ layers.getAt(event.index);
this.handleLayerAdd(layer);
};
/**
* @param {ol.ArrayEvent} event Event.
* @param {ol.CollectionEvent} event Event.
* @protected
*/
ol.Map.prototype.handleLayersRemoveAt = function(event) {
@@ -610,13 +610,13 @@ ol.Map.prototype.handleLayersRemoveAt = function(event) {
/**
* @param {ol.ArrayEvent} event Event.
* @param {ol.CollectionEvent} event Event.
* @protected
*/
ol.Map.prototype.handleLayersSetAt = function(event) {
var prevLayer = /** @type {ol.Layer} */ event.prev;
this.handleLayerRemove(prevLayer);
var layers = /** @type {ol.Array} */ event.target;
var layers = /** @type {ol.Collection} */ event.target;
var layer = /** @type {ol.Layer} */ layers.getAt(event.index);
this.handleLayerAdd(layer);
};
@@ -641,11 +641,11 @@ ol.Map.prototype.handleLayersChanged = function() {
this.setLayerRenderer(layer, layerRenderer);
}, this);
this.layersListenerKeys_ = [
goog.events.listen(layers, ol.ArrayEventType.INSERT_AT,
goog.events.listen(layers, ol.CollectionEventType.INSERT_AT,
this.handleLayersInsertAt, false, this),
goog.events.listen(layers, ol.ArrayEventType.REMOVE_AT,
goog.events.listen(layers, ol.CollectionEventType.REMOVE_AT,
this.handleLayersRemoveAt, false, this),
goog.events.listen(layers, ol.ArrayEventType.SET_AT,
goog.events.listen(layers, ol.CollectionEventType.SET_AT,
this.handleLayersSetAt, false, this)
];
}
@@ -795,7 +795,7 @@ ol.Map.prototype.setCenter = function(center) {
/**
* @param {ol.Array} controls Controls.
* @param {ol.Collection} controls Controls.
*/
ol.Map.prototype.setControls = function(controls) {
this.set(ol.MapProperty.CONTROLS, controls);
@@ -815,7 +815,7 @@ ol.Map.prototype.setLayerRenderer = function(layer, layerRenderer) {
/**
* @param {ol.Array} layers Layers.
* @param {ol.Collection} layers Layers.
*/
ol.Map.prototype.setLayers = function(layers) {
this.set(ol.MapProperty.LAYERS, layers);