Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Caesura.System.Collections = {};
- Caesura.System.Collections.List = function (args) {
- var self = Caesura.System.class(args, "Caesura.System.Collections.List");
- var public = self.public;
- self.addArgs = function (col) {
- var newcol = [];
- if (col === undefined || col.collection === undefined) {
- return newcol;
- }
- if (Caesura.System.isArray(col.collection)) {
- for (var i = 0; i < col.collection.length; i++) {
- newcol.push(col.collection[i]);
- }
- }
- if (col.collection.getIterator !== undefined) {
- var it = col.collection.getIterator();
- while (it.peek()) {
- newcol.push(it.next());
- }
- }
- return newcol;
- }
- self.array = self.addArgs(args);
- self.readOnly = self.readOnly || false;
- public.readOnly = function () {
- self.readOnly = true;
- }
- public.isReadOnly = function () {
- return self.readOnly;
- }
- public.get = function (index) {
- self.indexValidator(index);
- return self.array[index];
- }
- public.set = function (index, item) {
- self.indexValidator(index);
- if (index >= self.array.length) {
- throw Caesura.System.IndexOutOfRangeException({
- message: "Index was out of range when attempting to access the collection.",
- stackCutoff: 4
- });
- }
- self.array[index] = item;
- }
- public.count = function () {
- return self.array.length;
- }
- public.getIterator = function () {
- return (function (collection) {
- var iterator = {};
- var position = 0;
- iterator.move = function () {
- position++;
- }
- iterator.peek = function () {
- return position < collection.count();
- }
- iterator.next = function () {
- if (iterator.peek()) {
- var item = collection.get(position);
- iterator.move();
- return item;
- }
- throw Caesura.System.IndexOutOfRangeException({
- message: "Iterator index has gone out of range of the collection.",
- stackCutoff: 4
- });;
- }
- iterator.reset = function () {
- position = 0;
- }
- iterator.getPosition = function () {
- return position;
- }
- return iterator;
- })(public);
- }
- public.find = function (predicate) {
- if (typeof predicate !== 'function') {
- throw Caesura.System.ArgumentException({
- message: "Argument 'predicate' must be a function.",
- stackCutoff: 4
- });
- }
- for (var i = 0; i < self.array.length; i++) {
- if (predicate(self.array[i])) {
- return self.array[i];
- }
- }
- return undefined;
- }
- public.indexOf = function (item) {
- for (var i = 0; i < self.array.length; i++) {
- if (item.equals !== undefined && item.equals(self.array[i])) {
- return i;
- }
- if (self.array[i] === item) {
- return i;
- }
- }
- return -1;
- }
- public.add = function (item) {
- self.array.push(item);
- }
- public.remove = function (item) {
- var index = public.indexOf(item);
- if (index === -1) {
- return false;
- }
- self.array.splice(index, 1);
- return true;
- }
- public.removeAt = function (index) {
- self.indexValidator(index);
- self.array.splice(index, 1);
- }
- public.insert = function (item, index) {
- index = index || 0;
- self.array.splice(index, 0, item);
- }
- public.contains = function (item) {
- var inquery = public.find(function (x) {
- return ((item === x) || (item.equals !== undefined && item.equals(x)));
- });
- if (inquery !== undefined && inquery !== null) {
- return true;
- }
- return false;
- }
- public.combine = function (col) {
- var arr = self.addArgs({collection: col});
- for (var i = 0; i < arr.length; i++) {
- public.add(arr[i]);
- }
- }
- public.map = function (callback) {
- if (typeof callback !== 'function') {
- throw Caesura.System.ArgumentException({
- message: "Argument 'callback' must be a function.",
- stackCutoff: 4
- });
- }
- for (var i = 0; i < self.array.length; i++) {
- callback(self.array[i], i);
- }
- }
- public.toArray = function () {
- return self.array.slice(0);
- }
- self.indexValidator = function (index) {
- if ((typeof index !== 'number') ||
- (index % 1 !== 0) ||
- (index < 0)
- ) {
- throw Caesura.System.ArgumentException({
- message: "Argument must be a positive integer.",
- stackCutoff: 5
- });
- }
- if (index >= self.array.length) {
- throw Caesura.System.IndexOutOfRangeException({
- message: "Index was out of range when attempting to access the collection.",
- stackCutoff: 5
- });
- }
- }
- // overrides:
- public.toString = function (formatting) {
- formatting = formatting || {};
- var str = "[ ";
- for (var i = 0; i < self.array.length; i++) {
- if (formatting.toString === true) {
- str += self.array[i].toString();
- } else {
- str += self.array[i];
- }
- str += (i === self.array.length - 1 ? " " : ", ");
- }
- str += "]";
- return str;
- }
- public.getHashCode = function () {
- return Caesura.System.generateHash(public.toString({toString: true}));
- }
- public.equals = function (other) {
- return (public.getHashCode() === other.getHashCode());
- }
- return public;
- }
- Caesura.System.Collections.ObservableList = function (args) {
- var self = Caesura.System.class(args, "Caesura.System.Collections.ObservableList").extends(Caesura.System.Collections.List);
- var public = self.public;
- var base = self.base;
- self.onGetCallbacks = Caesura.System.Collections.List();
- self.onSetCallbacks = Caesura.System.Collections.List();
- self.onInsertCallbacks = Caesura.System.Collections.List();
- self.onAddCallbacks = Caesura.System.Collections.List();
- self.onRemoveCallbacks = Caesura.System.Collections.List();
- public.onGet = function (callback) {
- self.onGetCallbacks.add(callback);
- }
- public.removeOnGet = function (callback) {
- return self.onGetCallbacks.remove(callback);
- }
- public.onSet = function (callback) {
- self.onSetCallbacks.add(callback);
- }
- public.removeOnSet = function (callback) {
- return self.onSetCallbacks.remove(callback);
- }
- public.onInsert = function (callback) {
- self.onInsertCallbacks.add(callback);
- }
- public.removeOnInsert = function (callback) {
- return self.onInsertCallbacks.remove(callback);
- }
- public.onAdd = function (callback) {
- self.onAddCallbacks.add(callback);
- }
- public.removeOnAdd = function (callback) {
- return self.onAddCallbacks.remove(callback);
- }
- public.onRemove = function (callback) {
- self.onRemoveCallbacks.add(callback);
- }
- public.removeOnRemove = function (callback) {
- return self.onRemoveCallbacks.remove(callback);
- }
- public.get = function (index, args) {
- var item = base.public.get(index);
- args = args || { triggerEvent: true }
- if (args.triggerEvent) {
- self.onGetCallbacks.map(function (callback) {
- if (typeof callback === 'function') {
- callback(item, index);
- }
- });
- }
- return item;
- }
- public.set = function (index, item) {
- base.public.set(index, item);
- self.onSetCallbacks.map(function (callback) {
- if (typeof callback === 'function') {
- callback(item, index);
- }
- });
- }
- public.insert = function (item, index) {
- base.public.insert(index, item);
- self.onInsertCallbacks.map(function (callback) {
- if (typeof callback === 'function') {
- callback(item, index);
- }
- });
- }
- public.add = function (item) {
- base.public.add(item);
- self.onAddCallbacks.map(function (callback) {
- if (typeof callback === 'function') {
- callback(item, public.count());
- }
- });
- }
- public.remove = function (item) {
- var result = base.public.remove(item);
- if (!result) {
- return false;
- }
- self.onRemoveCallbacks.map(function (callback) {
- if (typeof callback === 'function') {
- callback(item, public.count());
- }
- });
- return result;
- }
- public.removeAt = function (index) {
- var item = public.get(index, { triggerEvent: false });
- base.public.removeAt(index);
- self.onRemoveCallbacks.map(function (callback) {
- if (typeof callback === 'function') {
- callback(item, index);
- }
- });
- }
- return public;
- }
- Caesura.System.Collections.Stack = function (args) {
- var self = Caesura.System.class(args, "Caesura.System.Collections.Stack").extends(Caesura.System.Collections.List);
- var public = self.public;
- var base = self.base;
- public.push = function (item) {
- public.add(item);
- }
- public.pop = function () {
- if (public.count() <= 0) {
- throw Caesura.System.CollectionException({
- message: "The stack is empty.",
- stackCutoff: 4
- });
- }
- var index = public.count() - 1;
- var item = public.get(index);
- public.removeAt(index);
- return item;
- }
- return public;
- }
- Caesura.System.Collections.Queue = function (args) {
- var self = Caesura.System.class(args, "Caesura.System.Collections.Queue").extends(Caesura.System.Collections.List);
- var public = self.public;
- var base = self.base;
- public.enqueue = function (item) {
- public.add(item);
- }
- public.dequeue = function () {
- if (public.count() <= 0) {
- throw Caesura.System.CollectionException({
- message: "The queue is empty.",
- stackCutoff: 4
- });
- }
- var item = public.get(0);
- public.removeAt(0);
- return item;
- }
- return public;
- }
- Caesura.System.Collections.Set = function (args) {
- var self = Caesura.System.class(args, "Caesura.System.Collections.Set").extends(Caesura.System.Collections.List);
- var public = self.public;
- var base = self.base;
- public.set = function (index, item) {
- self.validate(item);
- base.public.set(index, item);
- }
- public.add = function (item) {
- self.validate(item);
- base.public.add(item);
- }
- public.insert = function (item, index) {
- self.validate(item);
- base.public.insert(item, index);
- }
- self.validate = function (item) {
- if (public.contains(item)) {
- throw Caesura.System.CollectionException({
- message: "Duplicate item exists.",
- stackCutoff: 4
- });
- }
- }
- return public;
- }
Add Comment
Please, Sign In to add comment