All Downloads are FREE. Search and download functionalities are using the official Maven repository.

functionalj.map.FuncMapBuilder Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
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;
    
    public FuncMapBuilder() {
        this.entries = new ArrayList>();
    }
    private FuncMapBuilder(List> entries) {
        this.entries = entries;
    }
    
    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 (Tuple2 entry : this.entries) {
            K key   = entry._1();
            V value = entry._2();
            map.put(key, value);
        }
        return new ImmutableMap(map);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy