Close

Spring Framework - Formatter Examples

Spring Framework 

import org.springframework.format.Formatter;

import java.text.ParseException;
import java.util.Locale;

public class AddressFormatter implements Formatter<Address> {

private Style style = Style.FULL;

public void setStyle (Style style) {
this.style = style;
}

@Override
public Address parse (String text, Locale locale) throws ParseException {
if (text != null) {
String[] parts = text.split(",");
if (style == Style.FULL && parts.length == 4) {
Address address = new Address();
address.setStreet(parts[0].trim());
address.setCity(parts[1].trim());
address.setZipCode(parts[2].trim());
address.setCounty(parts[3].trim());
return address;
} else if (style == Style.REGION && parts.length == 3) {
Address address = new Address();
address.setCity(parts[0].trim());
address.setZipCode(parts[1].trim());
address.setCounty(parts[4].trim());
return address;
}
}
return null;
}

@Override
public String print (Address a, Locale l) {
if (a == null) {
return "";
}
switch (style) {
case FULL:
return String.format(l, "%s, %s, %s, %s", a.getStreet(), a.getCity(),
a.getZipCode(), a.getCounty());
case REGION:
return String.format(l, "%s, %s, %s", a.getCity(), a.getZipCode(), a.getCounty());
}
return a.toString();
}

public enum Style {
FULL,
REGION
}
Original Post




Invoking formatter programmatically by using DefaultFormattingConversionService.

package com.logicbig.example;

import org.springframework.format.support.DefaultFormattingConversionService;

/**
* This example formats Employee object in following format:
* Employee#name, Employee#dept, Employee#phoneNumber
*/
public class CustomFormatterExample {

public static void main(String[] args) {
DefaultFormattingConversionService service =
new DefaultFormattingConversionService();
service.addFormatter(new EmployeeFormatter());

Employee employee = new Employee("Joe", "IT", "123-456-7890");
String string = service.convert(employee, String.class);
System.out.println(string);

//converting back to Employee
Employee e = service.convert(string, Employee.class);
System.out.println(e);
}

}

Output

Joe, IT, 123-456-7890
Employee{name='Joe', dept='IT', phoneNumber='123-456-7890'}

A custom formatter

package com.logicbig.example;

import org.springframework.format.Formatter;
import java.text.ParseException;
import java.util.Locale;
import java.util.StringJoiner;

public class EmployeeFormatter implements Formatter<Employee> {

@Override
public Employee parse(String text,
Locale locale) throws ParseException {


String[] split = text.split(",");
if (split.length != 3) {
throw new ParseException("The Employee string format " +
"should be in this format: Mike, Account, 111-111-1111",
split.length);
}
Employee employee = new Employee(split[0].trim(),
split[1].trim(), split[2].trim());
return employee;
}

@Override
public String print(Employee employee, Locale locale) {
return new StringJoiner(", ")
.add(employee.getName())
.add(employee.getDept())
.add(employee.getPhoneNumber())
.toString();

}
}
package com.logicbig.example;


public class Employee {
private String name;
private String dept;
private String phoneNumber;

public Employee(String name, String dept, String phoneNumber) {
this.name = name;
this.dept = dept;
this.phoneNumber = phoneNumber;
}

public String getName() {
return name;
}

public String getDept() {
return dept;
}


public String getPhoneNumber() {
return phoneNumber;
}

@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", dept='" + dept + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
'}';
}
}
Original Post




See Also