View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.message;
18  
19  import org.apache.logging.log4j.util.PerformanceSensitive;
20  
21  /**
22   * Mutable Message wrapper around a String message.
23   * @since 2.6
24   */
25  @PerformanceSensitive("allocation")
26  public class ReusableSimpleMessage implements ReusableMessage, CharSequence, ParameterVisitable, Clearable {
27      private static final long serialVersionUID = -9199974506498249809L;
28      private static Object[] EMPTY_PARAMS = new Object[0];
29      private CharSequence charSequence;
30  
31      public void set(final String message) {
32          this.charSequence = message;
33      }
34  
35      public void set(final CharSequence charSequence) {
36          this.charSequence = charSequence;
37      }
38  
39      @Override
40      public String getFormattedMessage() {
41          return String.valueOf(charSequence);
42      }
43  
44      @Override
45      public String getFormat() {
46          return charSequence instanceof String ? (String) charSequence : null;
47      }
48  
49      @Override
50      public Object[] getParameters() {
51          return EMPTY_PARAMS;
52      }
53  
54      @Override
55      public Throwable getThrowable() {
56          return null;
57      }
58  
59      @Override
60      public void formatTo(final StringBuilder buffer) {
61          buffer.append(charSequence);
62      }
63  
64      /**
65       * This message does not have any parameters, so this method returns the specified array.
66       * @param emptyReplacement the parameter array to return
67       * @return the specified array
68       */
69      @Override
70      public Object[] swapParameters(final Object[] emptyReplacement) {
71          return emptyReplacement;
72      }
73  
74      /**
75       * This message does not have any parameters so this method always returns zero.
76       * @return 0 (zero)
77       */
78      @Override
79      public short getParameterCount() {
80          return 0;
81      }
82  
83      @Override
84      public <S> void forEachParameter(final ParameterConsumer<S> action, final S state) {
85      }
86  
87      @Override
88      public Message memento() {
89          return new SimpleMessage(charSequence);
90      }
91  
92      // CharSequence impl
93  
94      @Override
95      public int length() {
96          return charSequence == null ? 0 : charSequence.length();
97      }
98  
99      @Override
100     public char charAt(final int index) {
101         return charSequence.charAt(index);
102     }
103 
104     @Override
105     public CharSequence subSequence(final int start, final int end) {
106         return charSequence.subSequence(start, end);
107     }
108 
109     @Override
110     public void clear() {
111         charSequence = null;
112     }
113 }
114