8

I'd like to write the following:

public enum OrderType
{
    Coop = 0,
    Normal = 1,
    Package = 2,
    SubInvoicing = 3,
}

It doesn't seem like it's supported. Or am I missing something?

2 Answers 2

12

Unfortunately not, but as long as you're happy with not being able to define the numbers yourself, and 0 based counting is good enough then you can use the enum method ordinal() which lets you know the index of the enum value you're currently referencing.

So if you declared your enum like so:

public enum OrderType
{
    Coop,
    Normal,
    Package,
    SubInvoicing
}

Then you calling OrderType.Normal.ordinal() would return an integer with value 1. Of course if you do need to specify numbers a somewhat hacky way would be to add placeholder entries within your list, but depending on the numbers you need that could get a bit crazy. Another alternative might be a public map:

public class OrderTypeMap
{
  private static Map<String, Integer> typeMap = new Map<String, Integer>
  {
    'Coop' => 0,
    'Normal' => 1,
    'Package' => 2,
    'SubInvoicing' => 3
  };

  public Integer Value(String s)
  {
    return typeMap.get(s);
  }
}

You could even combine that with an enum (if you really want the enum rather than a string) and use the name() enum method to pass into the map:

Integer i = OrderTypeMap.Value(OrderType.Normal.name());
2
  • 1
    Thanks. I was hoping I was doing something wrong. So there is no way to have an enum with bitwise flags there then? Commented Jul 24, 2013 at 17:48
  • Not really — I miss those myself coming from a C background!
    – Matt Lacey
    Commented Jul 25, 2013 at 0:33
1

As near as I can tell the best you can do is:

public class Foo {
    public enum OrderType
    {
        Coop,
        Normal,
        Package,
        SubInvoicing
    }

    public Map<OrderType,Integer> OrderTypeMap = ( new Map<OrderType,Integer>{
        Coop => 1,
        Normal => 2,
        Package => 3,
        SubInvoicing => 4 } );

...
}

Note: The parenthesis are just so the developer console will not mangle the indenting too badly.

I know this is not the solution you were looking for, but it seems like the simplest one. More complicated is you define a class instead:

public class Foo {
    public static OrderTypeEnum OrderType = new OrderTypeEnum();

    public class OrderTypeEnum
    {
        public Integer Coop { get { return 1; } }
        public Integer Normal { get { return 2; } }
        public Integer Package { get { return 3; } }
        public Integer SubInvoicing { get { return 4; } }
    }

...
}

Neither does exactly what you want, but both come close. In the first example, you can do things like:

Foo.OrderType ot = Foo.OrderType.Coop;
if(Foo.OrderTypeMap.get(ot) == 1)
...

In the second:

Integer ot = Foo.OrderType.Coop;
if(ot == Foo.OrderType.Coop)
...

Neither is doing exactly what you want, but they are close to what you want.

Even closer, but even more complicated:

public class Foo {
    public static OrderTypeEnum OrderType = new OrderTypeEnum(null);

    public class OrderTypeEnum
    {
        final Integer ordinal;
        public OrderTypeEnum(Integer ordinal) { this.ordinal = ordinal; }
        public OrderTypeEnum Coop { get { return new OrderTypeEnum(1); } }
        public OrderTypeEnum Normal { get { return new OrderTypeEnum(2); } }
        public OrderTypeEnum Package { get { return new OrderTypeEnum(3); } }
        public OrderTypeEnum SubInvoicing { get { return new OrderTypEnum(4); } }
        public Boolean equals(Object that) {
            return (that instanceof OrderTypeEnum)?((OrderTypeEnum)that.oridinal == ordinal):(that == ordinal);
        }
        public Integer hashCode() {return ordinal;}
        public Integer ordinal() {return ordinal;}
    }

...
}

You can now treat this almost like an Enum. e.g.

Foo.OrderTypeEnum ot = Foo.OrderType.Coop;
if(ot.ordinal() == 1)
...
if(ot.equals(1))
...
if(ot.equals(Foo.OrderType.Coop))
...

A few things won't work though, like using these in switch statements. Care needs to be taken when using the equality operator, ==. Because ot == 1 will work, but 1 == ot will not.

Really it is just an issue how complicated are you willing to get to replicate the usage scenario you have in mind.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .