How to return Enum if matches with given String
I have Enum class as given below
public enum AlgorithmEnum {
SHA512("RSA", "SHA512", 1), SHA1("RSA", "SHA1", 1), SHA384("RSA", "SHA384", 1);
private String keyAlgorithm;
private String hashAlgorithm;
private Integer key;
private AlgorithmEnum(String keyAlgorithm, String hashAlgorithm, Integer key) {
this.keyAlgorithm = keyAlgorithm;
this.hashAlgorithm = hashAlgorithm;
this.key = key;
}
public String getKeyAlgorithm() {
return keyAlgorithm;
}
public void setKeyAlgorithm(String keyAlgorithm) {
this.keyAlgorithm = keyAlgorithm;
}
public String getHashAlgorithm() {
return hashAlgorithm;
}
public void setHashAlgorithm(String hashAlgorithm) {
this.hashAlgorithm = hashAlgorithm;
}
public Integer getKey() {
return key;
}
public void setKey(Integer key) {
this.key = key;
}
}
I need to have method something like below which takes input as string and returns Enum
public AlgorithmEnum getAlgorithm(String algorithm){
//returns AlgorithmEnum object
}
I would call above method by passing "SHA512withRSA" as input for getAlgorithm method.
I need help in implementing the getAlgorithm method.
java java-ee enums enumeration enumerate
add a comment |
I have Enum class as given below
public enum AlgorithmEnum {
SHA512("RSA", "SHA512", 1), SHA1("RSA", "SHA1", 1), SHA384("RSA", "SHA384", 1);
private String keyAlgorithm;
private String hashAlgorithm;
private Integer key;
private AlgorithmEnum(String keyAlgorithm, String hashAlgorithm, Integer key) {
this.keyAlgorithm = keyAlgorithm;
this.hashAlgorithm = hashAlgorithm;
this.key = key;
}
public String getKeyAlgorithm() {
return keyAlgorithm;
}
public void setKeyAlgorithm(String keyAlgorithm) {
this.keyAlgorithm = keyAlgorithm;
}
public String getHashAlgorithm() {
return hashAlgorithm;
}
public void setHashAlgorithm(String hashAlgorithm) {
this.hashAlgorithm = hashAlgorithm;
}
public Integer getKey() {
return key;
}
public void setKey(Integer key) {
this.key = key;
}
}
I need to have method something like below which takes input as string and returns Enum
public AlgorithmEnum getAlgorithm(String algorithm){
//returns AlgorithmEnum object
}
I would call above method by passing "SHA512withRSA" as input for getAlgorithm method.
I need help in implementing the getAlgorithm method.
java java-ee enums enumeration enumerate
add a getter and compare them
– Stultuske
Nov 20 '18 at 8:28
There is no enum constant defined withSHA512withRSA. How are you expecting it to return a value? Would all strings have a syntax endingwithRSA?
– Nicholas K
Nov 20 '18 at 8:30
Please add code you tried to do and failed before you actually posted the question on SO.
– Sarief
Nov 20 '18 at 8:40
add a comment |
I have Enum class as given below
public enum AlgorithmEnum {
SHA512("RSA", "SHA512", 1), SHA1("RSA", "SHA1", 1), SHA384("RSA", "SHA384", 1);
private String keyAlgorithm;
private String hashAlgorithm;
private Integer key;
private AlgorithmEnum(String keyAlgorithm, String hashAlgorithm, Integer key) {
this.keyAlgorithm = keyAlgorithm;
this.hashAlgorithm = hashAlgorithm;
this.key = key;
}
public String getKeyAlgorithm() {
return keyAlgorithm;
}
public void setKeyAlgorithm(String keyAlgorithm) {
this.keyAlgorithm = keyAlgorithm;
}
public String getHashAlgorithm() {
return hashAlgorithm;
}
public void setHashAlgorithm(String hashAlgorithm) {
this.hashAlgorithm = hashAlgorithm;
}
public Integer getKey() {
return key;
}
public void setKey(Integer key) {
this.key = key;
}
}
I need to have method something like below which takes input as string and returns Enum
public AlgorithmEnum getAlgorithm(String algorithm){
//returns AlgorithmEnum object
}
I would call above method by passing "SHA512withRSA" as input for getAlgorithm method.
I need help in implementing the getAlgorithm method.
java java-ee enums enumeration enumerate
I have Enum class as given below
public enum AlgorithmEnum {
SHA512("RSA", "SHA512", 1), SHA1("RSA", "SHA1", 1), SHA384("RSA", "SHA384", 1);
private String keyAlgorithm;
private String hashAlgorithm;
private Integer key;
private AlgorithmEnum(String keyAlgorithm, String hashAlgorithm, Integer key) {
this.keyAlgorithm = keyAlgorithm;
this.hashAlgorithm = hashAlgorithm;
this.key = key;
}
public String getKeyAlgorithm() {
return keyAlgorithm;
}
public void setKeyAlgorithm(String keyAlgorithm) {
this.keyAlgorithm = keyAlgorithm;
}
public String getHashAlgorithm() {
return hashAlgorithm;
}
public void setHashAlgorithm(String hashAlgorithm) {
this.hashAlgorithm = hashAlgorithm;
}
public Integer getKey() {
return key;
}
public void setKey(Integer key) {
this.key = key;
}
}
I need to have method something like below which takes input as string and returns Enum
public AlgorithmEnum getAlgorithm(String algorithm){
//returns AlgorithmEnum object
}
I would call above method by passing "SHA512withRSA" as input for getAlgorithm method.
I need help in implementing the getAlgorithm method.
java java-ee enums enumeration enumerate
java java-ee enums enumeration enumerate
edited Nov 20 '18 at 8:28
rocky
asked Nov 20 '18 at 8:27
rockyrocky
8810
8810
add a getter and compare them
– Stultuske
Nov 20 '18 at 8:28
There is no enum constant defined withSHA512withRSA. How are you expecting it to return a value? Would all strings have a syntax endingwithRSA?
– Nicholas K
Nov 20 '18 at 8:30
Please add code you tried to do and failed before you actually posted the question on SO.
– Sarief
Nov 20 '18 at 8:40
add a comment |
add a getter and compare them
– Stultuske
Nov 20 '18 at 8:28
There is no enum constant defined withSHA512withRSA. How are you expecting it to return a value? Would all strings have a syntax endingwithRSA?
– Nicholas K
Nov 20 '18 at 8:30
Please add code you tried to do and failed before you actually posted the question on SO.
– Sarief
Nov 20 '18 at 8:40
add a getter and compare them
– Stultuske
Nov 20 '18 at 8:28
add a getter and compare them
– Stultuske
Nov 20 '18 at 8:28
There is no enum constant defined with
SHA512withRSA. How are you expecting it to return a value? Would all strings have a syntax ending withRSA?– Nicholas K
Nov 20 '18 at 8:30
There is no enum constant defined with
SHA512withRSA. How are you expecting it to return a value? Would all strings have a syntax ending withRSA?– Nicholas K
Nov 20 '18 at 8:30
Please add code you tried to do and failed before you actually posted the question on SO.
– Sarief
Nov 20 '18 at 8:40
Please add code you tried to do and failed before you actually posted the question on SO.
– Sarief
Nov 20 '18 at 8:40
add a comment |
4 Answers
4
active
oldest
votes
You can have something like:
public static AlgorithmEnum getAlgorithm(final String algorithm)
throws IllegalArgumentException
{
for (final AlgorithmEnum algorithmEnum : AlgorithmEnum.values())
{
if (algorithm.equalsIgnoreCase(String.format("%swith%s", algorithmEnum.getHashAlgorithm(), algorithmEnum.getKeyAlgorithm())))
{
return algorithmEnum;
}
}
throw new IllegalArgumentException("Unknown algorithm: " + algorithm);
}
However, I will not suggest to use this approach. Instead use 2 different arguments instead of a single String.
add a comment |
Assuming all string values passed to your method getAlgorithm() end with withRSA you could use the following to fetch the enum values :
public AlgorithmEnum getAlgorithm(String algorithm) {
return AlgorithmEnum.valueOf(algorithm.substring(0, algorithm.indexOf("withRSA")));
}
add a comment |
You can check if the given String contains a value that matches one of the enum attributes with some if statements:
public AlgorithmEnum getAlgorithm(String algorithm) {
if (algorithm.contains("SHA1")) {
return SHA1;
} else if (algorithm.contains("SHA512")) {
return SHA512;
} else if (algorithm.contains("SHA384")) {
return SHA384;
} else {
return null;
}
}
Please note that this will match
Strings like"SHA512withoutRSA", too...
Maybe a method like
public AlgorithmEnum getAlgorithm(String keyAlgorithm, String hashAlgorithm)
would be better. However, you would have to provide two parameters then.
add a comment |
I'm leaving you an example of how I did on similar cases, you can easily adapt it to your needs:
private static Map<Integer, YourEnum> valuesById = new HashMap<>();
private static Map<String, YourEnum> valuesByCode = new HashMap<>();
static {
Arrays.stream(YourEnum.values()).forEach(value -> valuesById.put(value.reasonId, value));
Arrays.stream(YourEnum.values()).forEach(value -> valuesByCode.put(value.reasonCode, value));
}
public static YourEnum getByReasonId(int endReason) {
return valuesById.get(endReason);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388900%2fhow-to-return-enum-if-matches-with-given-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can have something like:
public static AlgorithmEnum getAlgorithm(final String algorithm)
throws IllegalArgumentException
{
for (final AlgorithmEnum algorithmEnum : AlgorithmEnum.values())
{
if (algorithm.equalsIgnoreCase(String.format("%swith%s", algorithmEnum.getHashAlgorithm(), algorithmEnum.getKeyAlgorithm())))
{
return algorithmEnum;
}
}
throw new IllegalArgumentException("Unknown algorithm: " + algorithm);
}
However, I will not suggest to use this approach. Instead use 2 different arguments instead of a single String.
add a comment |
You can have something like:
public static AlgorithmEnum getAlgorithm(final String algorithm)
throws IllegalArgumentException
{
for (final AlgorithmEnum algorithmEnum : AlgorithmEnum.values())
{
if (algorithm.equalsIgnoreCase(String.format("%swith%s", algorithmEnum.getHashAlgorithm(), algorithmEnum.getKeyAlgorithm())))
{
return algorithmEnum;
}
}
throw new IllegalArgumentException("Unknown algorithm: " + algorithm);
}
However, I will not suggest to use this approach. Instead use 2 different arguments instead of a single String.
add a comment |
You can have something like:
public static AlgorithmEnum getAlgorithm(final String algorithm)
throws IllegalArgumentException
{
for (final AlgorithmEnum algorithmEnum : AlgorithmEnum.values())
{
if (algorithm.equalsIgnoreCase(String.format("%swith%s", algorithmEnum.getHashAlgorithm(), algorithmEnum.getKeyAlgorithm())))
{
return algorithmEnum;
}
}
throw new IllegalArgumentException("Unknown algorithm: " + algorithm);
}
However, I will not suggest to use this approach. Instead use 2 different arguments instead of a single String.
You can have something like:
public static AlgorithmEnum getAlgorithm(final String algorithm)
throws IllegalArgumentException
{
for (final AlgorithmEnum algorithmEnum : AlgorithmEnum.values())
{
if (algorithm.equalsIgnoreCase(String.format("%swith%s", algorithmEnum.getHashAlgorithm(), algorithmEnum.getKeyAlgorithm())))
{
return algorithmEnum;
}
}
throw new IllegalArgumentException("Unknown algorithm: " + algorithm);
}
However, I will not suggest to use this approach. Instead use 2 different arguments instead of a single String.
answered Nov 20 '18 at 8:39
Karan KhannaKaran Khanna
633628
633628
add a comment |
add a comment |
Assuming all string values passed to your method getAlgorithm() end with withRSA you could use the following to fetch the enum values :
public AlgorithmEnum getAlgorithm(String algorithm) {
return AlgorithmEnum.valueOf(algorithm.substring(0, algorithm.indexOf("withRSA")));
}
add a comment |
Assuming all string values passed to your method getAlgorithm() end with withRSA you could use the following to fetch the enum values :
public AlgorithmEnum getAlgorithm(String algorithm) {
return AlgorithmEnum.valueOf(algorithm.substring(0, algorithm.indexOf("withRSA")));
}
add a comment |
Assuming all string values passed to your method getAlgorithm() end with withRSA you could use the following to fetch the enum values :
public AlgorithmEnum getAlgorithm(String algorithm) {
return AlgorithmEnum.valueOf(algorithm.substring(0, algorithm.indexOf("withRSA")));
}
Assuming all string values passed to your method getAlgorithm() end with withRSA you could use the following to fetch the enum values :
public AlgorithmEnum getAlgorithm(String algorithm) {
return AlgorithmEnum.valueOf(algorithm.substring(0, algorithm.indexOf("withRSA")));
}
edited Nov 20 '18 at 8:37
answered Nov 20 '18 at 8:36
Nicholas KNicholas K
7,29161435
7,29161435
add a comment |
add a comment |
You can check if the given String contains a value that matches one of the enum attributes with some if statements:
public AlgorithmEnum getAlgorithm(String algorithm) {
if (algorithm.contains("SHA1")) {
return SHA1;
} else if (algorithm.contains("SHA512")) {
return SHA512;
} else if (algorithm.contains("SHA384")) {
return SHA384;
} else {
return null;
}
}
Please note that this will match
Strings like"SHA512withoutRSA", too...
Maybe a method like
public AlgorithmEnum getAlgorithm(String keyAlgorithm, String hashAlgorithm)
would be better. However, you would have to provide two parameters then.
add a comment |
You can check if the given String contains a value that matches one of the enum attributes with some if statements:
public AlgorithmEnum getAlgorithm(String algorithm) {
if (algorithm.contains("SHA1")) {
return SHA1;
} else if (algorithm.contains("SHA512")) {
return SHA512;
} else if (algorithm.contains("SHA384")) {
return SHA384;
} else {
return null;
}
}
Please note that this will match
Strings like"SHA512withoutRSA", too...
Maybe a method like
public AlgorithmEnum getAlgorithm(String keyAlgorithm, String hashAlgorithm)
would be better. However, you would have to provide two parameters then.
add a comment |
You can check if the given String contains a value that matches one of the enum attributes with some if statements:
public AlgorithmEnum getAlgorithm(String algorithm) {
if (algorithm.contains("SHA1")) {
return SHA1;
} else if (algorithm.contains("SHA512")) {
return SHA512;
} else if (algorithm.contains("SHA384")) {
return SHA384;
} else {
return null;
}
}
Please note that this will match
Strings like"SHA512withoutRSA", too...
Maybe a method like
public AlgorithmEnum getAlgorithm(String keyAlgorithm, String hashAlgorithm)
would be better. However, you would have to provide two parameters then.
You can check if the given String contains a value that matches one of the enum attributes with some if statements:
public AlgorithmEnum getAlgorithm(String algorithm) {
if (algorithm.contains("SHA1")) {
return SHA1;
} else if (algorithm.contains("SHA512")) {
return SHA512;
} else if (algorithm.contains("SHA384")) {
return SHA384;
} else {
return null;
}
}
Please note that this will match
Strings like"SHA512withoutRSA", too...
Maybe a method like
public AlgorithmEnum getAlgorithm(String keyAlgorithm, String hashAlgorithm)
would be better. However, you would have to provide two parameters then.
answered Nov 20 '18 at 8:40
deHaardeHaar
2,48951628
2,48951628
add a comment |
add a comment |
I'm leaving you an example of how I did on similar cases, you can easily adapt it to your needs:
private static Map<Integer, YourEnum> valuesById = new HashMap<>();
private static Map<String, YourEnum> valuesByCode = new HashMap<>();
static {
Arrays.stream(YourEnum.values()).forEach(value -> valuesById.put(value.reasonId, value));
Arrays.stream(YourEnum.values()).forEach(value -> valuesByCode.put(value.reasonCode, value));
}
public static YourEnum getByReasonId(int endReason) {
return valuesById.get(endReason);
}
add a comment |
I'm leaving you an example of how I did on similar cases, you can easily adapt it to your needs:
private static Map<Integer, YourEnum> valuesById = new HashMap<>();
private static Map<String, YourEnum> valuesByCode = new HashMap<>();
static {
Arrays.stream(YourEnum.values()).forEach(value -> valuesById.put(value.reasonId, value));
Arrays.stream(YourEnum.values()).forEach(value -> valuesByCode.put(value.reasonCode, value));
}
public static YourEnum getByReasonId(int endReason) {
return valuesById.get(endReason);
}
add a comment |
I'm leaving you an example of how I did on similar cases, you can easily adapt it to your needs:
private static Map<Integer, YourEnum> valuesById = new HashMap<>();
private static Map<String, YourEnum> valuesByCode = new HashMap<>();
static {
Arrays.stream(YourEnum.values()).forEach(value -> valuesById.put(value.reasonId, value));
Arrays.stream(YourEnum.values()).forEach(value -> valuesByCode.put(value.reasonCode, value));
}
public static YourEnum getByReasonId(int endReason) {
return valuesById.get(endReason);
}
I'm leaving you an example of how I did on similar cases, you can easily adapt it to your needs:
private static Map<Integer, YourEnum> valuesById = new HashMap<>();
private static Map<String, YourEnum> valuesByCode = new HashMap<>();
static {
Arrays.stream(YourEnum.values()).forEach(value -> valuesById.put(value.reasonId, value));
Arrays.stream(YourEnum.values()).forEach(value -> valuesByCode.put(value.reasonCode, value));
}
public static YourEnum getByReasonId(int endReason) {
return valuesById.get(endReason);
}
answered Nov 20 '18 at 8:41
Andrea CalinAndrea Calin
1889
1889
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388900%2fhow-to-return-enum-if-matches-with-given-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
add a getter and compare them
– Stultuske
Nov 20 '18 at 8:28
There is no enum constant defined with
SHA512withRSA. How are you expecting it to return a value? Would all strings have a syntax endingwithRSA?– Nicholas K
Nov 20 '18 at 8:30
Please add code you tried to do and failed before you actually posted the question on SO.
– Sarief
Nov 20 '18 at 8:40