This discussion is archived
4 Replies Latest reply: Aug 5, 2012 1:47 PM by DrClap RSS

make a generic method

csckid Newbie
Currently Being Moderated
I have to write the following code for several class like this:
private void doOperation(Department department){
  System.out.println(department.getName());
}
private void doOperation(School school){
  System.out.println(school.getCode());
}
I have refactor the above code and made it to this:
private void impovedOperation(Object obj){
  if(obj instance of Department)(
           Department dept = (Department) obj;
           System.out.println(dept .getName());
  )
 if(obj instance of School)(
           School school= (School) obj;
           System.out.println( school.getCode());
  )
}
Now I want to make the code even simpler. I just want to call my method like this without changing the improvedOperation method. I want to make it generic. Please tell me how whether is it possible and how to do it.
impovedOperation(department, "getName");
impovedOperation(university, "getUniversityName");
  • 1. Re: make a generic method
    EJP Guru
    Currently Being Moderated
    I have refactored above code
    Why? It was much better the way you had it at first. The change was not an improvement. Put it back the way it was.
  • 2. Re: make a generic method
    ++sja Explorer
    Currently Being Moderated
    Create an interface that declares a method "String name();" (possibly other methods if Department and School have common functionality; subclassing might also be appropriate instead of an interface.) Make Department and School implement the interface, and implement the name() method in each class. Then you can do System.out.println(whatever.name());

    If you have a class called University that has a method getUniversityName(), consider renaming the method to just name(). The words "get" and "university" are just unhelpful noise in the method name, IMHO, unless you are working in a bean environment that dictates the "get" prefix. Then the common interface becomes even easier to implement.
  • 3. Re: make a generic method
    csckid Newbie
    Currently Being Moderated
    I wanted to call only one method instead of creating several method that does the same task. My real example of code is different than this, I wrote the previous code to make it easy to understand.
    I was doing GWT; in there for cellTable sorting you need to write bunch of codes. To make it easy I was looking for generic.
    Here is the original code, which I wanted to enhance

    for sorting name column:
             nameColumn.setSortable(true);
              
             ListHandler<Department> columnSortHandler = new ListHandler<Department>(
                       list);
             columnSortHandler.setComparator(nameColumn,
                 new Comparator<Department>() {
                   public int compare(Department o1, Department o2) {
                     if (o1.getName() == o2.getName()) {
                       return 0;
                     }
    
                     // Compare the name columns.
                     if (o1.getName() != null) {
                       return (o2.getName() != null) ? o1.getName().compareTo(o2.getName()) : 1;
                     }
                     return -1;
                   }
                   
                 });        
    for sorting codeColumn
                     codeColumn.setSortable(true);
                     columnSortHandler.setComparator(codeColumn,
                           new Comparator<Department>() {
                             public int compare(Department o1, Department o2) {
                               if (o1.getCode() == o2.getCode()) {
                                 return 0;
                               }
    
                               // Compare the name columns.
                               if (o1.getCode() != null) {
                                 return (o2.getCode() != null) ? o1.getCode().compareTo(o2.getCode()) : 1;
                               }
                               return -1;
                             }
                             
            });
  • 4. Re: make a generic method
    DrClap Expert
    Currently Being Moderated
    But Generics is there so that you can make your code type-safe when casting. You don't have anything like that, you just have a normal situation where you have two Comparators which are intended to do things differently. It's questionable whether it's even worth trying to refactor two methods which do different things into a single method which does different things based on I don't know what, but even if it was worth it, Generics isn't the way to do it.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points