Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- django
- MySQL
- management
- agile
- Programming
- web
- Spain
- Kuala Lumpur
- France
- RFID
- program
- programming_book
- leadership
- Linux
- comic agile
- Software Engineering
- Python
- erlang
- history
- QT
- essay
- Java
- Book review
- hbase
- Italy
- ubuntu
- psychology
- hadoop
- Book
- Malaysia
Archives
- Today
- Total
EnumMap factory 본문
import java.util.Map; import java.util.EnumMap; import java.util.EnumSet; import com.google.common.collect.Maps; //import javax.annotation.Nullable; // $ javac -cp .:./Guava/guava-r09/guava-r09.jar TestEnumMap.java // $ java -cp .:./Guava/guava-r09/guava-r09.jar TestEnumMap public class TestEnumMap { public static void main(String[] args) { EnumMap<CHS, Integer> m1 = EnumMapFactory.newEnumMap(CHS.class); m1.put(CHS.A, 1); m1.put(CHS.B, 2); EnumMap<CHS, Integer> m2 = m1.clone(); m2.put(CHS.C, 1); m2.put(CHS.C, 2); EnumMap<CHS, Integer> m3 = EnumMapFactory.<CHS, Integer>newEnumMap(CHS.class); m3.put(CHS.C, 3); EnumMap<CHS, Integer> m4 = Maps.<CHS, Integer>newEnumMap(CHS.class); m4.put(CHS.A, 1); EnumMap<CHS, Long> m5 = EnumMapFactory.<CHS, Long>newEnumMap(CHS.class); m5.put(CHS.A, 1L); System.out.println(m1 + "\n" + m2 + "\n" + m3 + "\n" + m4 + "\n" + m5); } } // http://code.google.com/p/guava-libraries/downloads/detail?name=guava-concurrent-slides.pdf&can=2&q= class EnumMapFactory<K, V> { public static <T> T checkNotNull(T reference) { if (reference == null) { throw new NullPointerException(); } return reference; } public static <K extends Enum<K>, V> EnumMap<K, V> newEnumMap( Class<K> type) { return new EnumMap<K, V>(checkNotNull(type)); } public static <K extends Enum<K>, V> EnumMap<K,V> newEnumMap( Class<K> type, //@Nullable V defaultValue) { V defaultValue) { return newEnumMap(type, EnumSet.allOf(type), defaultValue); } public static <K extends Enum<K>, V> EnumMap<K,V> newEnumMap( Class<K> type, Iterable<? extends K> keys, //@Nullable V defaultValue) { V defaultValue) { EnumMap<K, V> map = newEnumMap(type); setValues(map, EnumSet.allOf(type), defaultValue); return map; } public static <K, V> void setValues( Map<? super K, ? super V> map, Iterable<? extends K> keys, //@Nullable V value) { V value) { checkNotNull(map); checkNotNull(keys); for (K key: keys) { map.put(key, value); } } } enum CHS { A, B, C }
Comments