How to check against generic enum in Java?Cast int to enum in C#Is Java “pass-by-reference” or “pass-by-value”?Create Generic method constraining T to an EnumHow do I enumerate an enum in C#?What is the preferred syntax for defining enums in JavaScript?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How to get an enum value from a string value in Java?Comparing Java enum members: == or equals()?How do I convert a String to an int in Java?
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
Doesn't the system of the Supreme Court oppose justice?
Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?
Delete multiple columns using awk or sed
Which was the first story featuring espers?
Is there a RAID 0 Equivalent for RAM?
Creating two special characters
Microchip documentation does not label CAN buss pins on micro controller pinout diagram
What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?
Did the UK lift the requirement for registering SIM cards?
Has any country ever had 2 former presidents in jail simultaneously?
What does Apple's new App Store requirement mean
Shouldn’t conservatives embrace universal basic income?
What is the difference between lands and mana?
The Digit Triangles
Does Doodling or Improvising on the Piano Have Any Benefits?
"It doesn't matter" or "it won't matter"?
C++ copy constructor called at return
What is going on with gets(stdin) on the site coderbyte?
What is the highest possible scrabble score for placing a single tile
What to do when eye contact makes your coworker uncomfortable?
Why do ¬, ∀ and ∃ have the same precedence?
What is Cash Advance APR?
Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?
How to check against generic enum in Java?
Cast int to enum in C#Is Java “pass-by-reference” or “pass-by-value”?Create Generic method constraining T to an EnumHow do I enumerate an enum in C#?What is the preferred syntax for defining enums in JavaScript?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How to get an enum value from a string value in Java?Comparing Java enum members: == or equals()?How do I convert a String to an int in Java?
Here is my code:
public enum DecisionType
REFUSAL,
GRANT_OF_PROTECTION,
PARTIAL_REFUSAL;
public class DocumentComposition<T extends Enum<DecisionType>> extends TreeMap<DocumentType, Object>
@Override
public Object put(DocumentType key, Object value)
if (key.getDecisionType() != )
return null;
return value;
DocumentComposition map = new DocumentComposition<DecisionType.REFUSAL>();
I need my Map to contain only elements that are of a certain value of the DecisionType enum. How do I achieve this? What should my test look like?
java generics enums
add a comment |
Here is my code:
public enum DecisionType
REFUSAL,
GRANT_OF_PROTECTION,
PARTIAL_REFUSAL;
public class DocumentComposition<T extends Enum<DecisionType>> extends TreeMap<DocumentType, Object>
@Override
public Object put(DocumentType key, Object value)
if (key.getDecisionType() != )
return null;
return value;
DocumentComposition map = new DocumentComposition<DecisionType.REFUSAL>();
I need my Map to contain only elements that are of a certain value of the DecisionType enum. How do I achieve this? What should my test look like?
java generics enums
Why don't youextends TreeMap<DocumentType, T>
?
– daniu
15 hours ago
1
Why DO you extend TreeMap? If you want your Map "to contain only elements that are of a certain value of the DecisionType enum", just check their types with 'If' statement before putting on a map and decide whether you want to put it or not.
– Pavel Smirnov
15 hours ago
You could add a property to the enum as well but it's hard to now without understanding the logic behind the enum and the filtering.
– Joakim Danielson
15 hours ago
add a comment |
Here is my code:
public enum DecisionType
REFUSAL,
GRANT_OF_PROTECTION,
PARTIAL_REFUSAL;
public class DocumentComposition<T extends Enum<DecisionType>> extends TreeMap<DocumentType, Object>
@Override
public Object put(DocumentType key, Object value)
if (key.getDecisionType() != )
return null;
return value;
DocumentComposition map = new DocumentComposition<DecisionType.REFUSAL>();
I need my Map to contain only elements that are of a certain value of the DecisionType enum. How do I achieve this? What should my test look like?
java generics enums
Here is my code:
public enum DecisionType
REFUSAL,
GRANT_OF_PROTECTION,
PARTIAL_REFUSAL;
public class DocumentComposition<T extends Enum<DecisionType>> extends TreeMap<DocumentType, Object>
@Override
public Object put(DocumentType key, Object value)
if (key.getDecisionType() != )
return null;
return value;
DocumentComposition map = new DocumentComposition<DecisionType.REFUSAL>();
I need my Map to contain only elements that are of a certain value of the DecisionType enum. How do I achieve this? What should my test look like?
java generics enums
java generics enums
edited 14 hours ago
Erwann
asked 15 hours ago
ErwannErwann
50111
50111
Why don't youextends TreeMap<DocumentType, T>
?
– daniu
15 hours ago
1
Why DO you extend TreeMap? If you want your Map "to contain only elements that are of a certain value of the DecisionType enum", just check their types with 'If' statement before putting on a map and decide whether you want to put it or not.
– Pavel Smirnov
15 hours ago
You could add a property to the enum as well but it's hard to now without understanding the logic behind the enum and the filtering.
– Joakim Danielson
15 hours ago
add a comment |
Why don't youextends TreeMap<DocumentType, T>
?
– daniu
15 hours ago
1
Why DO you extend TreeMap? If you want your Map "to contain only elements that are of a certain value of the DecisionType enum", just check their types with 'If' statement before putting on a map and decide whether you want to put it or not.
– Pavel Smirnov
15 hours ago
You could add a property to the enum as well but it's hard to now without understanding the logic behind the enum and the filtering.
– Joakim Danielson
15 hours ago
Why don't you
extends TreeMap<DocumentType, T>
?– daniu
15 hours ago
Why don't you
extends TreeMap<DocumentType, T>
?– daniu
15 hours ago
1
1
Why DO you extend TreeMap? If you want your Map "to contain only elements that are of a certain value of the DecisionType enum", just check their types with 'If' statement before putting on a map and decide whether you want to put it or not.
– Pavel Smirnov
15 hours ago
Why DO you extend TreeMap? If you want your Map "to contain only elements that are of a certain value of the DecisionType enum", just check their types with 'If' statement before putting on a map and decide whether you want to put it or not.
– Pavel Smirnov
15 hours ago
You could add a property to the enum as well but it's hard to now without understanding the logic behind the enum and the filtering.
– Joakim Danielson
15 hours ago
You could add a property to the enum as well but it's hard to now without understanding the logic behind the enum and the filtering.
– Joakim Danielson
15 hours ago
add a comment |
1 Answer
1
active
oldest
votes
Do I understand it right you want to have a DocumentComposition which accepts only DocumentType instances of a specific DecisionType ?
My parts of the solution:
- You don't need to use generics for that but rather an internal variable which you provide in the constructor.
In you overridden put method you must not forget to call the super otherwise your TreeMap will never get any elements.
public class DocumentComposition extends TreeMap<DocumentType, Object>
private DecisionType acceptedDecisionType;
public DocumentComposition(DecisionType acceptedDecisionType)
this.acceptedDecisionType = acceptedDecisionType;
@Override
public Object put(DocumentType key, Object value)
if (key.getDecisionType() != acceptedDecisionType)
return null;
return super.put(key, value); // do not forget to call super, otherwise your TreeMap is not filled
Now you can use your map:
public static void main( String args[])
DocumentComposition dc=new DocumentComposition(DecisionType.REFUSAL);
dc.put(new DocumentType(DecisionType.REFUSAL), "refusalDoc");
dc.put(new DocumentType(DecisionType.PARTIAL_REFUSAL), "partialRefusalDoc");
System.out.println(dc);
Only refusalDoc will be in the map.
Thank you for taking the trouble of helping here. You're right of course, this is a simple way to handle this situation. I just thought, perhaps, there was an elegant way of doing it with generics... Somehow, from a design point of view, it was the first thing that popped into my mind.
– Erwann
13 hours ago
I'm not sure if generics can be used here as you want to specify an enum value and not a type as generic parameter.
– Conffusion
12 hours ago
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%2f55280087%2fhow-to-check-against-generic-enum-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Do I understand it right you want to have a DocumentComposition which accepts only DocumentType instances of a specific DecisionType ?
My parts of the solution:
- You don't need to use generics for that but rather an internal variable which you provide in the constructor.
In you overridden put method you must not forget to call the super otherwise your TreeMap will never get any elements.
public class DocumentComposition extends TreeMap<DocumentType, Object>
private DecisionType acceptedDecisionType;
public DocumentComposition(DecisionType acceptedDecisionType)
this.acceptedDecisionType = acceptedDecisionType;
@Override
public Object put(DocumentType key, Object value)
if (key.getDecisionType() != acceptedDecisionType)
return null;
return super.put(key, value); // do not forget to call super, otherwise your TreeMap is not filled
Now you can use your map:
public static void main( String args[])
DocumentComposition dc=new DocumentComposition(DecisionType.REFUSAL);
dc.put(new DocumentType(DecisionType.REFUSAL), "refusalDoc");
dc.put(new DocumentType(DecisionType.PARTIAL_REFUSAL), "partialRefusalDoc");
System.out.println(dc);
Only refusalDoc will be in the map.
Thank you for taking the trouble of helping here. You're right of course, this is a simple way to handle this situation. I just thought, perhaps, there was an elegant way of doing it with generics... Somehow, from a design point of view, it was the first thing that popped into my mind.
– Erwann
13 hours ago
I'm not sure if generics can be used here as you want to specify an enum value and not a type as generic parameter.
– Conffusion
12 hours ago
add a comment |
Do I understand it right you want to have a DocumentComposition which accepts only DocumentType instances of a specific DecisionType ?
My parts of the solution:
- You don't need to use generics for that but rather an internal variable which you provide in the constructor.
In you overridden put method you must not forget to call the super otherwise your TreeMap will never get any elements.
public class DocumentComposition extends TreeMap<DocumentType, Object>
private DecisionType acceptedDecisionType;
public DocumentComposition(DecisionType acceptedDecisionType)
this.acceptedDecisionType = acceptedDecisionType;
@Override
public Object put(DocumentType key, Object value)
if (key.getDecisionType() != acceptedDecisionType)
return null;
return super.put(key, value); // do not forget to call super, otherwise your TreeMap is not filled
Now you can use your map:
public static void main( String args[])
DocumentComposition dc=new DocumentComposition(DecisionType.REFUSAL);
dc.put(new DocumentType(DecisionType.REFUSAL), "refusalDoc");
dc.put(new DocumentType(DecisionType.PARTIAL_REFUSAL), "partialRefusalDoc");
System.out.println(dc);
Only refusalDoc will be in the map.
Thank you for taking the trouble of helping here. You're right of course, this is a simple way to handle this situation. I just thought, perhaps, there was an elegant way of doing it with generics... Somehow, from a design point of view, it was the first thing that popped into my mind.
– Erwann
13 hours ago
I'm not sure if generics can be used here as you want to specify an enum value and not a type as generic parameter.
– Conffusion
12 hours ago
add a comment |
Do I understand it right you want to have a DocumentComposition which accepts only DocumentType instances of a specific DecisionType ?
My parts of the solution:
- You don't need to use generics for that but rather an internal variable which you provide in the constructor.
In you overridden put method you must not forget to call the super otherwise your TreeMap will never get any elements.
public class DocumentComposition extends TreeMap<DocumentType, Object>
private DecisionType acceptedDecisionType;
public DocumentComposition(DecisionType acceptedDecisionType)
this.acceptedDecisionType = acceptedDecisionType;
@Override
public Object put(DocumentType key, Object value)
if (key.getDecisionType() != acceptedDecisionType)
return null;
return super.put(key, value); // do not forget to call super, otherwise your TreeMap is not filled
Now you can use your map:
public static void main( String args[])
DocumentComposition dc=new DocumentComposition(DecisionType.REFUSAL);
dc.put(new DocumentType(DecisionType.REFUSAL), "refusalDoc");
dc.put(new DocumentType(DecisionType.PARTIAL_REFUSAL), "partialRefusalDoc");
System.out.println(dc);
Only refusalDoc will be in the map.
Do I understand it right you want to have a DocumentComposition which accepts only DocumentType instances of a specific DecisionType ?
My parts of the solution:
- You don't need to use generics for that but rather an internal variable which you provide in the constructor.
In you overridden put method you must not forget to call the super otherwise your TreeMap will never get any elements.
public class DocumentComposition extends TreeMap<DocumentType, Object>
private DecisionType acceptedDecisionType;
public DocumentComposition(DecisionType acceptedDecisionType)
this.acceptedDecisionType = acceptedDecisionType;
@Override
public Object put(DocumentType key, Object value)
if (key.getDecisionType() != acceptedDecisionType)
return null;
return super.put(key, value); // do not forget to call super, otherwise your TreeMap is not filled
Now you can use your map:
public static void main( String args[])
DocumentComposition dc=new DocumentComposition(DecisionType.REFUSAL);
dc.put(new DocumentType(DecisionType.REFUSAL), "refusalDoc");
dc.put(new DocumentType(DecisionType.PARTIAL_REFUSAL), "partialRefusalDoc");
System.out.println(dc);
Only refusalDoc will be in the map.
answered 14 hours ago
ConffusionConffusion
1,5021715
1,5021715
Thank you for taking the trouble of helping here. You're right of course, this is a simple way to handle this situation. I just thought, perhaps, there was an elegant way of doing it with generics... Somehow, from a design point of view, it was the first thing that popped into my mind.
– Erwann
13 hours ago
I'm not sure if generics can be used here as you want to specify an enum value and not a type as generic parameter.
– Conffusion
12 hours ago
add a comment |
Thank you for taking the trouble of helping here. You're right of course, this is a simple way to handle this situation. I just thought, perhaps, there was an elegant way of doing it with generics... Somehow, from a design point of view, it was the first thing that popped into my mind.
– Erwann
13 hours ago
I'm not sure if generics can be used here as you want to specify an enum value and not a type as generic parameter.
– Conffusion
12 hours ago
Thank you for taking the trouble of helping here. You're right of course, this is a simple way to handle this situation. I just thought, perhaps, there was an elegant way of doing it with generics... Somehow, from a design point of view, it was the first thing that popped into my mind.
– Erwann
13 hours ago
Thank you for taking the trouble of helping here. You're right of course, this is a simple way to handle this situation. I just thought, perhaps, there was an elegant way of doing it with generics... Somehow, from a design point of view, it was the first thing that popped into my mind.
– Erwann
13 hours ago
I'm not sure if generics can be used here as you want to specify an enum value and not a type as generic parameter.
– Conffusion
12 hours ago
I'm not sure if generics can be used here as you want to specify an enum value and not a type as generic parameter.
– Conffusion
12 hours ago
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%2f55280087%2fhow-to-check-against-generic-enum-in-java%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
Why don't you
extends TreeMap<DocumentType, T>
?– daniu
15 hours ago
1
Why DO you extend TreeMap? If you want your Map "to contain only elements that are of a certain value of the DecisionType enum", just check their types with 'If' statement before putting on a map and decide whether you want to put it or not.
– Pavel Smirnov
15 hours ago
You could add a property to the enum as well but it's hard to now without understanding the logic behind the enum and the filtering.
– Joakim Danielson
15 hours ago