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