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?
One answer
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
}
Comments
Ralph Schoon
Apr 27, 11:43 a.m.Ralph Schoon
May 05, 11:21 a.m.AttributeTypes
Ralph Schoon
May 05, 11:21 a.m.Used in Work Item Command line
Ralph Schoon
May 05, 11:23 a.m.UpdateHelper