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     * storing it for later use by the write method.
030     * 
031     * @param event the event to convert
032     */
033    public abstract String convert(E event);
034
035    /**
036     * In its simplest incarnation, a convert simply appends the data extracted from
037     * the event to the buffer passed as parameter.
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    public final void setNext(Converter<E> next) {
047        if (this.next != null) {
048            throw new IllegalStateException("Next converter has been already set");
049        }
050        this.next = next;
051    }
052
053    public final Converter<E> getNext() {
054        return next;
055    }
056}