Enum Alternative Java Example
In Java Enum type has been introduced, which is a special data type it enables for variable to be a set of predefined constants. The variable should be equal to one of the values that have been predefined for it. But here we are will create Enum but will discuss about alternative of Enum. This was required to me when I was working with Oracle Coherence which does not support Enum types so I had to take an alternative approach to define my constant.
- Let’s say you have Enum called ExpirySettlementType.java and you need to take alternative of this:
public enum ExpirySettlementType { Cash(70,""), Physical(71, "Net Share"), NetShare(72, ""), Undetermd (3, "Undetermined"), None (0, ""); private final int value; private final String description; ExpirySettlementType(int value, String description) { this.value = value; this.description = description; } public int getValue() { return value; } public String getDescription() { return description; } }
- Enum alternative EnumAlternativeConstant.java:
public class EnumAlternativeConstant { public static final EnumAlternativeConstant Cash = new EnumAlternativeConstant(70,"Cash"); public static final EnumAlternativeConstant Physical = new EnumAlternativeConstant(70,"Physical"); public static final EnumAlternativeConstant NetShare = new EnumAlternativeConstant(70,"Net Share"); public static final EnumAlternativeConstant Undetermd = new EnumAlternativeConstant(70,"Undetermd"); public static final EnumAlternativeConstant None = new EnumAlternativeConstant(70,"None"); private final int value; private final String description; EnumAlternativeConstant(int value, String description) { this.value = value; this.description = description; } public int getValue() { return value; } public String getDescription() { return description; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((description == null) ? 0 : description.hashCode()); result = prime * result + value; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; EnumAlternativeConstant other = (EnumAlternativeConstant) obj; if (description == null) { if (other.description != null) return false; } else if (!description.equals(other.description)) return false; if (value != other.value) return false; return true; } @Override public String toString() { return "EnumAlternativeConstant [value=" + value + ", description=" + description + "]"; } }
As you see above I have created Enum alternative constant class. Now Let’s create test class to pass and retrieve its value:
- EnumAlternativeTest.java:
public class EnumAlternativeTest{ private EnumAlternativeConstant enumAlternativeConstant; public static void main(String[] args) { EnumAlternativeTest classTest = new EnumAlternativeTest(); classTest.setValue(EnumAlternativeConstant.NetShare); System.out.println("Value: "+classTest.getValue().getValue()+" Description: "+classTest.getValue().getDescription()); } public EnumAlternativeConstant getValue() { return enumAlternativeConstant; } public void setValue(EnumAlternativeConstant enumAlternativeConstant) { this.enumAlternativeConstant = enumAlternativeConstant; } }
Above example is Enum with two variable constant and If you are looking for Enum with single constant variable please see example below:
- Enum AnchorType.java;
public enum AnchorType { Payment, Reset }
- Enum alternative AnchorTypeConstant.java:
public class AnchorTypeConstant { public static final AnchorTypeConstant Payment = new AnchorTypeConstant("Cash"); public static final AnchorTypeConstant Reset = new AnchorTypeConstant("Reset"); private final String name; public AnchorTypeConstant(String name) { this.name = name; } public String getName() { return name; } }
- OR you could use below if you don’t want to use constructor:
public class AnchorTypeConstant { public static final String Payment = "Cash"; public static final String Reset = "Reset"; }
Reference: