Solved

java 5.0 syntax

Posted on 2004-10-09
9
211 Views
Last Modified: 2010-03-31
is there any way to do the following (this code doesnt even compile

private void addPreparedArgs(SQLStatement statement, List args, Class clazz)
  {
    for(Object value : args)
    {
      statement.addPreparedArgument(clazz.cast(value));
    }
  }

where:
 statement.addPreparedArgument(...);
can accept Date, float, int .. etc??

or still have to use series of if statements??
0
Comment
Question by:cathalmchale
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12266085
Just make the method take a type of Object:


statement.addPreparedArgument(value);
0
 

Author Comment

by:cathalmchale
ID: 12266106
its not my method, thats the problem!!  its on the back end and has been defined
0
 

Author Comment

by:cathalmchale
ID: 12266187
because u cannot even do the following - it says duplicate methods

private void addPreparedArgs(SQLStatement statement, List<Date> args)
  {
    for (Date value : args)
      statement.addPreparedArgument(value);
  }


  private void addPreparedArgs(SQLStatement statement, List<String> args)
  {
    for (String value : args)
      statement.addPreparedArgument(value);
  }

so guess there has to be some way of doing this!
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12267125
>>its on the back end and has been defined

Well it's a question of how addPreparedArgument has been defined. How *has* is been defined?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12267279
If a parameter of type Object is used, then you can do something like this:

import java.util.List;
import java.util.ArrayList;
import java.util.Date;


public class AddPrepared {

      public static void main(String[] args) {
            AddPrepared ap = new AddPrepared();
            SQLStatement ss = new SQLStatement();
            List list = new ArrayList();
            int i = 1;
            Date d = new Date();
            String s = "hello";
            list.add(i);
            list.add(d);
            list.add(s);
            ap.addPreparedArgs(ss, list, null);
      }


      private void addPreparedArgs(SQLStatement statement, List args, Class clazz) {
            for(Object value : args) {
                  statement.addPreparedArgument(value);
            }
      }


}

class SQLStatement {
      public void addPreparedArgument(Object value) {
            System.out.println(value);
      }
}
0
 

Author Comment

by:cathalmchale
ID: 12271544
the back-end has been defined as follows

class SQLStatement {
     public void addPreparedArgument(String value) {
       
     }
     public void addPreparedArgument(int value) {
       
     }
     public void addPreparedArgument(float value) {
       
     }
     public void addPreparedArgument(Date value) {
       
     }
}

in the front end i have

class SqlQuery (
    private SQLStatement sql;

    private List<Date> dateArgs;
    private List<String> filterArgs;

    //and i want to write a method which will add all the args in a list (regardless of the generic type of the list - as long as its type is 1 of those listed in the back-end) to the SQLStatement object.

  // see earlier posts for egs. of how i proposed to do this, but they dont work for obvious reasons, but is there a way to do this or should i just use if elses to figure out type.  I cant change the back end so i cannot pass Object to the sql.addPreparedArgument(Object o) as it has not been defined - the back end isnt to do any casting or validation, i must do this in the front end.

Thanks,
Cathal.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 12271973
This is the way to do it, but if you can't change the back end, you'd  have to do something like:

            for(Object value : args) {
                  if (value instanceof Integer) {
                        statement.addPreparedArgument((Integer)value);
                  }
                  else if (value instanceof Float) {
                        statement.addPreparedArgument((Float)value);                  
                  }
                  //etc

            }





//SNIP===========================================
import java.util.List;
import java.util.ArrayList;
import java.util.Date;


public class AddPrepared {

      public static void main(String[] args) {
            AddPrepared ap = new AddPrepared();
            SQLStatement ss = new SQLStatement();
            List list = new ArrayList();
            int i = 1;
            Date d = new Date();
            String s = "hello";
            list.add(i);
            list.add(d);
            list.add(s);
            ap.addPreparedArgs(ss, list, null);
      }


      private void addPreparedArgs(SQLStatement statement, List args, Class clazz) {
            for(Object value : args) {
                  statement.addPreparedArgument(value);
                  System.out.println(statement);
            }
      }


}

class SQLStatement {
      private Object argument;


      public void addPreparedArgument(Object value) {
            argument = value;
      }


      public String toString() {
            return "My argument is " + argument;
      }

}
0
 

Author Comment

by:cathalmchale
ID: 12274214
ok thanks a lot for your help.  1 further comment.  I notice in your answers you have:

 List list = new ArrayList();
          int i = 1;
          Date d = new Date();
          String s = "hello";
          list.add(i);
          list.add(d);
          list.add(s);

i.e. a single List to which you add different types of object. does it make any difference to a possible solution if you have like me, several lists of a single generic type?

 private List<Date> dateArgs;
    private List<String> filterArgs;

Kindest regards,
Cathal.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12274755
:-)

>>does it make any difference to a possible solution ...

I can't think of anything other than that you'd be able to treat your Lists uniformly, which may be convenient in certain cases

0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

688 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