Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix language code -> enum conversion #345

Merged
merged 3 commits into from
Apr 3, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

package com.amplifyframework.predictions.models;

import java.util.HashMap;
import java.util.Map;

/**
* Enum of different types of language. These are the languages
* that are recognized and supported by Amazon Translate Service
Expand Down Expand Up @@ -131,8 +134,18 @@ public enum LanguageType {
YORUBA("yo"),
UNKNOWN("unknown");

private static final Map<String, LanguageType> CODES;

private final String languageCode;

// Reverse look-up table
static {
CODES = new HashMap<>();
for (LanguageType language : LanguageType.values()) {
CODES.put(language.getLanguageCode(), language);
}
}

LanguageType(String languageCode) {
this.languageCode = languageCode;
}
Expand All @@ -145,11 +158,10 @@ public enum LanguageType {
* @return An enum value of matching language code
*/
public static LanguageType from(String languageCode) {
raphkim marked this conversation as resolved.
Show resolved Hide resolved
try {
return LanguageType.valueOf(languageCode);
} catch (IllegalArgumentException noMatchError) {
if (!CODES.containsKey(languageCode)) {
return UNKNOWN;
}
return CODES.get(languageCode);
}

/**
Expand Down