/** * Looks up an instance by name. * <p> * This finds the instance matching the specified name. * Instances may have alternate names (aliases), thus the returned instance * may have a name other than that requested. * * @param name the enum name to return * @return the named enum * @throws IllegalArgumentException if the name is not found */ public T lookup(String name) { return find(name) .orElseThrow(() -> new IllegalArgumentException(type.getSimpleName() + " name not found: " + name)); }
/** * Obtains a combined extended enum instance. * <p> * Calling this method loads configuration files to determine which extended enums to combine. * The configuration file has the same simple name as the specified type and is a * {@linkplain IniFile INI file} with the suffix '.ini'. * * @param <R> the type of the enum * @param type the type to load * @return the extended enum */ public static <R extends Named> CombinedExtendedEnum<R> of(Class<R> type) { try { // load all matching files String name = type.getSimpleName() + ".ini"; IniFile config = ResourceConfig.combinedIniFile(name); // parse files ImmutableList<ExtendedEnum<? extends R>> children = parseChildren(config, type); log.fine(() -> "Loaded combined extended enum: " + name + ", providers: " + children); return new CombinedExtendedEnum<>(type, children); } catch (RuntimeException ex) { // logging used because this is loaded in a static variable log.severe("Failed to load CombinedExtendedEnum for " + type + ": " + Throwables.getStackTraceAsString(ex)); // return an empty instance to avoid ExceptionInInitializerError return new CombinedExtendedEnum<>(type, ImmutableList.of()); } }
public void test_lookup() { assertEquals(UberNamed.of("Standard"), SampleNameds.STANDARD); assertEquals(UberNamed.of("More"), MoreSampleNameds.MORE); CombinedExtendedEnum<UberNamed> combined = CombinedExtendedEnum.of(UberNamed.class); assertEquals(combined.find("Rubbish"), Optional.empty()); assertThrows(IllegalArgumentException.class, () -> combined.lookup("Rubbish")); assertEquals(combined.toString(), "CombinedExtendedEnum[UberNamed]"); }
/** * Obtains an instance from the specified unique name. * * @param uniqueName the unique name * @return the index * @throws IllegalArgumentException if the name is not known */ @FromString public static Index of(String uniqueName) { ArgChecker.notNull(uniqueName, "uniqueName"); return Indices.ENUM_LOOKUP.lookup(uniqueName); }