1
\$\begingroup\$

This is my code to populate a Class and later convert it to JSON object.

public List<CSATGraphJSON> convertListToCSATGraphJSON(List<Object[]> list) throws ParseException{
        List<CSATGraphJSON> customers=new ArrayList<CSATGraphJSON>();
        //going through each list of object
        for (int i = 0; i < list.size(); i++) {
            CSATGraphJSON graphJSON=new CSATGraphJSON();
            Object eachObject=list.get(i);
            int index=0;
            //iterating the each object array
            for(Object obj: (Object[]) eachObject){
                System.out.println(obj.toString());
                System.out.println(obj.getClass());
                switch(index){
                    case 0:{
                        if(obj!=null){
                            // if not null
                            /*DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
                            Date parsedDate = null;
                            parsedDate = df.parse(obj.toString());
                            // gives you java.util.Date
                            customerList.setResponsedatestr(parsedDate);*/
                            graphJSON.setX(obj.toString());
                        }
                        break;
                    }
                    case 1:{
                        if(obj!=null){
                            graphJSON.setVery_poor(Integer.parseInt(obj.toString()));
                        }
                        break;
                    }
                    case 2:{
                        if(obj!=null){
                            graphJSON.setPoor(Integer.parseInt(obj.toString()));
                        }
                        break;
                    }
                    case 3:{
                        if(obj!=null){
                            graphJSON.setAverage(Integer.parseInt(obj.toString()));
                        }                       
                        break;
                    }
                    case 4:{
                        if(obj!=null){
                            graphJSON.setGood(Integer.parseInt(obj.toString()));
                        }                       
                        break;
                    }
                    case 5:{
                        if(obj!=null){
                            graphJSON.setVery_good(Integer.parseInt(obj.toString()));
                        }                       
                        break;
                    }
                    case 6:{
                        if(obj!=null){
                            graphJSON.setTotal(Integer.parseInt(obj.toString()));
                        }                       
                        break;
                    }
                    default:{
                        break;
                    }
                }
                index++;

            }
            customers.add(graphJSON);
        }


        return customers;
    }

Here List list the result of createSQLQuery() in hibernate as it returns List of Object ,so it is converted to List of CSATGraphJSON object.

Please suggest any alternative method for the switch statement as it is making the code lengthy.

\$\endgroup\$
2
\$\begingroup\$

Naming

Java method names should not contain underscore characters. It's also preferable to use CamelCase even for abbreviations for readability (e.g. graphJson instead of graphJSON. convertListToCSATGraphJSON is pretty clumsy and could stand to be renamed for simplicity and clarity.

Implementation

I'm going to assume there's a good reason you're hand-rolling this. If not, hibernate has tools to help do this for you.

You have both for loops backwards. The first one should be an enhanced for-loop, and the second should be old-school.

There is absolutely no reason for the second for loop or the switch statement to exist. Just directly set the values by accessing the index number in the Object[] directly.

You may run into trouble later using a List<Object[]>. Generics and arrays do not play nicely together. Changing that is outside the scope of this review.

Small static helper methods can greatly clean up the variable assignments.

If you were to make all these changes, your code might look more like:

public List<CSATGraphJSON> convertListToCSATGraphJSON(final List<Object[]> list) throws ParseException {
    final List<CSATGraphJSON> customers = new ArrayList<CSATGraphJSON>();
    for (final Object[] objects : list) {
        final CSATGraphJSON graphJson = new CSATGraphJSON();
        graphJson.setX(parseString(objects[0]));
        graphJson.setVeryPoor(parseInt(objects[1]));
        graphJson.setPoor(parseInt(objects[2]));
        graphJson.setAverage(parseInt(objects[3]));
        graphJson.setGood(parseInt(objects[4]));
        graphJson.setVeryGood(parseInt(objects[5]));
        graphJson.setTotal(parseInt(objects[6]));
        customers.add(graphJson);
    }

    return customers;
}

private static String parseString(final Object o) {
    if (o == null) {
        return null;
    }
    return o.toString();
}

private static int parseInt(final Object o) {
    if (o == null) {
        return 0;
    }
    return Integer.parseInt(o.toString());
}
\$\endgroup\$
  • \$\begingroup\$ Thanks @Eric I appreciate your response. I will keep this Naming Convention in mind. \$\endgroup\$ – Soumik Dutta Jan 21 '16 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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