Java Formattable Interface

Introduction

The Formattable interface in Java, part of the java.util package, allows custom formatting for objects when using the Formatter class. This interface provides a way to define how an object should be formatted as a string.

Table of Contents

  1. What is the Formattable Interface?
  2. Common Methods
  3. Examples of Using the Formattable Interface
  4. Conclusion

1. What is the Formattable Interface?

The Formattable interface is used to customize the string representation of objects when using Java's formatting features. It allows an object to control how it is formatted, providing flexibility beyond the default toString method.

2. Common Methods

The Formattable interface contains a single method:

  • formatTo(Formatter formatter, int flags, int width, int precision): Formats the object using the provided Formatter object, with specified flags, width, and precision.

Parameters

  • formatter: The Formatter instance to be used.
  • flags: Flags to modify the output format.
  • width: Minimum number of characters to be written to the output.
  • precision: Maximum number of characters to be written to the output.

3. Examples of Using the Formattable Interface

Example 1: Implementing the Formattable Interface

This example demonstrates how to implement the Formattable interface for a custom class.

import java.util.Formattable;
import java.util.Formatter;
import java.util.Locale;

public class Person implements Formattable {
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public void formatTo(Formatter formatter, int flags, int width, int precision) {
        StringBuilder sb = new StringBuilder();
        sb.append(lastName).append(", ").append(firstName);
        int len = sb.length();

        if (precision >= 0 && precision < len) {
            sb.setLength(precision);
        }
        if (width > len) {
            for (int i = len; i < width; i++) {
                sb.insert(0, ' ');
            }
        }
        formatter.format(sb.toString());
    }

    public static void main(String[] args) {
        Person person = new Person("John", "Doe");
        Formatter formatter = new Formatter();
        formatter.format("%20.10s", person);
        System.out.println(formatter);
    }
}

Output:

Doe, John

Example 2: Using Custom Formatting

This example shows how to use custom formatting with the Formattable implementation.

package net.javaguides.utility;

import java.util.Formattable;
import java.util.Formatter;
import java.util.Locale;

public class CustomFormattingExample {
    public static void main(String[] args) {
        Person person = new Person("Ramesh", "Fadatare");
        Formatter formatter = new Formatter(Locale.US);

        // Default format
        formatter.format("%s", person);
        System.out.println(formatter);

        // Custom width and precision
        formatter = new Formatter(Locale.US);
        formatter.format("%20.10s", person);
        System.out.println(formatter);
    }
}

class Person implements Formattable {
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public void formatTo(Formatter formatter, int flags, int width, int precision) {
        StringBuilder sb = new StringBuilder();
        sb.append(lastName).append(", ").append(firstName);
        int len = sb.length();

        if (precision >= 0 && precision < len) {
            sb.setLength(precision);
        }
        if (width > len) {
            for (int i = len; i < width; i++) {
                sb.insert(0, ' ');
            }
        }
        formatter.format(sb.toString());
    }
}

Output:

Fadatare, Ramesh
    Fadatare, 

4. Conclusion

The Formattable interface in Java allows for custom formatting of objects, providing more control over how objects are represented as strings. By implementing the formatTo method, you can define specific formatting rules that can be applied when using the Formatter class. This is especially useful for creating well-formatted output in applications where presentation is important.

Comments