View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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   * @param <E> The type of the event object
21   * @author ceki
22   */
23  abstract public class Converter<E> {
24  
25      Converter<E> next;
26  
27      /**
28       * The convert method is responsible for extracting data from the event and
29       * returning a formatted string representation.
30       * 
31       * @param event the event to convert
32       * @return the formatted string representation
33       */
34      public abstract String convert(E event);
35  
36      /**
37       * Formats the event by calling convert() and appends the resulting string to the provided buffer.
38       *
39       * @param buf The input buffer where data is appended
40       * @param event The event from where data is extracted
41       */
42      public void write(StringBuilder buf, E event) {
43          buf.append(convert(event));
44      }
45  
46      /**
47       * Sets the next converter in the chain.
48       * This method can only be called once per converter instance.
49       * 
50       * @param next the next converter to chain
51       * @throws IllegalStateException if next has already been set
52       */
53      public final void setNext(Converter<E> next) {
54          if (this.next != null) {
55              throw new IllegalStateException("Next converter has been already set");
56          }
57          this.next = next;
58      }
59  
60      /**
61       * Gets the next converter in the chain.
62       * 
63       * @return the next converter, or null if not set
64       */
65      public final Converter<E> getNext() {
66          return next;
67      }
68  }