|
| 1 | +package btp.oneP; |
| 2 | + |
| 3 | +import java.util.AbstractMap; |
| 4 | +import java.util.AbstractSet; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.Hashtable; |
| 9 | +import java.util.Iterator; |
| 10 | +import java.util.LinkedHashMap; |
| 11 | +import java.util.LinkedHashSet; |
| 12 | +import java.util.LinkedList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Set; |
| 16 | +import java.util.TreeMap; |
| 17 | +import java.util.TreeSet; |
| 18 | + |
| 19 | +public class Countries { |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + System.out.println(capitals(10)); |
| 23 | + System.out.println(names(10)); |
| 24 | + System.out.println(new HashMap<String,String>(capitals(3))); |
| 25 | + System.out.println(new LinkedHashMap<String,String>(capitals(3))); |
| 26 | + System.out.println(new TreeMap<String,String>(capitals(3))); |
| 27 | + System.out.println(new Hashtable<String,String>(capitals(3))); |
| 28 | + System.out.println(new HashSet<String>(names(6))); |
| 29 | + System.out.println(new LinkedHashSet<String>(names(6))); |
| 30 | + System.out.println(new TreeSet<String>(names(6))); |
| 31 | + System.out.println(new ArrayList<String>(names(6))); |
| 32 | + System.out.println(new LinkedList<String>(names(6))); |
| 33 | + System.out.println(capitals().get("ALGERIA")); |
| 34 | + } |
| 35 | + |
| 36 | + public static final String[][] DATA = { |
| 37 | + {"ALGERIA","Algiers"},{"CHINA","Beijing"}, |
| 38 | + {"Japan","Tokoy"},{"America","Washington"} |
| 39 | + }; |
| 40 | + private static class FlyweightMap extends AbstractMap<String,String>{ |
| 41 | + private static class Entry implements Map.Entry<String, String>{ |
| 42 | + int index; |
| 43 | + Entry(int index){ |
| 44 | + this.index = index; |
| 45 | + } |
| 46 | + public boolean equals(Object o){ |
| 47 | + return DATA[index][0].equals(o); |
| 48 | + } |
| 49 | + @Override |
| 50 | + public String getKey() { |
| 51 | + // TODO Auto-generated method stub |
| 52 | + return DATA[index][0]; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String getValue() { |
| 57 | + return DATA[index][1]; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String setValue(String value) { |
| 62 | + // TODO Auto-generated method stub |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + public int hashCode(){ |
| 67 | + return DATA[index][0].hashCode(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + static class EntrySet extends AbstractSet<Map.Entry<String, String>>{ |
| 72 | + private int size; |
| 73 | + EntrySet(int size){ |
| 74 | + if(size < 0){ |
| 75 | + this.size = 0; |
| 76 | + }else if(size > DATA.length){ |
| 77 | + this.size = DATA.length; |
| 78 | + }else{ |
| 79 | + this.size = size; |
| 80 | + } |
| 81 | + } |
| 82 | + public int size(){ |
| 83 | + return size; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Iterator<java.util.Map.Entry<String, String>> iterator() { |
| 88 | + // TODO Auto-generated method stub |
| 89 | + return new Iter(); |
| 90 | + } |
| 91 | + |
| 92 | + private class Iter implements Iterator<Map.Entry<String, String>>{ |
| 93 | + private Entry entry = new Entry(-1); |
| 94 | + @Override |
| 95 | + public boolean hasNext() { |
| 96 | + // TODO Auto-generated method stub |
| 97 | + return entry.index < size - 1; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public java.util.Map.Entry<String, String> next() { |
| 102 | + entry.index++; |
| 103 | + return entry; |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static Set<Map.Entry<String, String>> entries = new EntrySet(DATA.length); |
| 110 | + @Override |
| 111 | + public Set<java.util.Map.Entry<String, String>> entrySet() { |
| 112 | + return entries; |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + static Map<String,String> select(final int size){ |
| 123 | + return new FlyweightMap(){ |
| 124 | + public Set<Map.Entry<String, String>> entrySet(){ |
| 125 | + return new EntrySet(size); |
| 126 | + } |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + static Map<String,String> map = new FlyweightMap(); |
| 131 | + public static Map<String,String> capitals(){ |
| 132 | + return map; |
| 133 | + } |
| 134 | + public static Map<String,String> capitals(int size){ |
| 135 | + return select(size); |
| 136 | + } |
| 137 | + static List<String> names = new ArrayList<String>(map.keySet()); |
| 138 | + |
| 139 | + public static List<String> names(){ |
| 140 | + return names; |
| 141 | + } |
| 142 | + public static List<String> names(int size){ |
| 143 | + return new ArrayList<String>(select(size).keySet()); |
| 144 | + } |
| 145 | +} |
0 commit comments