001package squidpony.squidmath; 002 003/** 004 * A simple alternative to EnumMap that has a zero-argument constructor (which makes serialization easier) but is a 005 * little less efficient. However, it supports more features by inheriting from {@link OrderedMap}, such as retrieval of 006 * random keys or values with {@link #randomKey(IRNG)} and {@link #randomValue(IRNG)}, iteration in insertion order 007 * instead of always using enum declaration order (order can be shuffled with {@link #shuffle(IRNG)} or reordered with 008 * {@link #reorder(int...)}), and a little more. The implementation is nearly trivial due to how OrderedMap allows 009 * customization of hashing strategy with its IHasher option, and this class always uses a specific custom IHasher to 010 * hash Enum values by their ordinal. This IHasher is shared with {@link EnumOrderedSet}. 011 * <br> 012 * Created by Tommy Ettinger on 10/21/2017. 013 */ 014public class EnumOrderedMap<K extends Enum<?>, V> extends OrderedMap<K, V> { 015 public EnumOrderedMap() 016 { 017 super(16, 0.9375f, HashCommon.enumHasher); 018 } 019 public EnumOrderedMap(Class<K> enumClass) { 020 super(enumClass.getEnumConstants().length, 0.9375f, HashCommon.enumHasher); 021 } 022 public EnumOrderedMap(K enumObject) { 023 super(enumObject.getClass().getEnumConstants().length, 0.9375f, HashCommon.enumHasher); 024 } 025 026 @Override 027 public String toString() { 028 final StringBuilder s = new StringBuilder(); 029 int n = size(), i = 0; 030 boolean first = true; 031 s.append("EnumOrderedMap{"); 032 while (i < n) { 033 if (first) first = false; 034 else s.append(", "); 035 s.append(entryAt(i++)); 036 } 037 s.append("}"); 038 return s.toString(); 039 } 040}