Skip to content

Commit 07fd229

Browse files
committed
Добавлен метод ifPresent для MapValue
1 parent c9249e8 commit 07fd229

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/main/java/com/annimon/ownlang/lib/MapValue.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Iterator;
66
import java.util.Map;
77
import java.util.Objects;
8+
import java.util.function.Consumer;
89

910
/**
1011
*
@@ -31,6 +32,18 @@ public MapValue(Map<Value, Value> map) {
3132
this.map = map;
3233
}
3334

35+
public boolean ifPresent(String key, Consumer<Value> consumer) {
36+
return ifPresent(new StringValue(key), consumer);
37+
}
38+
39+
public boolean ifPresent(Value key, Consumer<Value> consumer) {
40+
if (map.containsKey(key)) {
41+
consumer.accept(map.get(key));
42+
return true;
43+
}
44+
return false;
45+
}
46+
3447
public ArrayValue toPairs() {
3548
final int size = map.size();
3649
final ArrayValue result = new ArrayValue(size);

0 commit comments

Comments
 (0)