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 {
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 getFormattedMessage();
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 Message memento() {
85          return new SimpleMessage(charSequence);
86      }
87  
88      // CharSequence impl
89  
90      @Override
91      public int length() {
92          return charSequence == null ? 0 : charSequence.length();
93      }
94  
95      @Override
96      public char charAt(final int index) {
97          return charSequence.charAt(index);
98      }
99  
100     @Override
101     public CharSequence subSequence(final int start, final int end) {
102         return charSequence.subSequence(start, end);
103     }
104 }
105