// com/mindviewinc/util/MapData.java // A Map filled with data using a generator object. package com.mindviewinc.util; import java.util.*; import java.util.function.*; public class MapData extends LinkedHashMap { // A single Pair Supplier: public MapData(Supplier> gen, int quantity) { for(int i = 0; i < quantity; i++) { Pair p = gen.get(); put(p.key, p.value); } } // Two separate Suppliers: public MapData(Supplier genK, Supplier genV, int quantity) { for(int i = 0; i < quantity; i++) { put(genK.get(), genV.get()); } } // A key Supplier and a single value: public MapData(Supplier genK, V value, int quantity){ for(int i = 0; i < quantity; i++) { put(genK.get(), value); } } // An Iterable and a value Supplier: public MapData(Iterable genK, Supplier genV) { for(K key : genK) { put(key, genV.get()); } } // An Iterable and a single value: public MapData(Iterable genK, V value) { for(K key : genK) { put(key, value); } } // Generic convenience methods: public static MapData map(Supplier> gen, int quantity) { return new MapData<>(gen, quantity); } public static MapData map(Supplier genK, Supplier genV, int quantity) { return new MapData<>(genK, genV, quantity); } public static MapData map(Supplier genK, V value, int quantity) { return new MapData<>(genK, value, quantity); } public static MapData map(Iterable genK, Supplier genV) { return new MapData<>(genK, genV); } public static MapData map(Iterable genK, V value) { return new MapData<>(genK, value); } }