views:

76

answers:

3

I have an Enum in .Net. How can I rewrite this Enum in java?

Here is the Enum:

public enum AdCategoryType : short
{
    ForSale = 1,
    ForBuy = 2,
    ForRent = 8,
    WantingForRent = 16,
    WorkIsWanted = 32, 
    WorkIsGiven = 64
}
+2  A: 

This gets you the enum:

public enum AdCategoryType
{
    private final int value;

    ForSale(1),
    ForBuy(2),
    ForRent(4),
    WantingForRent(8),
    WorkIsWanted(16),
    WorkIsGiven(32);

    public AdCategoryType(value) { this.value = value; }

    public int getValue() { return this.value; }
}
duffymo
A: 
public enum Foo
{
   Bar(1),
   Baz(45);

   private short myValue;

   private short value()
   {
     return myValue;
   }

   public Foo(short val)
   {
      myValue = val;
   }

}

Burleigh Bear
+1  A: 

This will work:

public enum AdCategoryType {
    ForSale/*           */ (1 << 0), //
    ForBuy/*            */ (1 << 1), //
    ForRent/*           */ (1 << 2), //
    WantingForRent/*    */ (1 << 3), //
    WorkIsWanted/*      */ (1 << 4), //
    WorkIsGiven/*       */ (1 << 5);
    private final int value;

    private AdCategoryType(int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }
};

To get the value of ForBuy use AdCategoryType.ForBuy.getValue().

Margus
+1 - I like your use of the left shift operator.
duffymo