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.

282 lines (281 loc) 9.48 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 map_1 = require("./map"); var unsupportedoperationexception_1 = require("../lang/unsupportedoperationexception"); var abstractset_1 = require("./abstractset"); var abstractcollection_1 = require("./abstractcollection"); var illegalargumentexception_1 = require("../lang/illegalargumentexception"); var AbstractMap = /** @class */ (function () { function AbstractMap() { } AbstractMap.prototype.size = function () { return this.entrySet().size(); }; AbstractMap.prototype.isEmpty = function () { return this.size() === 0; }; AbstractMap.prototype.containsKey = function (key) { var itr = this.entrySet().iterator(); if (key === null) { while (itr.hasNext()) { var e = itr.next(); if (e.getKey() === null) { return true; } } } else { while (itr.hasNext()) { var e = itr.next(); var keyAny = key; if (keyAny.equals(e.getKey())) { return true; } } } return false; }; AbstractMap.prototype.containsValue = function (value) { var itr = this.entrySet().iterator(); if (value === null) { while (itr.hasNext()) { var e = itr.next(); if (e.getValue() === null) { return true; } } } else { while (itr.hasNext()) { var e = itr.next(); var valueAny = value; if (valueAny.equals(e.getValue())) { return true; } } } return false; }; AbstractMap.prototype.get = function (key) { var itr = this.entrySet().iterator(); if (key === null) { while (itr.hasNext()) { var e = itr.next(); if (e.getKey() === null) { return e.getValue(); } } } else { while (itr.hasNext()) { var e = itr.next(); var keyAny = e.getKey(); if (keyAny.equals(key)) { return e.getValue(); } } } return null; }; AbstractMap.prototype.put = function (key, v) { throw new unsupportedoperationexception_1.UnsupportedOperationException(); }; AbstractMap.prototype.remove = function (key) { var itr = this.entrySet().iterator(); var correctEntry = null; if (key === null) { while (correctEntry === null && itr.hasNext()) { var e = itr.next(); if (e.getKey() === null) { correctEntry = e; } } } else { while (correctEntry === null && itr.hasNext()) { var e = itr.next(); var keyAny = key; if (keyAny.equals(e.getKey())) { correctEntry = e; } } } var oldValue = null; if (correctEntry !== null) { oldValue = correctEntry.getValue(); itr.remove(); } return oldValue; }; AbstractMap.prototype.putAll = function (m) { var itr = this.entrySet().iterator(); while (itr.hasNext()) { var e = itr.next(); this.put(e.getKey(), e.getValue()); } }; AbstractMap.prototype.clear = function () { this.entrySet().clear(); }; AbstractMap.prototype.keySet = function () { return new MapKeySet(this); }; AbstractMap.prototype.values = function () { return new MapValueCollection(this); }; return AbstractMap; }()); exports.AbstractMap = AbstractMap; var MapKeySet = /** @class */ (function (_super) { __extends(MapKeySet, _super); function MapKeySet(map) { var _this = _super.call(this) || this; _this.map = map; return _this; } MapKeySet.prototype.iterator = function () { return new MapKeySetIterator(this.map.entrySet().iterator()); }; MapKeySet.prototype.size = function () { return this.map.size(); }; MapKeySet.prototype.isEmpty = function () { return this.map.isEmpty(); }; MapKeySet.prototype.clear = function () { this.map.clear(); }; MapKeySet.prototype.contains = function (key) { return this.map.containsKey(key); }; return MapKeySet; }(abstractset_1.AbstractSet)); var MapKeySetIterator = /** @class */ (function () { function MapKeySetIterator(entrySetIterator) { this.entrySetIterator = entrySetIterator; } MapKeySetIterator.prototype.hasNext = function () { return this.entrySetIterator.hasNext(); }; MapKeySetIterator.prototype.next = function () { return this.entrySetIterator.next().getKey(); }; MapKeySetIterator.prototype.remove = function () { return this.entrySetIterator.remove(); }; return MapKeySetIterator; }()); var MapValueCollection = /** @class */ (function (_super) { __extends(MapValueCollection, _super); function MapValueCollection(map) { var _this = _super.call(this) || this; _this.map = map; return _this; } MapValueCollection.prototype.size = function () { return this.map.size(); }; MapValueCollection.prototype.isEmpty = function () { return this.map.isEmpty(); }; MapValueCollection.prototype.clear = function () { this.map.clear(); }; MapValueCollection.prototype.contains = function (value) { return this.map.containsValue(value); }; MapValueCollection.prototype.iterator = function () { return new MapValueCollectionIterator(this.map.entrySet().iterator()); }; return MapValueCollection; }(abstractcollection_1.AbstractCollection)); var MapValueCollectionIterator = /** @class */ (function () { function MapValueCollectionIterator(entrySetIterator) { this.entrySetIterator = entrySetIterator; } MapValueCollectionIterator.prototype.hasNext = function () { return this.entrySetIterator.hasNext(); }; MapValueCollectionIterator.prototype.next = function () { return this.entrySetIterator.next().getValue(); }; MapValueCollectionIterator.prototype.remove = function () { this.entrySetIterator.remove(); }; return MapValueCollectionIterator; }()); var ImmutableMapEntry = /** @class */ (function (_super) { __extends(ImmutableMapEntry, _super); function ImmutableMapEntry(keyOrMapEntry, value) { var _this = _super.call(this) || this; if (map_1.isMapEntry(keyOrMapEntry)) { _this.key = keyOrMapEntry.getKey(); _this.value = keyOrMapEntry.getValue(); } else { _this.key = keyOrMapEntry; _this.value = value; } return _this; } ImmutableMapEntry.prototype.getKey = function () { return this.key; }; ImmutableMapEntry.prototype.getValue = function () { return this.value; }; ImmutableMapEntry.prototype.setValue = function (value) { throw new unsupportedoperationexception_1.UnsupportedOperationException(); }; ImmutableMapEntry.prototype.equals = function (obj) { throw new unsupportedoperationexception_1.UnsupportedOperationException(); }; return ImmutableMapEntry; }(map_1.AbstractMapEntry)); exports.ImmutableMapEntry = ImmutableMapEntry; var MutableMapEntry = /** @class */ (function (_super) { __extends(MutableMapEntry, _super); function MutableMapEntry(keyOrMapEntry, value) { var _this = _super.call(this) || this; if (map_1.isMapEntry(keyOrMapEntry)) { _this.key = keyOrMapEntry.getKey(); _this.value = keyOrMapEntry.getValue(); } else { if (value === undefined || value === null) { throw new illegalargumentexception_1.IllegalArgumentException(); } _this.key = keyOrMapEntry; _this.value = value; } return _this; } MutableMapEntry.prototype.getKey = function () { return this.key; }; MutableMapEntry.prototype.getValue = function () { return this.value; }; MutableMapEntry.prototype.setValue = function (value) { this.value = value; return value; }; MutableMapEntry.prototype.equals = function (obj) { return true; }; return MutableMapEntry; }(map_1.AbstractMapEntry)); exports.MutableMapEntry = MutableMapEntry;