001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.message; 018 019import org.apache.logging.log4j.util.Constants; 020import org.apache.logging.log4j.util.PerformanceSensitive; 021 022/** 023 * Mutable Message wrapper around a String message. 024 * @since 2.6 025 */ 026@PerformanceSensitive("allocation") 027public class ReusableSimpleMessage implements ReusableMessage, CharSequence, ParameterVisitable, Clearable { 028 private static final long serialVersionUID = -9199974506498249809L; 029 private CharSequence charSequence; 030 031 public void set(final String message) { 032 this.charSequence = message; 033 } 034 035 public void set(final CharSequence charSequence) { 036 this.charSequence = charSequence; 037 } 038 039 @Override 040 public String getFormattedMessage() { 041 return String.valueOf(charSequence); 042 } 043 044 @Override 045 public String getFormat() { 046 return charSequence instanceof String ? (String) charSequence : null; 047 } 048 049 @Override 050 public Object[] getParameters() { 051 return Constants.EMPTY_OBJECT_ARRAY; 052 } 053 054 @Override 055 public Throwable getThrowable() { 056 return null; 057 } 058 059 @Override 060 public void formatTo(final StringBuilder buffer) { 061 buffer.append(charSequence); 062 } 063 064 /** 065 * This message does not have any parameters, so this method returns the specified array. 066 * @param emptyReplacement the parameter array to return 067 * @return the specified array 068 */ 069 @Override 070 public Object[] swapParameters(final Object[] emptyReplacement) { 071 return emptyReplacement; 072 } 073 074 /** 075 * This message does not have any parameters so this method always returns zero. 076 * @return 0 (zero) 077 */ 078 @Override 079 public short getParameterCount() { 080 return 0; 081 } 082 083 @Override 084 public <S> void forEachParameter(final ParameterConsumer<S> action, final S state) { 085 } 086 087 @Override 088 public Message memento() { 089 return new SimpleMessage(charSequence); 090 } 091 092 // CharSequence impl 093 094 @Override 095 public int length() { 096 return charSequence == null ? 0 : charSequence.length(); 097 } 098 099 @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