001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.pattern;
015
016/**
017 * A minimal converter which sets up the general interface for derived classes.
018 * It also implements the functionality to chain converters in a linked list.
019 *
020 * @param <E> The type of the event object
021 * @author ceki
022 */
023abstract public class Converter<E> {
024
025    Converter<E> next;
026
027    /**
028     * The convert method is responsible for extracting data from the event and
029     * returning a formatted string representation.
030     * 
031     * @param event the event to convert
032     * @return the formatted string representation
033     */
034    public abstract String convert(E event);
035
036    /**
037     * Formats the event by calling convert() and appends the resulting string to the provided buffer.
038     *
039     * @param buf The input buffer where data is appended
040     * @param event The event from where data is extracted
041     */
042    public void write(StringBuilder buf, E event) {
043        buf.append(convert(event));
044    }
045
046    /**
047     * Sets the next converter in the chain.
048     * This method can only be called once per converter instance.
049     * 
050     * @param next the next converter to chain
051     * @throws IllegalStateException if next has already been set
052     */
053    public final void setNext(Converter<E> next) {
054        if (this.next != null) {
055            throw new IllegalStateException("Next converter has been already set");
056        }
057        this.next = next;
058    }
059
060    /**
061     * Gets the next converter in the chain.
062     * 
063     * @return the next converter, or null if not set
064     */
065    public final Converter<E> getNext() {
066        return next;
067    }
068}