Guest User

Untitled

a guest
Nov 12th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.70 KB | None | 0 0
  1. Caesura.System.Collections = {};
  2.  
  3. Caesura.System.Collections.List = function (args) {
  4. var self = Caesura.System.class(args, "Caesura.System.Collections.List");
  5. var public = self.public;
  6.  
  7. self.addArgs = function (col) {
  8. var newcol = [];
  9. if (col === undefined || col.collection === undefined) {
  10. return newcol;
  11. }
  12. if (Caesura.System.isArray(col.collection)) {
  13. for (var i = 0; i < col.collection.length; i++) {
  14. newcol.push(col.collection[i]);
  15. }
  16. }
  17. if (col.collection.getIterator !== undefined) {
  18. var it = col.collection.getIterator();
  19. while (it.peek()) {
  20. newcol.push(it.next());
  21. }
  22. }
  23. return newcol;
  24. }
  25.  
  26. self.array = self.addArgs(args);
  27. self.readOnly = self.readOnly || false;
  28.  
  29. public.readOnly = function () {
  30. self.readOnly = true;
  31. }
  32.  
  33. public.isReadOnly = function () {
  34. return self.readOnly;
  35. }
  36.  
  37. public.get = function (index) {
  38. self.indexValidator(index);
  39. return self.array[index];
  40. }
  41.  
  42. public.set = function (index, item) {
  43. self.indexValidator(index);
  44. if (index >= self.array.length) {
  45. throw Caesura.System.IndexOutOfRangeException({
  46. message: "Index was out of range when attempting to access the collection.",
  47. stackCutoff: 4
  48. });
  49. }
  50. self.array[index] = item;
  51. }
  52.  
  53. public.count = function () {
  54. return self.array.length;
  55. }
  56.  
  57. public.getIterator = function () {
  58. return (function (collection) {
  59. var iterator = {};
  60. var position = 0;
  61. iterator.move = function () {
  62. position++;
  63. }
  64. iterator.peek = function () {
  65. return position < collection.count();
  66. }
  67. iterator.next = function () {
  68. if (iterator.peek()) {
  69. var item = collection.get(position);
  70. iterator.move();
  71. return item;
  72. }
  73. throw Caesura.System.IndexOutOfRangeException({
  74. message: "Iterator index has gone out of range of the collection.",
  75. stackCutoff: 4
  76. });;
  77. }
  78. iterator.reset = function () {
  79. position = 0;
  80. }
  81. iterator.getPosition = function () {
  82. return position;
  83. }
  84. return iterator;
  85. })(public);
  86. }
  87.  
  88. public.find = function (predicate) {
  89. if (typeof predicate !== 'function') {
  90. throw Caesura.System.ArgumentException({
  91. message: "Argument 'predicate' must be a function.",
  92. stackCutoff: 4
  93. });
  94. }
  95. for (var i = 0; i < self.array.length; i++) {
  96. if (predicate(self.array[i])) {
  97. return self.array[i];
  98. }
  99. }
  100. return undefined;
  101. }
  102.  
  103. public.indexOf = function (item) {
  104. for (var i = 0; i < self.array.length; i++) {
  105. if (item.equals !== undefined && item.equals(self.array[i])) {
  106. return i;
  107. }
  108. if (self.array[i] === item) {
  109. return i;
  110. }
  111. }
  112. return -1;
  113. }
  114.  
  115. public.add = function (item) {
  116. self.array.push(item);
  117. }
  118.  
  119. public.remove = function (item) {
  120. var index = public.indexOf(item);
  121. if (index === -1) {
  122. return false;
  123. }
  124. self.array.splice(index, 1);
  125. return true;
  126. }
  127.  
  128. public.removeAt = function (index) {
  129. self.indexValidator(index);
  130. self.array.splice(index, 1);
  131. }
  132.  
  133. public.insert = function (item, index) {
  134. index = index || 0;
  135. self.array.splice(index, 0, item);
  136. }
  137.  
  138. public.contains = function (item) {
  139. var inquery = public.find(function (x) {
  140. return ((item === x) || (item.equals !== undefined && item.equals(x)));
  141. });
  142. if (inquery !== undefined && inquery !== null) {
  143. return true;
  144. }
  145. return false;
  146. }
  147.  
  148. public.combine = function (col) {
  149. var arr = self.addArgs({collection: col});
  150. for (var i = 0; i < arr.length; i++) {
  151. public.add(arr[i]);
  152. }
  153. }
  154.  
  155. public.map = function (callback) {
  156. if (typeof callback !== 'function') {
  157. throw Caesura.System.ArgumentException({
  158. message: "Argument 'callback' must be a function.",
  159. stackCutoff: 4
  160. });
  161. }
  162. for (var i = 0; i < self.array.length; i++) {
  163. callback(self.array[i], i);
  164. }
  165. }
  166.  
  167. public.toArray = function () {
  168. return self.array.slice(0);
  169. }
  170.  
  171. self.indexValidator = function (index) {
  172. if ((typeof index !== 'number') ||
  173. (index % 1 !== 0) ||
  174. (index < 0)
  175. ) {
  176. throw Caesura.System.ArgumentException({
  177. message: "Argument must be a positive integer.",
  178. stackCutoff: 5
  179. });
  180. }
  181. if (index >= self.array.length) {
  182. throw Caesura.System.IndexOutOfRangeException({
  183. message: "Index was out of range when attempting to access the collection.",
  184. stackCutoff: 5
  185. });
  186. }
  187. }
  188.  
  189. // overrides:
  190.  
  191. public.toString = function (formatting) {
  192. formatting = formatting || {};
  193. var str = "[ ";
  194. for (var i = 0; i < self.array.length; i++) {
  195. if (formatting.toString === true) {
  196. str += self.array[i].toString();
  197. } else {
  198. str += self.array[i];
  199. }
  200. str += (i === self.array.length - 1 ? " " : ", ");
  201. }
  202. str += "]";
  203. return str;
  204. }
  205.  
  206. public.getHashCode = function () {
  207. return Caesura.System.generateHash(public.toString({toString: true}));
  208. }
  209.  
  210. public.equals = function (other) {
  211. return (public.getHashCode() === other.getHashCode());
  212. }
  213.  
  214. return public;
  215. }
  216.  
  217. Caesura.System.Collections.ObservableList = function (args) {
  218. var self = Caesura.System.class(args, "Caesura.System.Collections.ObservableList").extends(Caesura.System.Collections.List);
  219. var public = self.public;
  220. var base = self.base;
  221.  
  222. self.onGetCallbacks = Caesura.System.Collections.List();
  223. self.onSetCallbacks = Caesura.System.Collections.List();
  224. self.onInsertCallbacks = Caesura.System.Collections.List();
  225. self.onAddCallbacks = Caesura.System.Collections.List();
  226. self.onRemoveCallbacks = Caesura.System.Collections.List();
  227.  
  228. public.onGet = function (callback) {
  229. self.onGetCallbacks.add(callback);
  230. }
  231.  
  232. public.removeOnGet = function (callback) {
  233. return self.onGetCallbacks.remove(callback);
  234. }
  235.  
  236. public.onSet = function (callback) {
  237. self.onSetCallbacks.add(callback);
  238. }
  239.  
  240. public.removeOnSet = function (callback) {
  241. return self.onSetCallbacks.remove(callback);
  242. }
  243.  
  244. public.onInsert = function (callback) {
  245. self.onInsertCallbacks.add(callback);
  246. }
  247.  
  248. public.removeOnInsert = function (callback) {
  249. return self.onInsertCallbacks.remove(callback);
  250. }
  251.  
  252. public.onAdd = function (callback) {
  253. self.onAddCallbacks.add(callback);
  254. }
  255.  
  256. public.removeOnAdd = function (callback) {
  257. return self.onAddCallbacks.remove(callback);
  258. }
  259.  
  260. public.onRemove = function (callback) {
  261. self.onRemoveCallbacks.add(callback);
  262. }
  263.  
  264. public.removeOnRemove = function (callback) {
  265. return self.onRemoveCallbacks.remove(callback);
  266. }
  267.  
  268. public.get = function (index, args) {
  269. var item = base.public.get(index);
  270. args = args || { triggerEvent: true }
  271. if (args.triggerEvent) {
  272. self.onGetCallbacks.map(function (callback) {
  273. if (typeof callback === 'function') {
  274. callback(item, index);
  275. }
  276. });
  277. }
  278. return item;
  279. }
  280.  
  281. public.set = function (index, item) {
  282. base.public.set(index, item);
  283. self.onSetCallbacks.map(function (callback) {
  284. if (typeof callback === 'function') {
  285. callback(item, index);
  286. }
  287. });
  288. }
  289.  
  290. public.insert = function (item, index) {
  291. base.public.insert(index, item);
  292. self.onInsertCallbacks.map(function (callback) {
  293. if (typeof callback === 'function') {
  294. callback(item, index);
  295. }
  296. });
  297. }
  298.  
  299. public.add = function (item) {
  300. base.public.add(item);
  301. self.onAddCallbacks.map(function (callback) {
  302. if (typeof callback === 'function') {
  303. callback(item, public.count());
  304. }
  305. });
  306. }
  307.  
  308. public.remove = function (item) {
  309. var result = base.public.remove(item);
  310. if (!result) {
  311. return false;
  312. }
  313. self.onRemoveCallbacks.map(function (callback) {
  314. if (typeof callback === 'function') {
  315. callback(item, public.count());
  316. }
  317. });
  318. return result;
  319. }
  320.  
  321. public.removeAt = function (index) {
  322. var item = public.get(index, { triggerEvent: false });
  323. base.public.removeAt(index);
  324. self.onRemoveCallbacks.map(function (callback) {
  325. if (typeof callback === 'function') {
  326. callback(item, index);
  327. }
  328. });
  329. }
  330.  
  331. return public;
  332. }
  333.  
  334. Caesura.System.Collections.Stack = function (args) {
  335. var self = Caesura.System.class(args, "Caesura.System.Collections.Stack").extends(Caesura.System.Collections.List);
  336. var public = self.public;
  337. var base = self.base;
  338.  
  339. public.push = function (item) {
  340. public.add(item);
  341. }
  342.  
  343. public.pop = function () {
  344. if (public.count() <= 0) {
  345. throw Caesura.System.CollectionException({
  346. message: "The stack is empty.",
  347. stackCutoff: 4
  348. });
  349. }
  350. var index = public.count() - 1;
  351. var item = public.get(index);
  352. public.removeAt(index);
  353. return item;
  354. }
  355.  
  356. return public;
  357. }
  358.  
  359. Caesura.System.Collections.Queue = function (args) {
  360. var self = Caesura.System.class(args, "Caesura.System.Collections.Queue").extends(Caesura.System.Collections.List);
  361. var public = self.public;
  362. var base = self.base;
  363.  
  364. public.enqueue = function (item) {
  365. public.add(item);
  366. }
  367.  
  368. public.dequeue = function () {
  369. if (public.count() <= 0) {
  370. throw Caesura.System.CollectionException({
  371. message: "The queue is empty.",
  372. stackCutoff: 4
  373. });
  374. }
  375. var item = public.get(0);
  376. public.removeAt(0);
  377. return item;
  378. }
  379.  
  380. return public;
  381. }
  382.  
  383. Caesura.System.Collections.Set = function (args) {
  384. var self = Caesura.System.class(args, "Caesura.System.Collections.Set").extends(Caesura.System.Collections.List);
  385. var public = self.public;
  386. var base = self.base;
  387.  
  388. public.set = function (index, item) {
  389. self.validate(item);
  390. base.public.set(index, item);
  391. }
  392.  
  393. public.add = function (item) {
  394. self.validate(item);
  395. base.public.add(item);
  396. }
  397.  
  398. public.insert = function (item, index) {
  399. self.validate(item);
  400. base.public.insert(item, index);
  401. }
  402.  
  403. self.validate = function (item) {
  404. if (public.contains(item)) {
  405. throw Caesura.System.CollectionException({
  406. message: "Duplicate item exists.",
  407. stackCutoff: 4
  408. });
  409. }
  410. }
  411.  
  412. return public;
  413. }
Add Comment
Please, Sign In to add comment