0

I have an object "JudgesSubmission" with the following methods:

public String getInnovationGrade1() {
    return innovationGrade1;
}
public String getInnovationGrade2() {
    return innovationGrade2;
}
public String getInnovationGrade3() {
    return innovationGrade3;
}
public String getInnovationGrade4() {
    return innovationGrade4;
}

Now, when calling these methods, I want to put them in a loop where the called method name gets the index of the loop attached to its end changing the method called. Is this possible? For example, the following code would never work, but I am writing it to explain what I need:

judgesSubmission metricScores= new judgesSubmission;
int metricSum=0;
for (int i=0;i<4;i++){
metricSum=metricSum
      Integer.parseInt(metricScores.getInnovationGrade+"i"());
}

Is there a way to do that or do I always have the full method name written?

1
  • Why not just hold an array/collection in your class? Why four different methods? Commented Nov 16, 2017 at 18:17

2 Answers 2

2

What you want to do is not possible... but with reflection such as :

MyObject.class.getMethod("mymethod"+i);

Without reflection you could use a Supplier<String> :

public void process(Supplier<String>... suppliers){

    judgesSubmission metricScores= new judgesSubmission;
    int metricSum=0;
    for (Supplier<String> supplier : suppliers){
          Integer.parseInt(supplier.get());
    }
}

And call it such as :

MyObject myObject = new MyObject();
process(()->myObject.getInnovationGrade1(), 
    ()->myObject.getInnovationGrade2(),
    ()->myObject.getInnovationGrade3(),
    ()->myObject.getInnovationGrade4());
Sign up to request clarification or add additional context in comments.

Comments

2

It is not possible without reflection (and is highly not recommended)

Instead you may want to use other methods:

  1. An array of the data (either replacing the 4 methods, or in addition)

    String[] getInnovationGrades()
    {
        return new String[]{innovationGrade1, innovationGrade2, innovationGrade3, innovationGrade4};
    }
    
    • Then later you can use

      for(String innovationGrade : getInnovationGrades())
          //do stuff
      
  2. An argument to get the data you want

    String getInnovationGrade(int i)
    {
        switch(i)
        {
            case 1:
                return getInnovationGrade1();
            case 2:
                return getInnovationGrade2();
            case 3:
                return getInnovationGrade3();
            case 4:
                return getInnovationGrade4();
            default:
                return ""; //or throw exception, depends on how you wish to handle errors
        }
    }
    
    • Then later you can use

      for(int i = 1; i <= 4; i++)
          getInnovationGrade(i); //and do stuff with it
      

3 Comments

This way is not so bad (+1). But it has still a drawback : you have to maintain both client and service side as you add or remove a method.
Even without full access to both parts of the code, an adapter/wrapper wouldn't be a terrible idea to implement
Really sure. But by using Supplier, you don't have this maintenance drawback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.