Solved

java 5.0 syntax

Posted on 2004-10-09
9
207 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 80
firstChar challenge 13 107
JAVA part two 5 57
Responding to Java JComponent extended classes's resize event 4 51
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

910 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

22 Experts available now in Live!

Get 1:1 Help Now