-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiterator.ts
54 lines (42 loc) · 886 Bytes
/
iterator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
export interface Iterator {
next () : any
hasNext (): Boolean
}
interface Aggregate {
iterator(): Iterator
}
export class Item {
private name: String
constructor (name: String) {
this.name = name
}
public getName (): String {
return this.name
}
}
export class ItemBag implements Aggregate {
private items: Array<Item>
constructor () {
this.items = []
}
public add (item: Item): void {
this.items.push(item)
}
public iterator (): Iterator {
return new ItemBagIterator(this.items)
}
}
class ItemBagIterator implements Iterator {
private items: Array<Item>
private index: number
constructor (items: Array<Item>) {
this.items= items
this.index = 0
}
public next (): Item {
return this.items[this.index++] as Item
}
public hasNext (): Boolean {
return this.index < this.items.length
}
}