Solved

java 5.0 syntax

Posted on 2004-10-09
9
210 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android development question 2 105
Adding multiple JVM environments to RedHat 6 7 79
Java: The Public Class Main 4 62
java imports not found 4 68
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

738 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