Skip to content

Enhance naming of enums - #1532

Merged
jodastephen merged 2 commits into
masterfrom
topic/enum-names
Jul 24, 2017
Merged

jodastephen merged 2 commits into
masterfrom
topic/enum-names

Conversation

@jodastephen

Copy link
Copy Markdown
Contributor

Add NamedEnum interface, to mark standard enums
Add EnumNames to ensure more lenient and consistent parsing
Enhanced performance of parse and format

Add `NamedEnum` interface, to mark standard enums
Add `EnumNames` to ensure more lenient and consistent parsing
Enhanced performance of parse and format

@brianweller89 brianweller89 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine - assuming the loss of custom parsing logic in the two highlighted enums is expected and known


// restricted constructor
private EnumNames(Class<T> enumType, boolean specialToString) {
ArgChecker.notNull(enumType, "enumType");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use consistent naming when referring to the overridden toString() functionality; currently using "manualToString" in the method name and "specialToString" as the variable name (overriddenToString another option)

map.put(formatted.toUpperCase(Locale.ENGLISH), value);
map.put(formatted.toLowerCase(Locale.ENGLISH), value);
formattedSet.add(formatted);
formatMap.put(value, formatted);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move these 5 duplicate lines outside of the if/else? Declaring the String outside the if un-itilialised may look abit nasty but IMO in simple cases like this it is better than carrying the risk of having somebody updating one block of code whilst forgetting to update the duplicate.

* @return the enum value
*/
public T parse(String name) {
ArgChecker.notNull(name, "name");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NotEmpty would be a tighter check here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but this way users get a better error message

String str = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, uniqueName);
if (str.endsWith("I_S_D_A")) {
str = "ORIGINAL_ISDA";
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assume we are happy for this endsWith logic to be completely removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a hack that is now supported properly.

ArgChecker.notNull(uniqueName, "uniqueName");
return valueOf(uniqueName.replace('-', '_').replace("/", "").toUpperCase(Locale.ENGLISH));
public static FixedCouponBondYieldConvention of(String name) {
return NAMES.parse(name);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again we're losing some custom parsing logic here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a hack that is now supported properly.

@jodastephen
jodastephen merged commit 85c814f into master Jul 24, 2017
@jodastephen
jodastephen deleted the topic/enum-names branch July 24, 2017 11:59
@jodastephen jodastephen modified the milestone: v1.4 Jul 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants