LinkedCollection.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. import { StringBuilder, cast_java_lang_StringBuilder } from '../../../java/lang/StringBuilder';
  2. import { IndexOutOfBoundsException, cast_java_lang_IndexOutOfBoundsException } from '../../../java/lang/IndexOutOfBoundsException';
  3. import { JavaString, cast_java_lang_String } from '../../../java/lang/JavaString';
  4. import { Deque, cast_java_util_Deque } from '../../../java/util/Deque';
  5. import { LinkedCollectionElement, cast_de_nrw_schule_svws_core_adt_collection_LinkedCollectionElement } from '../../../core/adt/collection/LinkedCollectionElement';
  6. import { Comparator, cast_java_util_Comparator } from '../../../java/util/Comparator';
  7. import { NullPointerException, cast_java_lang_NullPointerException } from '../../../java/lang/NullPointerException';
  8. import { JavaIterator, cast_java_util_Iterator } from '../../../java/util/JavaIterator';
  9. import { Collection, cast_java_util_Collection } from '../../../java/util/Collection';
  10. import { Cloneable, cast_java_lang_Cloneable } from '../../../java/lang/Cloneable';
  11. import { JavaObject, cast_java_lang_Object } from '../../../java/lang/JavaObject';
  12. import { LinkedCollectionDescendingIterator, cast_de_nrw_schule_svws_core_adt_collection_LinkedCollectionDescendingIterator } from '../../../core/adt/collection/LinkedCollectionDescendingIterator';
  13. import { LinkedCollectionIterator, cast_de_nrw_schule_svws_core_adt_collection_LinkedCollectionIterator } from '../../../core/adt/collection/LinkedCollectionIterator';
  14. import { Arrays, cast_java_util_Arrays } from '../../../java/util/Arrays';
  15. import { NoSuchElementException, cast_java_util_NoSuchElementException } from '../../../java/util/NoSuchElementException';
  16. import { CloneNotSupportedException, cast_java_lang_CloneNotSupportedException } from '../../../java/lang/CloneNotSupportedException';
  17. export class LinkedCollection<E> extends JavaObject implements Deque<E>, Cloneable {
  18. _head : LinkedCollectionElement<E> | null = null;
  19. _tail : LinkedCollectionElement<E> | null = null;
  20. private _size : number = 0;
  21. _modCount : number = 0;
  22. /**
  23. * Erzeugt eine neue LinkedCollection.
  24. */
  25. public constructor();
  26. /**
  27. * Erzeugt eine neue LinkedCollection als Kopie der übergebenen
  28. * LinkedCollection
  29. *
  30. * @param c die LinkedCollection, die kopiert wird
  31. */
  32. public constructor(c : LinkedCollection<E> | null);
  33. /**
  34. * Implementation for method overloads of 'constructor'
  35. */
  36. public constructor(__param0? : LinkedCollection<E> | null) {
  37. super();
  38. if ((typeof __param0 === "undefined")) {
  39. this._head = null;
  40. this._tail = null;
  41. this._size = 0;
  42. this._modCount = 0;
  43. } else if (((typeof __param0 !== "undefined") && ((__param0 instanceof JavaObject) && (__param0.isTranspiledInstanceOf('de.nrw.schule.svws.core.adt.collection.LinkedCollection'))) || (__param0 === null))) {
  44. let c : LinkedCollection<E> | null = cast_de_nrw_schule_svws_core_adt_collection_LinkedCollection(__param0);
  45. this._size = 0;
  46. this._modCount = 0;
  47. let iter : JavaIterator<E> = c.iterator();
  48. while (iter.hasNext())
  49. this.add(iter.next());
  50. this._modCount = c._modCount;
  51. } else throw new Error('invalid method overload');
  52. }
  53. public size() : number {
  54. return this._size;
  55. }
  56. public isEmpty() : boolean {
  57. return (this._head === null) ? true : false;
  58. }
  59. public contains(obj : unknown | null) : boolean {
  60. if (this.isEmpty())
  61. return false;
  62. let iter : JavaIterator<E> = this.iterator();
  63. while (iter.hasNext())
  64. if (JavaObject.equalsTranspiler(iter.next(), (obj)))
  65. return true;
  66. return false;
  67. }
  68. public iterator() : JavaIterator<E> {
  69. return new LinkedCollectionIterator(this);
  70. }
  71. public toArray() : Array<unknown>;
  72. public toArray<T>(a : Array<T>) : Array<T>;
  73. /**
  74. * Implementation for method overloads of 'toArray'
  75. */
  76. public toArray<T>(__param0? : Array<T>) : Array<T> | Array<unknown> {
  77. if ((typeof __param0 === "undefined")) {
  78. if (this._size === 0)
  79. return Array(0).fill(null);
  80. let array : Array<E> = Array(this._size).fill(null);
  81. let iter : JavaIterator<E> = this.iterator();
  82. for (let i : number = 0; i < this._size; i++){
  83. array[i] = iter.next();
  84. }
  85. return array;
  86. } else if (((typeof __param0 !== "undefined") && Array.isArray(__param0))) {
  87. let a : Array<T> = __param0;
  88. if (a.length < this._size)
  89. return this.toArray();
  90. let iter : JavaIterator<E> = this.iterator();
  91. for (let i : number = 0; i < this._size; i++){
  92. let e : E = iter.next();
  93. a[i] = e as unknown as T;
  94. }
  95. Arrays.fill(a, this._size, a.length, null);
  96. return a;
  97. } else throw new Error('invalid method overload');
  98. }
  99. public add(e : E | null) : boolean {
  100. if (e === null)
  101. return false;
  102. let newElem : LinkedCollectionElement<E> = new LinkedCollectionElement(e, null, null);
  103. if ((this._head === null) || (this._tail === null)) {
  104. this._head = newElem;
  105. this._tail = newElem;
  106. this._size++;
  107. this._modCount++;
  108. return true;
  109. }
  110. newElem.setPrev(this._tail);
  111. this._tail.setNext(newElem);
  112. this._tail = newElem;
  113. this._size++;
  114. this._modCount++;
  115. return true;
  116. }
  117. /**
  118. * Entfernt das übergebene Element.
  119. *
  120. * @param elem das zu entfernende Element
  121. *
  122. * @return true, falls das Element erfolgreich entfernt wurde, und false, falls null übergeben wurde.
  123. */
  124. private removeElement(elem : LinkedCollectionElement<E> | null) : boolean {
  125. if (elem === null)
  126. return false;
  127. let prev : LinkedCollectionElement<E> | null = elem.getPrev();
  128. let next : LinkedCollectionElement<E> | null = elem.getNext();
  129. if (this._size === 1) {
  130. this._head = null;
  131. this._tail = null;
  132. } else
  133. if (JavaObject.equalsTranspiler(elem, (this._head))) {
  134. if (next === null)
  135. throw new NullPointerException()
  136. this._head = next;
  137. next.setPrev(null);
  138. } else
  139. if (JavaObject.equalsTranspiler(elem, (this._tail))) {
  140. if (prev === null)
  141. throw new NullPointerException()
  142. this._tail = prev;
  143. prev.setNext(null);
  144. } else {
  145. if ((next === null) || (prev === null))
  146. throw new NullPointerException()
  147. next.setPrev(prev);
  148. prev.setNext(next);
  149. }
  150. this._size--;
  151. this._modCount++;
  152. return true;
  153. }
  154. public remove(obj : unknown | null) : boolean;
  155. public remove() : E;
  156. /**
  157. * Implementation for method overloads of 'remove'
  158. */
  159. public remove(__param0? : null | unknown) : E | boolean {
  160. if (((typeof __param0 !== "undefined") && ((__param0 instanceof Object) || ((__param0 instanceof JavaObject) && (__param0.isTranspiledInstanceOf('java.lang.Object')))) || (__param0 === null))) {
  161. let obj : unknown | null = (__param0 instanceof JavaObject) ? cast_java_lang_Object(__param0) : __param0;
  162. if (this.isEmpty())
  163. return false;
  164. return this.removeElement(this.findFirst(obj));
  165. } else if ((typeof __param0 === "undefined")) {
  166. let value : E | null = this.poll();
  167. if (value === null)
  168. throw new NoSuchElementException()
  169. return value;
  170. } else throw new Error('invalid method overload');
  171. }
  172. public containsAll(c : Collection<unknown> | null) : boolean {
  173. if ((c === null) || (this as unknown === c as unknown))
  174. return true;
  175. for (let o of c)
  176. if (!this.contains(o))
  177. return false;
  178. return true;
  179. }
  180. public addAll(c : Collection<E> | null) : boolean {
  181. if ((c === null) || (c.size() === 0))
  182. return false;
  183. if (((c instanceof JavaObject) && (c.isTranspiledInstanceOf('de.nrw.schule.svws.core.adt.collection.LinkedCollection')))) {
  184. let coll : LinkedCollection<E> = cast_de_nrw_schule_svws_core_adt_collection_LinkedCollection(c);
  185. if ((coll._tail === null) || (coll._head === null))
  186. throw new NullPointerException()
  187. let last : LinkedCollectionElement<E> = coll._tail;
  188. let current : LinkedCollectionElement<E> | null = coll._head;
  189. this.add(current.getValue());
  190. while (current as unknown !== last as unknown) {
  191. current = current.getNext();
  192. if (current === null)
  193. throw new NullPointerException()
  194. this.add(current.getValue());
  195. }
  196. return true;
  197. }
  198. let result : boolean = false;
  199. for (let elem of c) {
  200. if (this.add(elem))
  201. result = true;
  202. }
  203. return result;
  204. }
  205. public removeAll(c : Collection<unknown> | null) : boolean {
  206. if (c === null)
  207. return false;
  208. if (this as unknown === c as unknown) {
  209. if (this.size() === 0)
  210. return false;
  211. this.clear();
  212. return true;
  213. }
  214. let result : boolean = false;
  215. for (let o of c) {
  216. if (this.remove(o)) {
  217. result = true;
  218. while (this.remove(o)) ;
  219. }
  220. }
  221. return result;
  222. }
  223. public retainAll(c : Collection<unknown> | null) : boolean {
  224. if ((this as unknown === c as unknown) || (this._head === null))
  225. return false;
  226. if (c === null) {
  227. this.clear();
  228. return true;
  229. }
  230. let iter : JavaIterator<E> = this.iterator();
  231. let tmp : LinkedCollection<E> = new LinkedCollection();
  232. while (iter.hasNext()) {
  233. let elem : E = iter.next();
  234. if (!c.contains(elem))
  235. tmp.add(elem);
  236. }
  237. if (tmp.isEmpty())
  238. return false;
  239. return this.removeAll(tmp);
  240. }
  241. public clear() : void {
  242. this._head = null;
  243. this._tail = null;
  244. this._size = 0;
  245. this._modCount++;
  246. }
  247. public hashCode() : number {
  248. let hashCode : number = 1;
  249. for (let e of this)
  250. hashCode = 31 * hashCode + (e === null ? 0 : JavaObject.getTranspilerHashCode(e));
  251. return hashCode;
  252. }
  253. public equals(obj : unknown | null) : boolean {
  254. if ((obj === null) || (!(((obj instanceof JavaObject) && (obj.isTranspiledInstanceOf('java.util.Collection'))))))
  255. return false;
  256. let other : Collection<unknown> = cast_java_util_Collection(obj);
  257. if (this._size !== other.size())
  258. return false;
  259. let iter : JavaIterator<E> = this.iterator();
  260. let otherIter : JavaIterator<unknown> = other.iterator();
  261. while (iter.hasNext()) {
  262. if (!JavaObject.equalsTranspiler(iter.next(), (otherIter.next())))
  263. return false;
  264. }
  265. return true;
  266. }
  267. public clone() : unknown {
  268. return new LinkedCollection(this);
  269. }
  270. public toString() : String {
  271. let sb : StringBuilder = new StringBuilder();
  272. sb.append("[");
  273. let iter : JavaIterator<E> = this.iterator();
  274. while (iter.hasNext()) {
  275. sb.append(iter.next());
  276. if (iter.hasNext() === true)
  277. sb.append(", ");
  278. }
  279. sb.append("]");
  280. return sb.toString();
  281. }
  282. /**
  283. * Diese Methode ist eine Hilfsmethode für die Methode sort(). Sie mischt die beiden über die prev-Zeiger
  284. * verketteten Listen left und right zu einer kombinierten, über die prev-Zeiger verketteten Liste.
  285. *
  286. * @param comparator ein {@link Comparator} zum Vergleichen zweier Elemente
  287. * @param left die erste sortierte Liste
  288. * @param right die zweite sortierte Liste
  289. *
  290. * @return die kombinierte sortierte Liste
  291. */
  292. private merge(comparator : Comparator<E>, left : LinkedCollectionElement<E>, right : LinkedCollectionElement<E>) : LinkedCollectionElement<E> {
  293. let headTo : LinkedCollectionElement<E> | null;
  294. let headFrom : LinkedCollectionElement<E> | null;
  295. if (comparator.compare(left.getValue(), right.getValue()) > 0) {
  296. headFrom = left;
  297. headTo = right;
  298. } else {
  299. headFrom = right;
  300. headTo = left;
  301. }
  302. let target : LinkedCollectionElement<E> = headTo;
  303. while (headFrom !== null) {
  304. let current : LinkedCollectionElement<E> = headFrom;
  305. headFrom = headFrom.getPrev();
  306. let targetPrev : LinkedCollectionElement<E> | null = target.getPrev();
  307. while ((targetPrev !== null) && (comparator.compare(targetPrev.getValue(), current.getValue()) < 0)) {
  308. target = targetPrev;
  309. targetPrev = target.getPrev();
  310. }
  311. if (targetPrev === null) {
  312. target.setPrev(current);
  313. break;
  314. }
  315. current.setPrev(targetPrev);
  316. target.setPrev(current);
  317. }
  318. return headTo;
  319. }
  320. /**
  321. * Sortiert den Inhalte dieser Liste mithilfe des übergebenen {@link Comparator}-Objekts.
  322. *
  323. * @param comparator ein {@link Comparator} zum Vergleichen zweier Elemente
  324. *
  325. * @return true, falls eine Sortierung erfolgreich war
  326. */
  327. public sort(comparator : Comparator<E> | null) : boolean {
  328. if (comparator === null)
  329. return false;
  330. if ((this._size <= 1) || (this._head === null) || (this._tail === null))
  331. return true;
  332. this._modCount++;
  333. for (let current : LinkedCollectionElement<E> | null = this._head; current !== null; current = current.getNext())
  334. current.setPrev(null);
  335. while (this._head !== null) {
  336. let left : LinkedCollectionElement<E> = this._head;
  337. let right : LinkedCollectionElement<E> | null = left.getNext();
  338. if (right === null)
  339. throw new NullPointerException()
  340. this._head = right.getNext();
  341. left.setNext(null);
  342. right.setNext(null);
  343. let sorted : LinkedCollectionElement<E> = this.merge(comparator, left, right);
  344. this._tail.setNext(sorted);
  345. this._tail = sorted;
  346. }
  347. this._head = this._tail;
  348. this._tail.setNext(this._tail.getPrev());
  349. while (this._tail.getPrev() !== null) {
  350. this._tail = this._tail.getPrev();
  351. if (this._tail === null)
  352. throw new NullPointerException()
  353. this._tail.setNext(this._tail.getPrev());
  354. }
  355. this._head.setPrev(null);
  356. let current : LinkedCollectionElement<E> = this._head;
  357. let next : LinkedCollectionElement<E> | null = current.getNext();
  358. while (next !== null) {
  359. next.setPrev(current);
  360. current = next;
  361. next = current.getNext();
  362. }
  363. this._modCount++;
  364. return true;
  365. }
  366. /**
  367. * Sucht das Element an der Stelle Index.
  368. *
  369. * @param index die Stelle des gesuchten Elements
  370. *
  371. * @return das Element an der gesuchten Stelle
  372. *
  373. * @throws IndexOutOfBoundsException wenn der Index nicht im gültigen Bereich liegt (index >= 0) && (index < size()))
  374. */
  375. private find(index : number) : LinkedCollectionElement<E> {
  376. if ((index < 0) || (index >= this._size))
  377. throw new IndexOutOfBoundsException()
  378. let current : LinkedCollectionElement<E> | null = this._head;
  379. for (let i : number = 0; (current !== null); i++, current = current.getNext())
  380. if (i === index)
  381. return current;
  382. throw new IndexOutOfBoundsException()
  383. }
  384. /**
  385. * Sucht ein LinkedCollectionElement in der Collection mit dem Wert obj
  386. * und gibt es zurück
  387. *
  388. * @param obj der Wert der in der LinkedCollection gesucht werden soll
  389. *
  390. * @return ein LinkedCollectionElement<E> falls der Wert in der Collection
  391. * enthalten ist und das Element dessen , ansonsten null
  392. */
  393. private findFirst(obj : unknown | null) : LinkedCollectionElement<E> | null {
  394. if (obj === null)
  395. return null;
  396. let current : LinkedCollectionElement<E> | null = this._head;
  397. while (current !== null) {
  398. if (JavaObject.equalsTranspiler(current.getValue(), (obj)))
  399. return current;
  400. current = current.getNext();
  401. }
  402. return null;
  403. }
  404. /**
  405. * Sucht ein LinkedCollectionElement in der Collection mit dem Wert obj
  406. * und gibt es zurück
  407. *
  408. * @param obj der Wert der in der LinkedCollection gesucht werden soll
  409. *
  410. * @return ein LinkedCollectionElement<E> falls der Wert in der Collection
  411. * enthalten ist und das Element dessen, ansonsten null
  412. */
  413. private findLast(obj : unknown | null) : LinkedCollectionElement<E> | null {
  414. if (obj === null)
  415. return null;
  416. let current : LinkedCollectionElement<E> | null = this._tail;
  417. while (current !== null) {
  418. if (JavaObject.equalsTranspiler(current.getValue(), (obj)))
  419. return current;
  420. current = current.getPrev();
  421. }
  422. return null;
  423. }
  424. public offer(e : E | null) : boolean {
  425. return this.add(e);
  426. }
  427. public poll() : E | null {
  428. if (this._head === null)
  429. return null;
  430. let value : E = this._head.getValue();
  431. this._head = this._head.getNext();
  432. if (this._head === null)
  433. this._tail = null; else
  434. this._head.setPrev(null);
  435. this._size--;
  436. this._modCount++;
  437. return value;
  438. }
  439. public element() : E {
  440. if (this._head === null)
  441. throw new NoSuchElementException()
  442. return this._head.getValue();
  443. }
  444. public peek() : E | null {
  445. return (this._head === null) ? null : this._head.getValue();
  446. }
  447. public addFirst(e : E | null) : void {
  448. if (e === null)
  449. throw new NullPointerException()
  450. let newElem : LinkedCollectionElement<E> = new LinkedCollectionElement(e, null, null);
  451. if ((this._head === null) || (this._tail === null)) {
  452. this._head = newElem;
  453. this._tail = newElem;
  454. } else {
  455. newElem.setNext(this._head);
  456. this._head.setPrev(newElem);
  457. this._head = newElem;
  458. }
  459. this._size++;
  460. this._modCount++;
  461. }
  462. public addLast(e : E | null) : void {
  463. if (e === null)
  464. throw new NullPointerException()
  465. this.add(e);
  466. }
  467. public offerFirst(e : E | null) : boolean {
  468. this.addFirst(e);
  469. return true;
  470. }
  471. public offerLast(e : E | null) : boolean {
  472. this.addLast(e);
  473. return true;
  474. }
  475. public removeFirst() : E {
  476. let value : E | null = this.poll();
  477. if (value === null)
  478. throw new NoSuchElementException()
  479. return value;
  480. }
  481. public removeLast() : E {
  482. let value : E | null = this.pollLast();
  483. if (value === null)
  484. throw new NoSuchElementException()
  485. return value;
  486. }
  487. public pollFirst() : E | null {
  488. return this.poll();
  489. }
  490. public pollLast() : E | null {
  491. if (this._tail === null)
  492. return null;
  493. let value : E = this._tail.getValue();
  494. this._tail = this._tail.getPrev();
  495. if (this._tail === null)
  496. this._head = null; else
  497. this._tail.setNext(null);
  498. this._size--;
  499. this._modCount++;
  500. return value;
  501. }
  502. public getFirst() : E {
  503. if (this._head === null)
  504. throw new NoSuchElementException()
  505. return this._head.getValue();
  506. }
  507. public getLast() : E {
  508. if (this._tail === null)
  509. throw new NoSuchElementException()
  510. return this._tail.getValue();
  511. }
  512. public peekFirst() : E | null {
  513. return (this._head === null) ? null : this._head.getValue();
  514. }
  515. public peekLast() : E | null {
  516. return (this._tail === null) ? null : this._tail.getValue();
  517. }
  518. public removeFirstOccurrence(obj : unknown | null) : boolean {
  519. if (this.isEmpty())
  520. return false;
  521. return this.removeElement(this.findFirst(obj));
  522. }
  523. public removeLastOccurrence(obj : unknown | null) : boolean {
  524. if (this.isEmpty())
  525. return false;
  526. return this.removeElement(this.findLast(obj));
  527. }
  528. public push(e : E) : void {
  529. this.addFirst(e);
  530. }
  531. public pop() : E {
  532. let value : E | null = this.poll();
  533. if (value === null)
  534. throw new NoSuchElementException()
  535. return value;
  536. }
  537. public descendingIterator() : JavaIterator<E> {
  538. return new LinkedCollectionDescendingIterator(this);
  539. }
  540. /**
  541. * Gibt den Wert an der Stelle index zurück.
  542. *
  543. * @param index der Index
  544. *
  545. * @return der Wert
  546. *
  547. * @throws IndexOutOfBoundsException wenn der Index nicht im gültigen Bereich liegt {@code (index >= 0) && (index < size()))}
  548. */
  549. public get(index : number) : E {
  550. return this.find(index).getValue();
  551. }
  552. /**
  553. * Ersetzt den Wert an der Stelle index mit dem neuen übergebenen Wert.
  554. *
  555. * @param index die Stelle, wo der Wert ersetzt werden soll
  556. * @param element der neue Wert für die Stelle
  557. *
  558. * @return der alte Wert an der Stelle
  559. *
  560. * @throws IndexOutOfBoundsException wenn der Index nicht im gültigen Bereich liegt {@code (index >= 0) && (index < size()))}
  561. */
  562. public set(index : number, element : E) : E {
  563. return this.find(index).setValue(element);
  564. }
  565. isTranspiledInstanceOf(name : string): boolean {
  566. return ['java.lang.Cloneable', 'java.util.Collection', 'java.util.Queue', 'java.util.Deque', 'java.lang.Iterable', 'de.nrw.schule.svws.core.adt.collection.LinkedCollection'].includes(name);
  567. }
  568. public [Symbol.iterator](): Iterator<E> {
  569. let iter : JavaIterator<E> = this.iterator();
  570. const result : Iterator<E> = {
  571. next() : IteratorResult<E> {
  572. if (iter.hasNext())
  573. return { value : iter.next(), done : false };
  574. return { value : null, done : true };
  575. }
  576. };
  577. return result;
  578. }
  579. }
  580. export function cast_de_nrw_schule_svws_core_adt_collection_LinkedCollection<E>(obj : unknown) : LinkedCollection<E> {
  581. return obj as LinkedCollection<E>;
  582. }