Solved

java 5.0 syntax

Posted on 2004-10-09
9
206 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
Comment Utility
Just make the method take a type of Object:


statement.addPreparedArgument(value);
0
 

Author Comment

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

Author Comment

by:cathalmchale
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
:-)

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

744 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

17 Experts available now in Live!

Get 1:1 Help Now