functionalj.map.FuncMapBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
package functionalj.map;
import java.util.ArrayList;
import java.util.List;
import functionalj.map.FuncMap.UnderlineMap;
import functionalj.tuple.Tuple;
import functionalj.tuple.Tuple2;
import lombok.val;
public class FuncMapBuilder {
private final List> entries;
private final int count;
public FuncMapBuilder() {
this.entries = new ArrayList>();
this.count = 0;
}
private FuncMapBuilder(List> entries) {
this.entries = entries;
this.count = this.entries.size();
}
public FuncMapBuilder with(K key, V value) {
this.entries.add(Tuple.of(key, value));
return new FuncMapBuilder<>(this.entries);
}
public ImmutableMap build() {
val map = FuncMap.underlineMap.orElse(UnderlineMap.HashMap).newMap();
for (int i = 0; i < count; i++) {
Tuple2 entry = entries.get(i);
K key = entry._1();
V value = entry._2();
map.put(key, value);
}
return new ImmutableMap(map);
}
}