Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, can I consolidate two similar functions where uses JspWriter and the other PrintWriter?

I have the following class, which as you will see has rather a rather redundant formatNameAndAddress method:

package hu.flux.helper;
import java.io.PrintWriter;

import javax.servlet.jsp.JspWriter;

// A holder for formatting data 
public class NameAndAddress 
{
 public String firstName;
 public String middleName;
 public String lastName;
 public String address1;
 public String address2;
 public String city;
 public String state;
 public String zip;

 // Print out the name and address.
 public void formatNameAndAddress(JspWriter out)
  throws java.io.IOException
  {
   out.println("<PRE>");
    out.print(firstName);

    // Print the middle name only if it contains data.
    if ((middleName != null) && (middleName.length() > 0)) 
     {out.print(" " + middleName);}

    out.println(" " + lastName);

    out.println(" " + address1);

    if ((address2 != null) && (address2.length() > 0))
     out.println(" " + address2);

    out.println(city + ", " + state + " " + zip);
   out.println("</PRE>");
  }

 public void formatName(PrintWriter out) 
 {
  out.println("<PRE>");
  out.print(firstName);

  // Print the middle name only if it contains data.
  if ((middleName != null) && (middleName.length() > 0)) 
   {out.print(" " + middleName);}

  out.println(" " + lastName);

  out.println(" " + address1);

  if ((address2 != null) && (address2.length() > 0))
   out.println(" " + address2);

  out.println(city + ", " + state + " " + zip);
  out.println("</PRE>");
 }
}

I'd like to rewrite the class to use a generic method like:

     // Print out the name and address.
 private void genericFormatNameAndAddress(Object out)
 {
  out.println("<PRE>");
   out.print(firstName);

   // Print the middle name only if it contains data.
   if ((middleName != null) && (middleName.length() > 0)) 
    {out.print(" " + middleName);}

   out.println(" " + lastName);

   out.println(" " + address1);

   if ((address2 != null) && (address2.length() > 0))
    out.println(" " + address2);

   out.println(city + ", " + state + " " + zip);
  out.println("</PRE>");
 }

But, I can't do this exactly like this because Object doesn't have print() and println() methods. If I cast the output to either JspWriter or PrintWriter, I'd be casting it the wrong way sometimes.

I imagine what I need to do is somehow pass the object type as a variable and then use the variable to determine how to cast. Is this possible? If so, how? If not, what would be a good solution?

like image 691
Brian Kessler Avatar asked Oct 03 '10 12:10

Brian Kessler


2 Answers

This will probably work:

public void formatNameAndAddress(JspWriter out) throws java.io.IOException {
    formatNameAndAddress(new PrintWriter(out));
}
like image 61
Michael Barker Avatar answered Sep 19 '22 12:09

Michael Barker


You're kind of muddling two different tasks with these methods, and breaking the OO principle of specialization. That is, you have methods which are responsible for formatting one of two types of strings... AND responsible for sending them to one of two types of output targets.

A better approach might be to make your methods more specialized. That is, have them ONLY be responsible for building a "Name" string or a "Name and Address" string... and return String as the methods' return type.

At the point in code where you're invoking these methods, you obviously already have a JspWriter or a PrintWriter object... because right now you're passing it as an method argument. So it would be cleaner to simply leave that object where it is in the code, and have it print out the String which is returned by your specialized output-agnostic method.

like image 21
Steve Perkins Avatar answered Sep 21 '22 12:09

Steve Perkins