UNPKG

ts-collection

Version:

This is re-write of the java collection classes in typescript. There is some tweak as typescript templates are not as equivalent as Java.

213 lines (212 loc) 7.61 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var abstractcollection_1 = require("./abstractcollection"); var indexoutofboundsexception_1 = require("./indexoutofboundsexception"); var nosuchelementexception_1 = require("./nosuchelementexception"); var illegalstateexception_1 = require("../lang/illegalstateexception"); var concurrentmodificationexception_1 = require("./concurrentmodificationexception"); var AbstractList = /** @class */ (function (_super) { __extends(AbstractList, _super); function AbstractList() { var _this = _super.call(this) || this; _this.modCount = 0; return _this; } AbstractList.prototype.add = function (e) { this.addAt(this.size(), e); return true; }; AbstractList.prototype.indexOf = function (e) { var it = this.listIterator(); while (it.hasNext()) { if (e === it.next()) { return it.previousIndex(); } } return -1; }; AbstractList.prototype.lastIndexOf = function (e) { var it = this.listIteratorFrom(this.size()); while (it.hasPrevious()) { if (it.previous() === e) { return it.nextIndex(); } } return -1; }; AbstractList.prototype.clear = function () { this.removeRange(0, this.size()); }; AbstractList.prototype.addAll = function (c) { return this.addAllFrom(this.size(), c); }; AbstractList.prototype.addAllFrom = function (index, c) { this.rangeCheckForAdd(index); var modified = false; var it = c.iterator(); while (it.hasNext()) { this.addAt(index++, it.next()); modified = true; } return modified; }; AbstractList.prototype.iterator = function () { return new Itr(this); }; AbstractList.prototype.listIterator = function () { return this.listIteratorFrom(0); }; AbstractList.prototype.listIteratorFrom = function (index) { this.rangeCheckForAdd(index); return new ListItr(index, this); }; AbstractList.prototype.removeRange = function (fromIndex, toIndex) { var it = this.listIteratorFrom(fromIndex); for (var i = 0, n = toIndex - fromIndex; i < n; i++) { it.next(); it.remove(); } }; AbstractList.prototype.rangeCheckForAdd = function (index) { if (index < 0 || index > this.size()) { throw new indexoutofboundsexception_1.IndexOutOfBoundsException(this.indexOutOfBoundsMsg(index)); } }; AbstractList.prototype.indexOutOfBoundsMsg = function (index) { return 'Index: ' + index + ", Size: " + this.size(); }; AbstractList.prototype.rangeCheck = function (index) { if (index < 0 || index >= this.size()) { throw new indexoutofboundsexception_1.IndexOutOfBoundsException(this.indexOutOfBoundsMsg(index)); } }; return AbstractList; }(abstractcollection_1.AbstractCollection)); exports.AbstractList = AbstractList; var Itr = /** @class */ (function () { function Itr(iteratorOf) { this.iteratorOf = iteratorOf; this.cursor = 0; this.lastRet = -1; this.expectedModCount = iteratorOf.modCount; } Itr.prototype.hasNext = function () { return this.cursor !== this.iteratorOf.size(); }; Itr.prototype.next = function () { this.checkForModification(); try { var i = this.cursor; var next = this.iteratorOf.get(i); this.lastRet = i; this.cursor = i + 1; return next; } catch (ex) { if (ex instanceof indexoutofboundsexception_1.IndexOutOfBoundsException) { this.checkForModification(); throw new nosuchelementexception_1.NoSuchElementException(); } } }; Itr.prototype.remove = function () { if (this.lastRet < 0) { throw new illegalstateexception_1.IllegalStateException(); } this.checkForModification(); try { this.iteratorOf.removeAt(this.lastRet); if (this.lastRet < this.cursor) { this.cursor--; } this.expectedModCount = this.iteratorOf.modCount; } catch (ex) { if (ex instanceof indexoutofboundsexception_1.IndexOutOfBoundsException) { throw new concurrentmodificationexception_1.ConcurrentModificationException(); } } }; Itr.prototype.checkForModification = function () { if (this.expectedModCount != this.iteratorOf.modCount) { throw new concurrentmodificationexception_1.ConcurrentModificationException(); } }; return Itr; }()); var ListItr = /** @class */ (function (_super) { __extends(ListItr, _super); function ListItr(index, iteratorOf) { var _this = _super.call(this, iteratorOf) || this; _this.iteratorOf = iteratorOf; _this.cursor = index; return _this; } ListItr.prototype.hasPrevious = function () { return this.cursor !== 0; }; ListItr.prototype.previous = function () { this.checkForModification(); try { var i = this.cursor - 1; var previous = this.iteratorOf.get(i); this.lastRet = this.cursor = i; return previous; } catch (ex) { if (ex instanceof indexoutofboundsexception_1.IndexOutOfBoundsException) { this.checkForModification(); throw new nosuchelementexception_1.NoSuchElementException(); } } }; ListItr.prototype.nextIndex = function () { return this.cursor; }; ListItr.prototype.previousIndex = function () { return this.cursor - 1; }; ListItr.prototype.set = function (e) { if (this.lastRet < 0) { throw new illegalstateexception_1.IllegalStateException(); } this.checkForModification(); try { this.iteratorOf.setAt(this.lastRet, e); this.expectedModCount = this.iteratorOf.modCount; } catch (ex) { if (ex instanceof indexoutofboundsexception_1.IndexOutOfBoundsException) { throw new concurrentmodificationexception_1.ConcurrentModificationException(); } } }; ListItr.prototype.add = function (e) { this.checkForModification(); try { var i = this.cursor; this.iteratorOf.addAt(i, e); this.lastRet = -1; this.cursor = i + 1; } catch (ex) { if (ex instanceof indexoutofboundsexception_1.IndexOutOfBoundsException) { throw new concurrentmodificationexception_1.ConcurrentModificationException(); } } }; return ListItr; }(Itr));