Map生成器 map适配器如今能够使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象

package org.rui.collection2.map;
/**
* map生成器
* @author lenovo
*
*/
public class Pair<K,V> {
public final K key;
public final V value;
public Pair(K k,V v)
{
this.key=k;
this.value=v;
}
}

//Generator.java
package org.rui.generics.anonymity; public interface Generator<T> { //返回泛型的内型对象
T next(); }

package org.rui.collection2.map;
import java.util.Iterator;
import java.util.LinkedHashMap; import org.rui.generics.anonymity.Generator;
/**
* map 适配器如今能够使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象
* @author lenovo
*
* @param <K>
* @param <V>
*/ public class MapData<K,V> extends LinkedHashMap<K,V>
{ 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);
}
}
///////////////////////////////////////////////////////////
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 genV,int quantity)
{
for(int i=0;i<quantity;i++)
{
put(genK.next(),genV);
}
}
///////////an iterable and a value generator////////////////////////////////////////////////
public MapData(Iterable<K> genK,Generator<V> genV)
{
for(K k : genK)
{
put(k,genV.next());
}
}
///////////an iterable and a single value////////////////////////////////////////////////
public MapData(Iterable<K> genK,V v)
{
for(K k : genK)
{
//System.out.println(k);
put(k,v);
}
}
/////////////generic convenience methods/////////////////////////////////////////// public static<K,V> MapData<K,V> map(Generator<Pair<K,V>> gen,int quantity)
{
return new MapData<K,V>(gen,quantity);
}
public static<K,V> MapData<K,V> map(Generator<K> gen,Generator<V> genV,int quantity)
{
return new MapData<K,V>(gen,genV,quantity);
}
public static<K,V> MapData<K,V> map(Generator<K> gen,V v,int quantity)
{
return new MapData<K,V>(gen,v,quantity);
}
public static<K,V> MapData<K,V> map(Iterable<K> k,Generator<V> v)
{
return new MapData<K,V>(k,v);
}
public static<K,V> MapData<K,V> map(Iterable<K> k,V v)
{
return new MapData<K,V>(k,v);
}
}
package org.rui.collection2.map;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List; import org.rui.generics.anonymity.Generator; /**
* 以下是一个使用MapData的演示样例,LettersGenerator
* 通过产生一个Iterator还实现了Iterable,通过这样的方式,
* 它能够被用来測试MapData.map()方法,而这些方法都须要用到iterable
* @author lenovo
*
*/ class Letters implements Generator<Pair<Integer,String>>,Iterable<Integer>
{ private int size=9;
private int number=1;
private char letter='a'; @Override
public Pair<Integer, String> next() {
return new Pair<Integer,String>(number++,""+letter++);
} @Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>()
{
public Integer next() {return number++;}
public boolean hasNext(){return number<size;}
public void remove()
{
throw new UnsupportedOperationException();
}
};
}
} public class MapDataTest {
public static void main(String[] args)
{
Character[] chars={'a','b','c','d','e'}; List<Character> list = Arrays.asList(chars);
System.out.println(MapData.map(new Letters(),11));
System.out.println(MapData.map(new Letters(),"pop"));
System.out.println(MapData.map(new Letters(),new Letters()));
System.out.println(MapData.map(list,"value")); }
}
/**output:
{1=a, 2=b, 3=c, 4=d, 5=e, 6=f, 7=g, 8=h, 9=i, 10=j, 11=k}
{1=pop, 2=pop, 3=pop, 4=pop, 5=pop, 6=pop, 7=pop, 8=pop}
{1=org.rui.collection2.map.Pair@170bea5, 2=org.rui.collection2.map.Pair@f47396, 3=org.rui.collection2.map.Pair@d0af9b, 4=org.rui.collection2.map.Pair@b8f8eb, 5=org.rui.collection2.map.Pair@1de17f4, 6=org.rui.collection2.map.Pair@1f6ba0f, 7=org.rui.collection2.map.Pair@1313906, 8=org.rui.collection2.map.Pair@96cf11}
{a=value, b=value, c=value, d=value, e=value}
*/
04-22 14:34