3

What is the current best practice for all the type conversions necessary in a Java web application? For example, HttpServletRequest.getParameters(...) returns String[], but Hibernate does not allow String[] to be used in an IN clause for a numeric column. Therefore I need to convert String[] to Long[] via Long.valueOf(). What is the best way to handle this, short of rewriting in another language? Do people just create a class full of little static methods to do this?

3 Answers 3

1

If you use a JAX-RS implementation like Jersey, you'll get much improved parameter handling and could map it directly to a List.

If you don't want to go that far, you could also pull tricks like using Guava Collections2.transform and a Function<String, Long> on an Arrays.asList view of the array. Not the most compact way, but each piece is reusable so it wouldn't be so bad.

1
  • This is a good suggestion. I have used Jersey and it is great, but right now I have a Struts application in need of a rewrite. I could migrate to Jersey, but I might as well migrate to Grails. Jul 26, 2011 at 15:14
6

What is the best way to handle this, short of rewriting in another language?

Seriously? Just because all those hip new languages have a compact syntax for transforming lists/arrays, it doesn't mean using a loop will infect you with a terminal disease or anything.

Long[] result = new Long[param.length];
for(int i=0; i<param.length; i++){
    result[i] = Long.valueOf(param[i]);
}

There, that wasn't so bad, was it? And yeah, you can and should put it in a static helper method if you use it in several places.

Edit: Since Hibernate can also take collections, a somewhat cleaner version:

List<Long> result = new ArrayList<Long>();
for(String s : param){
    result.add(Long.valueOf(s));
}
6
  • 3
    IMO, it is pretty bad. All this noise takes time to write and more time to read. Surely I'm about the 17,000,000th Java programmer to write this loop. That's why I'm asking the community. Does every Java programmer in the world write junk like this over and over again? I'm annoyed that I even have to think about this. Jul 26, 2011 at 15:13
  • @kevin cline: until Java gets closures (alas, not in Java 7) there isn't really anything that can be done about it; writing it yourself is easier and faster than finding an including a library that does it. And actually, it can be done with somewhat less noise since Hibernate takes collections as well as arrays. Jul 26, 2011 at 17:15
  • good point about Hibernate and collections. No closures in Java 7? By the time Java moves into the 21st century there won't be anyone still using it. Jul 26, 2011 at 17:37
  • That's why people are switching to Groovy and Scala. Jul 27, 2011 at 1:16
  • @Kaleb - yes, I'm one of them, but I still have Java code to maintain for another few months. Aug 2, 2011 at 16:22
0

Apache Commons NumberUtils is best. The conversion routines in the JDK like to just throw a NumberFormatException on unparseable strings. This lib allows you to specify a default value for failed parsing.

4
  • These functions only handle scalar conversions. They don't handle the need for conversion of collections or arrays from one type to another, as described in my example. Jul 25, 2011 at 23:41
  • So, write a loop? I assume you have some layers in between your request params and your database updates.
    – jiggy
    Jul 26, 2011 at 15:37
  • 1
    Ok, I think I see now. You aren't really asking about type conversions, but about request parameter binding. The "standard" Java approach would be to use an MVC framework like Spring MVC that has request param mapping annotations.
    – jiggy
    Jul 26, 2011 at 15:42
  • Sounds good if I can integrate that into my existing Struts 1 application. Jul 26, 2011 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.