-
Notifications
You must be signed in to change notification settings - Fork 0
/
IterableValue.java
43 lines (36 loc) · 993 Bytes
/
IterableValue.java
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
package org.example.interpreter.model.value;
import java.util.ArrayList;
import java.util.Iterator;
import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;
import org.example.ast.type.TypeDeclaration;
import org.example.interpreter.model.Value;
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class IterableValue implements Value, Iterator<Value> {
TypeDeclaration type;
Iterator<Value> iterator;
public IterableValue(TypeDeclaration type, Iterable<Value> iterable) {
this.type = type;
this.iterator = iterable.iterator();
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public Value next() {
return iterator.next();
}
@Override
public Value copy() {
var results = new ArrayList<Value>();
while (iterator.hasNext()) {
results.add(iterator.next());
}
iterator = results.iterator();
var copy = results.stream().map(Value::copy).toList();
return new IterableValue(type, copy);
}
}