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?













0















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?










share|improve this question
























  • 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















0















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?










share|improve this question
























  • 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













0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 14 hours ago







Erwann

















asked 15 hours ago









ErwannErwann

50111




50111












  • 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























  • 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










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
);



);













draft saved

draft discarded


















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









1














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.






share|improve this answer























  • 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















1














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.






share|improve this answer























  • 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













1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?