1 28 29 package org.jibx.runtime; 30 31 import java.util.Arrays ; 32 import java.util.Comparator ; 33 34 43 44 public class EnumSet 45 { 46 47 public static final int VALUE_LIMIT = 512; 48 49 50 private final EnumItem[] m_items; 51 52 53 private final String [] m_indexedNames; 54 55 56 private final String [] m_orderedNames; 57 58 59 private final int[] m_orderedIndexes; 60 61 70 71 public EnumSet(EnumItem[] items) { 72 m_items = items; 73 if (items.length > 0) { 74 75 Arrays.sort(items, new Comparator () { 77 public int compare(Object a, Object b) { 78 return ((EnumItem)a).m_name.compareTo(((EnumItem)b).m_name); 79 } 80 }); 81 82 m_orderedNames = new String [items.length]; 84 m_orderedIndexes = new int[items.length]; 85 int high = -1; 86 for (int i = 0; i < items.length; i++) { 87 EnumItem item = items[i]; 88 if (item.m_value < 0) { 89 throw new IllegalArgumentException ("Negative item value " + 90 item.m_value + " not allowed"); 91 } else if (item.m_value > high) { 92 high = item.m_value; 93 if (high >= VALUE_LIMIT) { 94 throw new IllegalArgumentException 95 ("Enumeration with value " + 96 high + " too large to be used."); 97 } 98 } 99 m_orderedNames[i] = item.m_name; 100 m_orderedIndexes[i] = item.m_value; 101 } 102 103 m_indexedNames = new String [high+1]; 105 for (int i = 0; i < items.length; i++) { 106 EnumItem item = items[i]; 107 if (m_indexedNames[item.m_value] == null) { 108 m_indexedNames[item.m_value] = item.m_name; 109 } else { 110 throw new IllegalArgumentException 111 ("Duplicate index value " + item.m_value); 112 } 113 } 114 115 } else { 116 m_indexedNames = new String [0]; 117 m_orderedNames = new String [0]; 118 m_orderedIndexes = new int[0]; 119 } 120 } 121 122 129 130 public EnumSet(int start, String [] names) { 131 this(buildItems(start, names)); 132 } 133 134 143 144 public EnumSet(EnumSet base, int start, String [] names) { 145 this(mergeItems(base, start, names)); 146 } 147 148 156 157 private static EnumItem[] buildItems(int start, String [] names) { 158 EnumItem[] items = new EnumItem[names.length]; 159 for (int i = 0; i < items.length; i++) { 160 items[i] = new EnumItem(start+i, names[i]); 161 } 162 return items; 163 } 164 165 174 175 private static EnumItem[] mergeItems(EnumSet base, int start, 176 String [] names) { 177 int prior = base.m_items.length; 178 EnumItem[] merges = new EnumItem[prior + names.length]; 179 System.arraycopy(base.m_items, 0, merges, 0, prior); 180 for (int i = 0; i < names.length; i++) { 181 merges[prior+i] = new EnumItem(start+i, names[i]); 182 } 183 return merges; 184 } 185 186 192 193 194 public String getName(int value) { 195 if (value >= 0 && value < m_indexedNames.length) { 196 String name = m_indexedNames[value]; 197 if (name != null) { 198 return name; 199 } 200 } 201 throw new IllegalArgumentException ("Value " + value + " not defined"); 202 } 203 204 210 211 public int getValue(String name) { 212 int base = 0; 213 int limit = m_orderedNames.length - 1; 214 while (base <= limit) { 215 int cur = (base + limit) >> 1; 216 int diff = name.compareTo(m_orderedNames[cur]); 217 if (diff < 0) { 218 limit = cur - 1; 219 } else if (diff > 0) { 220 base = cur + 1; 221 } else if (m_orderedIndexes != null) { 222 return m_orderedIndexes[cur]; 223 } else { 224 return cur; 225 } 226 } 227 return -1; 228 } 229 230 237 238 public int getValueChecked(String name) { 239 int index = getValue(name); 240 if (index >= 0) { 241 return index; 242 } else { 243 throw new IllegalArgumentException ("Name " + name + " not defined"); 244 } 245 } 246 247 251 252 public static class EnumItem { 253 public final int m_value; 254 public final String m_name; 255 256 public EnumItem(int value, String name) { 257 m_value = value; 258 m_name = name; 259 } 260 } 261 } | Popular Tags |