001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, 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 v1.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 * @author ceki
021 */
022abstract public class Converter<E> {
023
024    Converter<E> next;
025
026    /**
027     * The convert method is responsible for extracting data from the event and
028     * storing it for later use by the write method.
029     * 
030     * @param event
031     */
032    public abstract String convert(E event);
033
034    /**
035     * In its simplest incarnation, a convert simply appends the data extracted from
036     * the event to the buffer passed as parameter.
037     * 
038     * @param buf   The input buffer where data is appended
039     * @param event The event from where data is extracted
040     */
041    public void write(StringBuilder buf, E event) {
042        buf.append(convert(event));
043    }
044
045    public final void setNext(Converter<E> next) {
046        if (this.next != null) {
047            throw new IllegalStateException("Next converter has been already set");
048        }
049        this.next = next;
050    }
051
052    public final Converter<E> getNext() {
053        return next;
054    }
055}