1 | /* |
2 | Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/ |
3 | |
4 | This program is free software; |
5 | you can redistribute it and/or modify it under the terms of the |
6 | GNU General Public License as published by the Free Software Foundation; |
7 | either version 3 of the License, or any later version. |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | You should have received a copy of the GNU General Public License |
13 | along with this program; if not, write to the Free Software |
14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
15 | */ |
16 | /* |
17 | Changes: |
18 | - v4.0.0: |
19 | - created from v3.6.0 |
20 | Doc reviewed 202208 |
21 | */ |
22 | |
23 | import { ZERO, ONE, NOT_FOUND } from '../main/Constants.js'; |
24 | |
25 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
26 | /** |
27 | iterator for Collection class |
28 | */ |
29 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
30 | |
31 | class CollectionIterator { |
32 | |
33 | /** |
34 | The collection used by the iterator |
35 | @type {Collection} |
36 | */ |
37 | |
38 | #collection = null; |
39 | |
40 | /** |
41 | The current index |
42 | @type {Number} |
43 | */ |
44 | |
45 | #index = NOT_FOUND; |
46 | |
47 | /** |
48 | The constructor |
49 | @param {Collection} collection The collection for witch the iterator is used |
50 | */ |
51 | |
52 | constructor ( collection ) { |
53 | Object.freeze ( this ); |
54 | this.#collection = collection; |
55 | } |
56 | |
57 | /** |
58 | The object pointed by the iterator |
59 | @type {TravelObject} |
60 | */ |
61 | |
62 | get value ( ) { return this.#index < this.#collection.length ? this.#collection.at ( this.#index ) : null; } |
63 | |
64 | /** |
65 | The object before the object pointed by the iterator or null if iterator is on the first object |
66 | @type {TravelObject} |
67 | */ |
68 | |
69 | get previous ( ) { return ZERO >= this.#index ? null : this.#collection.at ( this.#index - ONE ); } |
70 | |
71 | /** |
72 | The object after the object pointed by the iterator or null if iterator is on the last object |
73 | @type {TravelObject} |
74 | */ |
75 | |
76 | get next ( ) { return this.#index < this.#collection.length - ONE ? this.#collection.at ( this.#index + ONE ) : null; } |
77 | |
78 | /** |
79 | Move the iterator to the next object and return true when the end of the Collection is reached |
80 | @type {Boolean} |
81 | */ |
82 | |
83 | get done ( ) { return ++ this.#index >= this.#collection.length; } |
84 | |
85 | /** |
86 | returns true when the iterator is on the first object |
87 | @type {Boolean} |
88 | */ |
89 | |
90 | get first ( ) { return ZERO === this.#index; } |
91 | |
92 | /** |
93 | returns true when the iterator is on the last object |
94 | @type {Boolean} |
95 | */ |
96 | |
97 | get last ( ) { return this.#index >= this.#collection.length - ONE; } |
98 | |
99 | /** |
100 | returns The position of the iterator in the Collection |
101 | @type {Number} |
102 | */ |
103 | |
104 | get index ( ) { return this.#index; } |
105 | } |
106 | |
107 | export default CollectionIterator; |
108 | |
109 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
110 |