Remove goog.asserts.*
This pull requests replaces type check hint assertions with type casts, library sanity check assertions with conditional console.assert statements in debug mode, and runtime sanity checks with assertions that throw an ol.AssertionError with an error code for lookup outside the library.
This commit is contained in:
+56
-54
@@ -1,6 +1,5 @@
|
||||
goog.provide('ol.structs.LRUCache');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.object');
|
||||
|
||||
|
||||
@@ -41,49 +40,51 @@ ol.structs.LRUCache = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* FIXME empty description for jsdoc
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.assertValid = function() {
|
||||
if (this.count_ === 0) {
|
||||
goog.asserts.assert(ol.object.isEmpty(this.entries_),
|
||||
'entries must be an empty object (count = 0)');
|
||||
goog.asserts.assert(!this.oldest_,
|
||||
'oldest must be null (count = 0)');
|
||||
goog.asserts.assert(!this.newest_,
|
||||
'newest must be null (count = 0)');
|
||||
} else {
|
||||
goog.asserts.assert(Object.keys(this.entries_).length == this.count_,
|
||||
'number of entries matches count');
|
||||
goog.asserts.assert(this.oldest_,
|
||||
'we have an oldest entry');
|
||||
goog.asserts.assert(!this.oldest_.older,
|
||||
'no entry is older than oldest');
|
||||
goog.asserts.assert(this.newest_,
|
||||
'we have a newest entry');
|
||||
goog.asserts.assert(!this.newest_.newer,
|
||||
'no entry is newer than newest');
|
||||
var i, entry;
|
||||
var older = null;
|
||||
i = 0;
|
||||
for (entry = this.oldest_; entry; entry = entry.newer) {
|
||||
goog.asserts.assert(entry.older === older,
|
||||
'entry.older links to correct older');
|
||||
older = entry;
|
||||
++i;
|
||||
if (ol.DEBUG) {
|
||||
/**
|
||||
* FIXME empty description for jsdoc
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.assertValid = function() {
|
||||
if (this.count_ === 0) {
|
||||
console.assert(ol.object.isEmpty(this.entries_),
|
||||
'entries must be an empty object (count = 0)');
|
||||
console.assert(!this.oldest_,
|
||||
'oldest must be null (count = 0)');
|
||||
console.assert(!this.newest_,
|
||||
'newest must be null (count = 0)');
|
||||
} else {
|
||||
console.assert(Object.keys(this.entries_).length == this.count_,
|
||||
'number of entries matches count');
|
||||
console.assert(this.oldest_,
|
||||
'we have an oldest entry');
|
||||
console.assert(!this.oldest_.older,
|
||||
'no entry is older than oldest');
|
||||
console.assert(this.newest_,
|
||||
'we have a newest entry');
|
||||
console.assert(!this.newest_.newer,
|
||||
'no entry is newer than newest');
|
||||
var i, entry;
|
||||
var older = null;
|
||||
i = 0;
|
||||
for (entry = this.oldest_; entry; entry = entry.newer) {
|
||||
console.assert(entry.older === older,
|
||||
'entry.older links to correct older');
|
||||
older = entry;
|
||||
++i;
|
||||
}
|
||||
console.assert(i == this.count_, 'iterated correct amount of times');
|
||||
var newer = null;
|
||||
i = 0;
|
||||
for (entry = this.newest_; entry; entry = entry.older) {
|
||||
console.assert(entry.newer === newer,
|
||||
'entry.newer links to correct newer');
|
||||
newer = entry;
|
||||
++i;
|
||||
}
|
||||
console.assert(i == this.count_, 'iterated correct amount of times');
|
||||
}
|
||||
goog.asserts.assert(i == this.count_, 'iterated correct amount of times');
|
||||
var newer = null;
|
||||
i = 0;
|
||||
for (entry = this.newest_; entry; entry = entry.older) {
|
||||
goog.asserts.assert(entry.newer === newer,
|
||||
'entry.newer links to correct newer');
|
||||
newer = entry;
|
||||
++i;
|
||||
}
|
||||
goog.asserts.assert(i == this.count_, 'iterated correct amount of times');
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -129,7 +130,8 @@ ol.structs.LRUCache.prototype.forEach = function(f, opt_this) {
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.get = function(key) {
|
||||
var entry = this.entries_[key];
|
||||
goog.asserts.assert(entry !== undefined, 'an entry exists for key %s', key);
|
||||
ol.assert(entry !== undefined,
|
||||
15); // Tried to get a value for a key that does not exist in the cache
|
||||
if (entry === this.newest_) {
|
||||
return entry.value_;
|
||||
} else if (entry === this.oldest_) {
|
||||
@@ -165,7 +167,7 @@ ol.structs.LRUCache.prototype.getKeys = function() {
|
||||
for (entry = this.newest_; entry; entry = entry.older) {
|
||||
keys[i++] = entry.key_;
|
||||
}
|
||||
goog.asserts.assert(i == this.count_, 'iterated correct number of times');
|
||||
ol.DEBUG && console.assert(i == this.count_, 'iterated correct number of times');
|
||||
return keys;
|
||||
};
|
||||
|
||||
@@ -180,7 +182,7 @@ ol.structs.LRUCache.prototype.getValues = function() {
|
||||
for (entry = this.newest_; entry; entry = entry.older) {
|
||||
values[i++] = entry.value_;
|
||||
}
|
||||
goog.asserts.assert(i == this.count_, 'iterated correct number of times');
|
||||
ol.DEBUG && console.assert(i == this.count_, 'iterated correct number of times');
|
||||
return values;
|
||||
};
|
||||
|
||||
@@ -189,7 +191,7 @@ ol.structs.LRUCache.prototype.getValues = function() {
|
||||
* @return {T} Last value.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.peekLast = function() {
|
||||
goog.asserts.assert(this.oldest_, 'oldest must not be null');
|
||||
ol.DEBUG && console.assert(this.oldest_, 'oldest must not be null');
|
||||
return this.oldest_.value_;
|
||||
};
|
||||
|
||||
@@ -198,7 +200,7 @@ ol.structs.LRUCache.prototype.peekLast = function() {
|
||||
* @return {string} Last key.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.peekLastKey = function() {
|
||||
goog.asserts.assert(this.oldest_, 'oldest must not be null');
|
||||
ol.DEBUG && console.assert(this.oldest_, 'oldest must not be null');
|
||||
return this.oldest_.key_;
|
||||
};
|
||||
|
||||
@@ -207,10 +209,10 @@ ol.structs.LRUCache.prototype.peekLastKey = function() {
|
||||
* @return {T} value Value.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.pop = function() {
|
||||
goog.asserts.assert(this.oldest_, 'oldest must not be null');
|
||||
goog.asserts.assert(this.newest_, 'newest must not be null');
|
||||
ol.DEBUG && console.assert(this.oldest_, 'oldest must not be null');
|
||||
ol.DEBUG && console.assert(this.newest_, 'newest must not be null');
|
||||
var entry = this.oldest_;
|
||||
goog.asserts.assert(entry.key_ in this.entries_,
|
||||
ol.DEBUG && console.assert(entry.key_ in this.entries_,
|
||||
'oldest is indexed in entries');
|
||||
delete this.entries_[entry.key_];
|
||||
if (entry.newer) {
|
||||
@@ -240,10 +242,10 @@ ol.structs.LRUCache.prototype.replace = function(key, value) {
|
||||
* @param {T} value Value.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.set = function(key, value) {
|
||||
goog.asserts.assert(!(key in {}),
|
||||
ol.DEBUG && console.assert(!(key in {}),
|
||||
'key is not a standard property of objects (e.g. "__proto__")');
|
||||
goog.asserts.assert(!(key in this.entries_),
|
||||
'key is not used already');
|
||||
ol.assert(!(key in this.entries_),
|
||||
16); // Tried to set a value for a key that is used already
|
||||
var entry = /** @type {ol.LRUCacheEntry} */ ({
|
||||
key_: key,
|
||||
newer: null,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
goog.provide('ol.structs.PriorityQueue');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.object');
|
||||
|
||||
|
||||
@@ -61,25 +60,27 @@ ol.structs.PriorityQueue = function(priorityFunction, keyFunction) {
|
||||
ol.structs.PriorityQueue.DROP = Infinity;
|
||||
|
||||
|
||||
/**
|
||||
* FIXME empty description for jsdoc
|
||||
*/
|
||||
ol.structs.PriorityQueue.prototype.assertValid = function() {
|
||||
var elements = this.elements_;
|
||||
var priorities = this.priorities_;
|
||||
var n = elements.length;
|
||||
goog.asserts.assert(priorities.length == n);
|
||||
var i, priority;
|
||||
for (i = 0; i < (n >> 1) - 1; ++i) {
|
||||
priority = priorities[i];
|
||||
goog.asserts.assert(priority <= priorities[this.getLeftChildIndex_(i)],
|
||||
'priority smaller than or equal to priority of left child (%s <= %s)',
|
||||
priority, priorities[this.getLeftChildIndex_(i)]);
|
||||
goog.asserts.assert(priority <= priorities[this.getRightChildIndex_(i)],
|
||||
'priority smaller than or equal to priority of right child (%s <= %s)',
|
||||
priority, priorities[this.getRightChildIndex_(i)]);
|
||||
}
|
||||
};
|
||||
if (ol.DEBUG) {
|
||||
/**
|
||||
* FIXME empty description for jsdoc
|
||||
*/
|
||||
ol.structs.PriorityQueue.prototype.assertValid = function() {
|
||||
var elements = this.elements_;
|
||||
var priorities = this.priorities_;
|
||||
var n = elements.length;
|
||||
console.assert(priorities.length == n);
|
||||
var i, priority;
|
||||
for (i = 0; i < (n >> 1) - 1; ++i) {
|
||||
priority = priorities[i];
|
||||
console.assert(priority <= priorities[this.getLeftChildIndex_(i)],
|
||||
'priority smaller than or equal to priority of left child (%s <= %s)',
|
||||
priority, priorities[this.getLeftChildIndex_(i)]);
|
||||
console.assert(priority <= priorities[this.getRightChildIndex_(i)],
|
||||
'priority smaller than or equal to priority of right child (%s <= %s)',
|
||||
priority, priorities[this.getRightChildIndex_(i)]);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -98,7 +99,7 @@ ol.structs.PriorityQueue.prototype.clear = function() {
|
||||
*/
|
||||
ol.structs.PriorityQueue.prototype.dequeue = function() {
|
||||
var elements = this.elements_;
|
||||
goog.asserts.assert(elements.length > 0,
|
||||
ol.DEBUG && console.assert(elements.length > 0,
|
||||
'must have elements in order to be able to dequeue');
|
||||
var priorities = this.priorities_;
|
||||
var element = elements[0];
|
||||
@@ -111,7 +112,7 @@ ol.structs.PriorityQueue.prototype.dequeue = function() {
|
||||
this.siftUp_(0);
|
||||
}
|
||||
var elementKey = this.keyFunction_(element);
|
||||
goog.asserts.assert(elementKey in this.queuedElements_,
|
||||
ol.DEBUG && console.assert(elementKey in this.queuedElements_,
|
||||
'key %s is not listed as queued', elementKey);
|
||||
delete this.queuedElements_[elementKey];
|
||||
return element;
|
||||
@@ -124,8 +125,8 @@ ol.structs.PriorityQueue.prototype.dequeue = function() {
|
||||
* @return {boolean} The element was added to the queue.
|
||||
*/
|
||||
ol.structs.PriorityQueue.prototype.enqueue = function(element) {
|
||||
goog.asserts.assert(!(this.keyFunction_(element) in this.queuedElements_),
|
||||
'key %s is already listed as queued', this.keyFunction_(element));
|
||||
ol.assert(!(this.keyFunction_(element) in this.queuedElements_),
|
||||
31); // Tried to enqueue an `element` that was already added to the queue
|
||||
var priority = this.priorityFunction_(element);
|
||||
if (priority != ol.structs.PriorityQueue.DROP) {
|
||||
this.elements_.push(element);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
goog.provide('ol.structs.RBush');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.ext.rbush');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.object');
|
||||
@@ -60,7 +59,7 @@ ol.structs.RBush.prototype.insert = function(extent, value) {
|
||||
|
||||
this.rbush_.insert(item);
|
||||
// remember the object that was added to the internal rbush
|
||||
goog.asserts.assert(!(ol.getUid(value) in this.items_),
|
||||
ol.DEBUG && console.assert(!(ol.getUid(value) in this.items_),
|
||||
'uid (%s) of value (%s) already exists', ol.getUid(value), value);
|
||||
this.items_[ol.getUid(value)] = item;
|
||||
};
|
||||
@@ -75,7 +74,7 @@ ol.structs.RBush.prototype.load = function(extents, values) {
|
||||
if (goog.DEBUG && this.readers_) {
|
||||
throw new Error('Can not insert values while reading');
|
||||
}
|
||||
goog.asserts.assert(extents.length === values.length,
|
||||
ol.DEBUG && console.assert(extents.length === values.length,
|
||||
'extens and values must have same length (%s === %s)',
|
||||
extents.length, values.length);
|
||||
|
||||
@@ -93,7 +92,7 @@ ol.structs.RBush.prototype.load = function(extents, values) {
|
||||
value: value
|
||||
};
|
||||
items[i] = item;
|
||||
goog.asserts.assert(!(ol.getUid(value) in this.items_),
|
||||
ol.DEBUG && console.assert(!(ol.getUid(value) in this.items_),
|
||||
'uid (%s) of value (%s) already exists', ol.getUid(value), value);
|
||||
this.items_[ol.getUid(value)] = item;
|
||||
}
|
||||
@@ -111,7 +110,7 @@ ol.structs.RBush.prototype.remove = function(value) {
|
||||
throw new Error('Can not remove value while reading');
|
||||
}
|
||||
var uid = ol.getUid(value);
|
||||
goog.asserts.assert(uid in this.items_,
|
||||
ol.DEBUG && console.assert(uid in this.items_,
|
||||
'uid (%s) of value (%s) does not exist', uid, value);
|
||||
|
||||
// get the object in which the value was wrapped when adding to the
|
||||
@@ -128,11 +127,10 @@ ol.structs.RBush.prototype.remove = function(value) {
|
||||
* @param {T} value Value.
|
||||
*/
|
||||
ol.structs.RBush.prototype.update = function(extent, value) {
|
||||
var uid = ol.getUid(value);
|
||||
goog.asserts.assert(uid in this.items_,
|
||||
'uid (%s) of value (%s) does not exist', uid, value);
|
||||
ol.DEBUG && console.assert(ol.getUid(value) in this.items_,
|
||||
'uid (%s) of value (%s) does not exist', ol.getUid(value), value);
|
||||
|
||||
var item = this.items_[uid];
|
||||
var item = this.items_[ol.getUid(value)];
|
||||
var bbox = [item.minX, item.minY, item.maxX, item.maxY];
|
||||
if (!ol.extent.equals(bbox, extent)) {
|
||||
if (goog.DEBUG && this.readers_) {
|
||||
|
||||
Reference in New Issue
Block a user