/** * Looks up an instance by name and type. * <p> * This finds the instance matching the specified name, ensuring it is of the specified type. * Instances may have alternate names (aliases), thus the returned instance * may have a name other than that requested. * * @param <S> the enum subtype * @param subtype the enum subtype to match * @param name the enum name to return * @return the named enum * @throws IllegalArgumentException if the name is not found or has the wrong type */ public <S extends T> S lookup(String name, Class<S> subtype) { T result = lookup(name); if (!subtype.isInstance(result)) { throw new IllegalArgumentException(Messages.format( "{}:{} external name found but did not match expected type: {}", extendedEnum.type.getSimpleName(), group, name)); } return subtype.cast(result); }
/** * Returns the mapping of external names to standard names for a group. * <p> * External names are used to map names used by external systems to the standard name used here. * There can be multiple groups of mappings to external systems, * For example, the mapping used by FpML may differ from that used by Bloomberg. * <p> * The result provides mapping between the external name and the standard name. * * @param group the group name to find external names for * @return the map of external names for the group * @throws IllegalArgumentException if the group is not found */ public ExternalEnumNames<T> externalNames(String group) { ImmutableMap<String, String> externals = externalNames.get(group); if (externals == null) { throw new IllegalArgumentException(type.getSimpleName() + " group not found: " + group); } return new ExternalEnumNames<>(this, group, externals); }
public void test_enum_SampleNamed_externals() { ExtendedEnum<SampleNamed> test = ExtendedEnum.of(SampleNamed.class); assertEquals(test.externalNameGroups(), ImmutableSet.of("Foo", "Bar")); assertThrowsIllegalArg(() -> test.externalNames("Rubbish")); ExternalEnumNames<SampleNamed> fooExternals = test.externalNames("Foo"); assertEquals(fooExternals.lookup("Foo1"), SampleNameds.STANDARD); assertEquals(fooExternals.lookup("Foo1", SampleNamed.class), SampleNameds.STANDARD); assertEquals(fooExternals.lookup("Foo1", SampleNamed.class), SampleNameds.STANDARD); assertEquals(fooExternals.externalNames(), ImmutableMap.of("Foo1", "Standard")); assertThrowsIllegalArg(() -> fooExternals.lookup("Rubbish")); assertThrowsIllegalArg(() -> fooExternals.lookup(null)); assertThrowsIllegalArg(() -> fooExternals.lookup("Other", MoreSampleNameds.class)); assertEquals(fooExternals.toString(), "ExternalEnumNames[SampleNamed:Foo]"); ExternalEnumNames<SampleNamed> barExternals = test.externalNames("Bar"); assertEquals(barExternals.lookup("Foo1"), MoreSampleNameds.MORE); assertEquals(barExternals.lookup("Foo2"), SampleNameds.STANDARD); assertEquals(barExternals.reverseLookup(MoreSampleNameds.MORE), "Foo1"); assertEquals(barExternals.reverseLookup(SampleNameds.STANDARD), "Foo2"); assertThrowsIllegalArg(() -> barExternals.reverseLookup(OtherSampleNameds.OTHER)); assertEquals(barExternals.externalNames(), ImmutableMap.of("Foo1", "More", "Foo2", "Standard")); assertEquals(barExternals.toString(), "ExternalEnumNames[SampleNamed:Bar]"); }
/** * Converts an FpML day count string to a {@code DayCount}. * * @param fpmlDayCountName the day count name used by FpML * @return the day count * @throws IllegalArgumentException if the day count is not known */ public DayCount convertDayCount(String fpmlDayCountName) { return DayCount.extendedEnum().externalNames(ENUM_FPML).lookup(fpmlDayCountName); }
/** * Converts an FpML roll convention string to a {@code RollConvention}. * * @param fmplRollConventionName the roll convention name used by FpML * @return the roll convention * @throws IllegalArgumentException if the roll convention is not known */ public RollConvention convertRollConvention(String fmplRollConventionName) { return RollConvention.extendedEnum().externalNames(ENUM_FPML).lookup(fmplRollConventionName); }
/** * Converts an FpML business day convention string to a {@code BusinessDayConvention}. * * @param fmplBusinessDayConventionName the business day convention name used by FpML * @return the business day convention * @throws IllegalArgumentException if the business day convention is not known */ public BusinessDayConvention convertBusinessDayConvention(String fmplBusinessDayConventionName) { return BusinessDayConvention.extendedEnum().externalNames(ENUM_FPML).lookup(fmplBusinessDayConventionName); }