1 /**
2 * Logback: the reliable, generic, fast and flexible logging framework.
3 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4 *
5 * This program and the accompanying materials are dual-licensed under
6 * either the terms of the Eclipse Public License v1.0 as published by
7 * the Eclipse Foundation
8 *
9 * or (per the licensee's choosing)
10 *
11 * under the terms of the GNU Lesser General Public License version 2.1
12 * as published by the Free Software Foundation.
13 */
14 package ch.qos.logback.core.pattern;
15
16 /**
17 * A minimal converter which sets up the general interface for derived classes.
18 * It also implements the functionality to chain converters in a linked list.
19 *
20 * @author ceki
21 */
22 abstract public class Converter<E> {
23
24 Converter<E> next;
25
26 /**
27 * The convert method is responsible for extracting data from the event and
28 * storing it for later use by the write method.
29 *
30 * @param event
31 */
32 public abstract String convert(E event);
33
34 /**
35 * In its simplest incarnation, a convert simply appends the data extracted from
36 * the event to the buffer passed as parameter.
37 *
38 * @param buf The input buffer where data is appended
39 * @param event The event from where data is extracted
40 */
41 public void write(StringBuilder buf, E event) {
42 buf.append(convert(event));
43 }
44
45 public final void setNext(Converter<E> next) {
46 if (this.next != null) {
47 throw new IllegalStateException("Next converter has been already set");
48 }
49 this.next = next;
50 }
51
52 public final Converter<E> getNext() {
53 return next;
54 }
55 }