bugcore
Version:
bugcore is a JavaScript library that provides a foundational architecture for object oriented JS
193 lines (151 loc) • 5.46 kB
JavaScript
/*
* Copyright (c) 2016 airbug Inc. http://airbug.com
*
* bugcore may be freely distributed under the MIT license.
*/
//-------------------------------------------------------------------------------
// Annotations
//-------------------------------------------------------------------------------
//@Export('ArrayIterator')
//@Require('ArgumentBug')
//@Require('Class')
//@Require('Exception')
//@Require('IIndexValueIterator')
//@Require('Obj')
//@Require('TypeUtil')
//-------------------------------------------------------------------------------
// Context
//-------------------------------------------------------------------------------
require('bugpack').context("*", function(bugpack) {
//-------------------------------------------------------------------------------
// BugPack
//-------------------------------------------------------------------------------
var ArgumentBug = bugpack.require('ArgumentBug');
var Class = bugpack.require('Class');
var Exception = bugpack.require('Exception');
var IIndexValueIterator = bugpack.require('IIndexValueIterator');
var Obj = bugpack.require('Obj');
var TypeUtil = bugpack.require('TypeUtil');
//-------------------------------------------------------------------------------
// Declare Class
//-------------------------------------------------------------------------------
/**
* @class
* @extends {Obj}
* @implements {IIndexValueIterator.<V>}
* @template V
*/
var ArrayIterator = Class.extend(Obj, {
_name: "ArrayIterator",
//-------------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------------
/**
* @constructs
*/
_constructor: function() {
this._super();
//-------------------------------------------------------------------------------
// Private Properties
//-------------------------------------------------------------------------------
/**
* @private
* @type {Array.<V>}
*/
this.array = [];
/**
* @private
* @type {number}
*/
this.index = -1;
},
//-------------------------------------------------------------------------------
// Init Methods
//-------------------------------------------------------------------------------
/**
* @param {Array.<V>} array
* @return {ArrayIterator}
*/
init: function(array) {
var _this = this._super();
if (_this) {
if (TypeUtil.isArray(array)) {
_this.array = array;
} else {
throw new ArgumentBug(ArgumentBug.ILLEGAL, "array", array, "parameter must be an Array");
}
}
return _this;
},
//-------------------------------------------------------------------------------
// Getters and Setters
//-------------------------------------------------------------------------------
/**
* @return {Array.<V>}
*/
getArray: function() {
return this.array;
},
/**
* @return {number}
*/
getIndex: function() {
return this.index;
},
//-------------------------------------------------------------------------------
// IIndexValueIterator Implementation
//-------------------------------------------------------------------------------
/**
* @return {boolean}
*/
hasNext: function() {
return (this.index < (this.array.length - 1));
},
/**
* @return {V}
*/
next: function() {
var index = this.nextIndex();
return this.array[index];
},
/**
* @return {number}
*/
nextIndex: function() {
if (this.hasNext()) {
this.index++;
return this.index;
} else {
throw new Exception("NoSuchElement", {}, "End of iteration reached.");
}
},
/**
* @return {{
* index: number
* value: V
* }}
*/
nextIndexValuePair: function() {
var index = this.nextIndex();
var value = this.array[index];
return {
index: index,
value: value
};
},
/**
* @return {V}
*/
nextValue: function() {
return this.next();
}
});
//-------------------------------------------------------------------------------
// Interfaces
//-------------------------------------------------------------------------------
Class.implement(ArrayIterator, IIndexValueIterator);
//-------------------------------------------------------------------------------
// Exports
//-------------------------------------------------------------------------------
bugpack.export('ArrayIterator', ArrayIterator);
});