11/*
2- * Created by Trevor Sears <trevorsears.main@gmail.com>.
3- * 7:43 PM -- October 06th, 2019.
4- * Project: @jsdsl/iterator
2+ * Created by Trevor Sears <trevor@trevorsears.com> (https://trevorsears.com/).
3+ * 3:43 PM -- August 31st, 2021.
4+ * Project: @jsdsl/iterator
5+ *
6+ * @jsdsl /iterator - A collection of classes that allow iteration over a predefined collection of elements.
7+ * Copyright (C) 2021 Trevor Sears
8+ *
9+ * This program is free software: you can redistribute it and/or modify
10+ * it under the terms of the GNU General Public License as published by
11+ * the Free Software Foundation, either version 3 of the License, or
12+ * (at your option) any later version.
13+ *
14+ * This program is distributed in the hope that it will be useful,
15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+ * GNU General Public License for more details.
18+ *
19+ * You should have received a copy of the GNU General Public License
20+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
521 */
622
7- import { Iterator as JSDSLIterator } from "./iterator" ;
23+ import { Iterator } from "./iterator" ;
824import { AbstractIterable } from "./abstract-iterable" ;
925
1026/**
11- * An abstract implementation of the JSDSL {@link Iterator} interface.
27+ * A basic abstract implementation of the JSDSL {@link Iterator} protocol, a class that furnishes the functionality
28+ * required to use an object with the JavaScript `for...of` syntax.
1229 *
13- * @author Trevor Sears <trevorsears.main@gmail.com>
14- * @version v0.2.0
30+ * Note: Usually, classes that desire to be 'iterable' should not extend this class, but should instead implement the
31+ * {@link Iterable} interface (or extend the {@link AbstractIterable} abstract class). Extending this class
32+ * syntactically implies that the class in question 'is-a' iterator, whereas most use cases intend to assert that the
33+ * class in question 'has-a' iterator. In either case, iteration via `for...of` works as expected (with either an
34+ * {@link Iterator} or an {@link Iterable}).
35+ *
36+ * @param {any } E The type of element that this AbstractIterator intends to iterate over.
37+ * @param {any } U The type of the value that will be returned in the case that this AbstractIterator's content has been
38+ * exhausted. Can be set to 'void' in the case that this AbstractIterator has infinite content. Defaults to `undefined`.
39+ *
40+ * @author Trevor Sears <trevor@trevorsears.com>
41+ * @version v1.0.0
1542 * @since v0.1.0
1643 */
17- export abstract class AbstractIterator < E > extends AbstractIterable < E > implements JSDSLIterator < E > {
44+ export abstract class AbstractIterator < E , U = undefined > extends AbstractIterable < E , U > implements Iterator < E , U > {
1845
1946 /**
2047 * Returns true if a call to {@link #next} would return a valid and meaningful result after calling this method.
@@ -24,25 +51,23 @@ export abstract class AbstractIterator<E> extends AbstractIterable<E> implements
2451 * present in the underlying structure versus the undefined value that is returned from {@link #next} when all other
2552 * valid results are exhausted.
2653 *
27- * @return true if a call to {@link #next} would return a valid and meaningful result.
54+ * @return { boolean } true if a call to {@link #next} would return a valid and meaningful result.
2855 */
2956 public abstract hasNext ( ) : boolean ;
3057
3158 /**
32- * Returns the next element this AbstractIterator has to iterate over, over undefined if there are no more valid
33- * elements to return.
59+ * Returns the next element this AbstractIterator has to iterate over.
3460 *
35- * @return The next element this AbstractIterator has to iterate over, over undefined if there are no more valid
36- * elements to return.
61+ * @return {E | U } The next element this AbstractIterator has to iterate over.
3762 */
38- public abstract next ( ) : E | undefined ;
63+ public abstract next ( ) : E | U ;
3964
4065 /**
4166 * Performs the specified action for all of the remaining elements in this AbstractIterator.
4267 *
43- * @param callback The action to perform on the remaining elements of this AbstractIterator.
68+ * @param { (element: E) => any } callback The action to perform on the remaining elements of this AbstractIterator.
4469 */
45- public forEach ( callback : ( element : E ) => void ) : void {
70+ public forEachRemaining ( callback : ( element : E ) => any ) : void {
4671
4772 for ( let element of this ) callback ( element ) ;
4873
@@ -54,9 +79,9 @@ export abstract class AbstractIterator<E> extends AbstractIterable<E> implements
5479 * Note if this method is not overridden in implementing child classes, the `#remove` method will throw an error,
5580 * with a warning that the method is not supported for the current implementation.
5681 *
57- * @return The last element returned by the {@link #next} method.
82+ * @return { E | U } The last element returned by the {@link #next} method.
5883 */
59- public remove ( ) : E | undefined {
84+ public remove ( ) : E | U {
6085
6186 throw new Error ( "ERR | #remove operation is not supported for this implementation of AbstractIterator." ) ;
6287
@@ -66,8 +91,11 @@ export abstract class AbstractIterator<E> extends AbstractIterable<E> implements
6691 * Resets this AbstractIterator back to it's initial position, readying it to iterate over the underlying collection
6792 * from the 'beginning' again.
6893 *
69- * Note if this method is not overridden in implementing child classes, the `#reset` method will throw an error,
94+ * Note: If this method is not overridden in implementing child classes, the `#reset` method will throw an error,
7095 * with a warning that the method is not supported for the current implementation.
96+ *
97+ * Note: This does not/should not modify the underlying data structure, meaning that any modifications to the
98+ * underlying structure will not be/should not be 'undone' by calling this method.
7199 */
72100 public reset ( ) : void {
73101
@@ -76,56 +104,17 @@ export abstract class AbstractIterator<E> extends AbstractIterable<E> implements
76104 }
77105
78106 /**
79- * Returns an Iterator over the contents of this AbstractIterator.
107+ * Returns an { @link Iterator} over the contents of this AbstractIterator.
80108 *
81- * Because of the fact that this class is in-and-of-itself an Iterator, this method simply returns 'this'. This is
82- * simply a formality so that this class will conform to the {@link Iterable} interface.
109+ * Because of the fact that this class is in-and-of-itself an { @link Iterator} , this method simply returns 'this'.
110+ * This is simply a formality so that this class will conform to the {@link Iterable} interface.
83111 *
84- * @return An Iterator over the contents of this AbstractIterator.
112+ * @return { Iterator<E, U> } An { @link Iterator} over the contents of this AbstractIterator.
85113 */
86- public iterator ( ) : JSDSLIterator < E > {
114+ public iterator ( ) : Iterator < E , U > {
87115
88116 return this ;
89117
90118 }
91119
92- /**
93- * This method is simply an ease-of-understanding alias method for the [Symbol.iterator] method.
94- *
95- * @return An instance of an IterableIterator.
96- * @see AbstractIterator#[Symbol.iterator]
97- */
98- public getIterableIterator ( ) : IterableIterator < E > {
99-
100- return new class implements IterableIterator < E > {
101-
102- private iterator : AbstractIterator < E > ;
103-
104- public constructor ( iterator : AbstractIterator < E > ) {
105-
106- this . iterator = iterator ;
107-
108- }
109-
110- public [ Symbol . iterator ] ( ) : IterableIterator < E > {
111-
112- return this ;
113-
114- }
115-
116- public next ( ) : IteratorResult < E > {
117-
118- return {
119-
120- done : ! this . iterator . hasNext ( ) ,
121- value : this . iterator . next ( ) as E
122-
123- } ;
124-
125- }
126-
127- } ( this ) ;
128-
129- }
130-
131- }
120+ }
0 commit comments