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;
    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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy