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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript