Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
title"SsnFormatter.java"
public class SsnFormatter implements Formatter<Ssn> { 
	private String formatType; 

	/** Sets the format type to be used to render Ssns as Strings. */ 
	public void setFormatType(String formatType) { 
		this.formatType = formatType; 
	} 

	public void setFormatPattern(String formatPattern) { 
		// SsnFormatter doesn't use format-pattern 
	} 

	public void setLocale(Locale locale) { 
		// Ssn's are US specific, so no locale support either 
	} 

	/** Sets defaults if a formatType was not supplied. */ 
	public void init() { 
		if (this.formatType == null) { 
			this.formatType = "lastfour"; 
		} 
	} 

	/** Formats the Ssn supplied as a String. */ 
	public String format(Ssn ssn) { 
		if ("lastfour".equals(this.formatType)) { 
			return "###-##-" + ssn.getLastFourDigits(); 
		} 
		else if ("allnine".equals(this.formatType)) { 
			return ssn.getDigits(0,3) + "-" + ssn.getDigits(3,5) + "-" + ssn.getLastFourDigits(); 
		} 
	} 
}