?
Solved

java 5.0 syntax

Posted on 2004-10-09
9
Medium Priority
?
219 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

831 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