2015-04-20 15:36:01 -07:00
|
|
|
//: net/mindview/util/MapData.java
|
|
|
|
// A Map filled with data using a generator object.
|
|
|
|
package net.mindview.util;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class MapData<K,V> extends LinkedHashMap<K,V> {
|
|
|
|
// A single Pair Generator:
|
|
|
|
public MapData(Generator<Pair<K,V>> gen, int quantity) {
|
|
|
|
for(int i = 0; i < quantity; i++) {
|
|
|
|
Pair<K,V> p = gen.next();
|
|
|
|
put(p.key, p.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Two separate Generators:
|
|
|
|
public MapData(Generator<K> genK, Generator<V> genV,
|
|
|
|
int quantity) {
|
|
|
|
for(int i = 0; i < quantity; i++) {
|
|
|
|
put(genK.next(), genV.next());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// A key Generator and a single value:
|
|
|
|
public MapData(Generator<K> genK, V value, int quantity){
|
|
|
|
for(int i = 0; i < quantity; i++) {
|
|
|
|
put(genK.next(), value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// An Iterable and a value Generator:
|
|
|
|
public MapData(Iterable<K> genK, Generator<V> genV) {
|
|
|
|
for(K key : genK) {
|
|
|
|
put(key, genV.next());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// An Iterable and a single value:
|
|
|
|
public MapData(Iterable<K> genK, V value) {
|
|
|
|
for(K key : genK) {
|
|
|
|
put(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Generic convenience methods:
|
|
|
|
public static <K,V> MapData<K,V>
|
|
|
|
map(Generator<Pair<K,V>> gen, int quantity) {
|
2015-05-05 11:20:13 -07:00
|
|
|
return new MapData<>(gen, quantity);
|
2015-04-20 15:36:01 -07:00
|
|
|
}
|
|
|
|
public static <K,V> MapData<K,V>
|
|
|
|
map(Generator<K> genK, Generator<V> genV, int quantity) {
|
2015-05-05 11:20:13 -07:00
|
|
|
return new MapData<>(genK, genV, quantity);
|
2015-04-20 15:36:01 -07:00
|
|
|
}
|
|
|
|
public static <K,V> MapData<K,V>
|
|
|
|
map(Generator<K> genK, V value, int quantity) {
|
2015-05-05 11:20:13 -07:00
|
|
|
return new MapData<>(genK, value, quantity);
|
2015-04-20 15:36:01 -07:00
|
|
|
}
|
|
|
|
public static <K,V> MapData<K,V>
|
|
|
|
map(Iterable<K> genK, Generator<V> genV) {
|
2015-05-05 11:20:13 -07:00
|
|
|
return new MapData<>(genK, genV);
|
2015-04-20 15:36:01 -07:00
|
|
|
}
|
|
|
|
public static <K,V> MapData<K,V>
|
|
|
|
map(Iterable<K> genK, V value) {
|
2015-05-05 11:20:13 -07:00
|
|
|
return new MapData<>(genK, value);
|
2015-04-20 15:36:01 -07:00
|
|
|
}
|
|
|
|
} ///:~
|