Solved

Interface Run Time Identification

Posted on 2009-05-19
14
187 Views
Last Modified: 2012-05-07
Hi Experts,

I have one interface as below:

public interface Constant {
    public enum Info {
        INFO1(4),
        INFO2(5),
        INFO3(6),
        INFO4(7),
        INFO5(8),
        INFO6(9),
        INFO7(10);
       
        private int index;

        Info(int index) {
            this.index = index;
        }
    }
}

I have a class ConstantUtil which implements the Constants interface with a method to return the Enum value of the given index.

public class ConstantUtil implements Constants
{
      public static Constants.Info getConstant (int index)
      {
            return Enum.valueOf(Constants.Info.class, "INFO" + index);
      }
}

Now, I have a requirement as below:

I have different type of Constants like ConstantA, ConstantB, etc. with the same Enum declarations but the INFO index will be different. For example INFO1(4) will be like INFO1(9) and so on.

I want to have one common util class with a method to return the corresponding Constants value.

The getConstant method must return the value from the corresponding Constants interface.

How can I do this?

Thanks,
Rameshbabu Arjuna Rajan
0
Comment
Question by:arjunarajan
  • 7
  • 3
  • 3
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24420472
Don't think you can sorry.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24420481
You could do something like
public class ConstantUtil implements Constants

{

      public static Constants.Info getConstant (Class constantsClass, int index)

      {

            return Enum.valueOf(constantsClass, "INFO" + index);

      }

}

Open in new window

0
 

Author Comment

by:arjunarajan
ID: 24420522
Hi CEHJ,

Thank you for responding quickly.

Sorry, I just forgot that you are the one who gave me the solutions for the question related to the same thing earlier.

Hope you know the history of the question and hence I am not explaining them.

If I pass Class as parameter. How can I pass it to this method from the calling method since we have the interfaces (ConstantA, ConstantB, etc)

Thanks
Rameshbabu Arjuna Rajan
0
 
LVL 92

Expert Comment

by:objects
ID: 24420541
> How can I pass it to this method from the calling method since we have the interfaces (ConstantA, ConstantB, etc)

you can't

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24420750
>>Hope you know the history of the question and hence I am not explaining them.

Well i can only remember vaguely now, but it looks to me like you're heading into (possibly unecessarily) complex territory and these things have a way of getting increasingly complex.

I think perhaps you could be losing sight of the functional requirement and your thinking might have got locked into the abstraction itself - always a Bad Thing. Can you explain your functional requirement, your Use Case?
0
 
LVL 92

Expert Comment

by:objects
ID: 24420778
What you are attempting will not work sorry. You need to do it differently, perhaps an index or mapping may be better suited.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:arjunarajan
ID: 24420800
I have different type of records (ARecord, BRecord, etc).

All the records have this INFO values like INFO1, INFO2, etc in an array. but the thing is the array index of the INFO1, INFO2, etc will differ from each record type.

So I created an ENUM for every record type with its corresponding array index in the info index. INFO1(3), INFO2 (4), etc.

Now, when I get the type of the record during the run-time and I want to know the array index of INFO1, INFO2, etc for that particular record type, how can I write a getConstant method for that ?

0
 
LVL 92

Expert Comment

by:objects
ID: 24420829
Sounds like you should just be using an index for each class with a single shared enum.
That way you could use the index to lookup the appropriate enum value for that class.

though what are the enums actually used for?

0
 

Author Comment

by:arjunarajan
ID: 24420983
Not using single shared enum for class index.

I have different interfaces for each record type with enum representing its INFO index as mentioned above.

I want to get the corresponding interface object when I use the Enum.valueOf to find the INFO index.
0
 
LVL 92

Expert Comment

by:objects
ID: 24421021
And as I explained above that won't work.
I'm wondering why you are using an enum at all?

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24421079
>>Now, when I get the type of the record during the run-time and I want to know the array index of INFO1, INFO2, etc for that particular record type, how can I write a getConstant method for that ?

I don't see the problem there: each Record type would simply return the index of ifs own (private) enum
0
 
LVL 92

Expert Comment

by:objects
ID: 24421085
Instead of having a different enum for each class keep an index that maps the array index to the appropriate Contants.Info
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 24421138
class Constants2
{
   private int[] index = new int[] { 3, 4 };

      public Constants.Info getConstant (int index)
      {
            return Enum.valueOf(Constants.Info.class, "INFO" + index[index]);
      }


you can have a getIndex() method in a base class if you wanted which you could then use from your utils method'

      public static Constants.Info getConstant (Constant constant, int index)
      {
            return constant.getIndex(index);
      }

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hibernate Not Rolling Back When something fails in a stored procedure 2 50
what is a "java.lang.System Property"   ? 20 57
eclipse shortcuts 9 45
javap bin 2 26
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now