2

I need to accomplish the following (this is a simplified version):

enum Animals{
 enum Cats{tabby("some value"), siamese("some value")},
 enum Dogs{poodle("some value"), dachsund("some value")},
 enum Birds{canary("some value"), parrot("some value")}

 private String someValue = "";

 private ShopByCategory(String someValue)
 {
     this.someValue = someValue;
 }

 public String getSomeValue()
 {
     return this.someValue;
 }
}

So that I can access these items as follows:

string cat1 = Animals.Cats.tabby.getSomeValue;
string dog1 = Animals.Dogs.dachsund.getSomeValue;
string bird1 = Animals.Birds.canary.getSomeValue;

The reason why I am attempting to do this with enums is the fact that I need to be able to access each tier without having to a) instantiate a class, b) hide the names of the tiers behind a method name, or c) use an iterator to go through an EnumSet.

Is this at all possible? What would you suggest instead of enums?

bynary
  • 841
  • 2
  • 11
  • 26

2 Answers2

1
//Animals.java
public class Animals {
    public static class Cats {
        public static final String tabby = "some value";
        public static final String siamese = "some value";
    }
    public static class Dogs {
        public static final String poodle = "some value";
        public static final String dachsund = "some value";
    }
    public static class Birds {
        public static final String canary = "some value";
        public static final String parrot = "some value";
    }
}

//ShopByCategory.java
public class ShopByCategory {
    private String someValue;
    public ShopByCategory(String value){
         this.someValue = value;
    }
    public String getSomeValue(){
        return this.someValue;
    }
}
//Main.java - an example of what you can do
public class Main {
    public static void main(String[] args){
         ShopByCategory sbc = new ShopByCategory(Animals.Birds.canary);
         System.out.println(sbc.getSomeValue());
         System.out.println(Animals.Dogs.poodle);
    }
}
Leo Izen
  • 4,165
  • 7
  • 37
  • 56
  • Interesting to see pre java 1.5 code. Why do this when enums are so much easier not to mention type safe? – Erik Feb 07 '11 at 20:09
1

Here's how I finally ended up implementing my solution:

    public static class Animals()
    {
      public enum Cats()
      {
        tabby("some value"),
        siamese("some value");

        private String someValue = "";

        private ShopByCategory(String someValue)
        {
          this.someValue = someValue;
        }

        public String getSomeValue()
        {
          return this.someValue;
        }
      }

      public enum Dogs()
      {
        poodle("some value"),
        dachsund("some value");

        private String someValue = "";

        private ShopByCategory(String someValue)
        {
          this.someValue = someValue;
        }

        public String getSomeValue()
        {
          return this.someValue;
        }
      }

      public enum Birds()
      {
        canary("some value"),
        parrot("some value");

        private String someValue = "";

        private ShopByCategory(String someValue)
        {
          this.someValue = someValue;
        }

        public String getSomeValue()
        {
          return this.someValue;
        }
      }

This way, I don't have to instantiate the class or call any class specific methods to get my desired info. I can get at all the "some value" strings like this:

string cat1 = Animals.Cats.tabby.getSomeValue;
string dog1 = Animals.Dogs.dachsund.getSomeValue;
string bird1 = Animals.Birds.canary.getSomeValue;
bynary
  • 841
  • 2
  • 11
  • 26