Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

Java how to check if an attribute is an enumeration?

How can I check if an attribute is an Enumeration in java. I know you can get the attribute type of an IAttribute with the getAttributeType() method, but I know the id of an enum can't always be used to check if the attribute is itself an enum. Is there a way to use server side code to make sure the attribute is an enum without using the attribute's id?

0 votes

Comments
The forum blocks all my attempts with illegal text string found.

AttributeTypes

Used in Work Item Command line

UpdateHelper



One answer

Permanent link

As Ralph has mentioned, the AttributeTypes class has a method to check if an attribute is an enum or enum list, in addition to checking if an attribute is of the other common attribute type. All you need is the attribute id, which can be retrieve from the IAtrribute class.


boolean isEnumeration(IAttribute attribute) {
        String typeId = attribute.getAttributeType();
     
        if(AttributeTypes.isEnumerationAttributeType(typeId) ||
            AttributeTypes.isEnumerationListAttributeType(typeId)) {
        
           return true;
         }
        return false;
}

The StringId could also be passed in if it is declared before the method is called.

boolean isEnumeration(String typeId) {
     
        if(AttributeTypes.isEnumerationAttributeType(typeId) ||
            AttributeTypes.isEnumerationListAttributeType(typeId)) {
        
           return true;
         }
        return false;
}

The cleaner way of writing both of these methods would be: 

boolean isEnumeration(IAttribute attribute) {
        String typeId = attribute.getAttributeType();
     
        return AttributeTypes.isEnumerationAttributeType(typeId) ||
            AttributeTypes.isEnumerationListAttributeType(typeId);  
}

boolean isEnumeration(String typeId) {
     
        return AttributeTypes.isEnumerationAttributeType(typeId) ||
            AttributeTypes.isEnumerationListAttributeType(typeId);  
}

The method would then be called like this.

if(isEnumeration(attribute)){
      Do something  or return true
} else { 
     Do something different for non enum attribute or return false
}

Alternate calling.

String typeId = attribute.getAttributeType();

if(isEnumeration(typeId)) {
      Do something  or return true
}

0 votes

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 11,102
× 7,619
× 1,449
× 91
× 5

Question asked: Apr 24, 6:48 p.m.

Question was seen: 1,110 times

Last updated: May 22, 11:31 a.m.

Confirmation Cancel Confirm