View Javadoc
1   package org.apache.commons.jcs3.log;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache license, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License. You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the license for the specific language governing permissions and
17   * limitations under the license.
18   */
19  
20  import java.text.MessageFormat;
21  import java.util.IllegalFormatException;
22  import java.util.function.Supplier;
23  import java.util.stream.Stream;
24  
25  /**
26   * Handles messages that consist of a format string conforming to
27   * java.text.MessageFormat. (Borrowed from log4j2)
28   */
29  public class MessageFormatter
30  {
31      private final String messagePattern;
32      private final transient Object[] parameters;
33      private transient String formattedMessage;
34      private transient Throwable throwable;
35  
36      /**
37       * Constructs a message formatter.
38       *
39       * @param messagePattern
40       *            the pattern for this message format
41       * @param parameters
42       *            The objects to format
43       */
44      public MessageFormatter(final String messagePattern, final Object... parameters)
45      {
46          this.messagePattern = messagePattern;
47          this.parameters = parameters;
48          final int length = parameters == null ? 0 : parameters.length;
49          if (length > 0 && parameters[length - 1] instanceof Throwable)
50          {
51              this.throwable = (Throwable) parameters[length - 1];
52          }
53      }
54  
55      /**
56       * Constructs a message formatter.
57       *
58       * @param messagePattern
59       *            the pattern for this message format
60       * @param paramSuppliers
61       *            An array of functions, which when called, produce the desired
62       *            log message parameters.
63       */
64      public MessageFormatter(final String messagePattern, final Supplier<?>... paramSuppliers)
65      {
66          this.messagePattern = messagePattern;
67          this.parameters = Stream.of(paramSuppliers)
68                              .map(Supplier::get)
69                              .toArray();
70  
71          final int length = parameters.length;
72          if (length > 0 && parameters[length - 1] instanceof Throwable)
73          {
74              this.throwable = (Throwable) parameters[length - 1];
75          }
76      }
77  
78      /**
79       * Returns the formatted message.
80       *
81       * @return the formatted message.
82       */
83      public String getFormattedMessage()
84      {
85          if (formattedMessage == null)
86          {
87              formattedMessage = formatMessage(messagePattern, parameters);
88          }
89          return formattedMessage;
90      }
91  
92      protected String formatMessage(final String msgPattern, final Object... args)
93      {
94          try
95          {
96              final MessageFormat temp = new MessageFormat(msgPattern);
97              return temp.format(args);
98          }
99          catch (final IllegalFormatException ife)
100         {
101             return msgPattern;
102         }
103     }
104 
105     @Override
106     public String toString()
107     {
108         return getFormattedMessage();
109     }
110 
111     /**
112      * Return the throwable passed to the Message.
113      *
114      * @return the Throwable.
115      */
116     public Throwable getThrowable()
117     {
118         return throwable;
119     }
120 
121     /**
122      * Return true, if the parameters list contains a Throwable.
123      *
124      * @return true, if the parameters list contains a Throwable.
125      */
126     public boolean hasThrowable()
127     {
128         return throwable != null;
129     }
130 }