1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.logging

File Message.java

 

Coverage histogram

../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

16
41
20
1
275
138
28
0.68
2.05
20
1.4

Classes

Class Line # Actions
Message 39 41 0% 28 31
0.597402659.7%
 

Contributing tests

This file is covered by 241 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.logging;
21   
22    import java.io.Serializable;
23    import java.util.List;
24   
25    import org.apache.commons.lang3.builder.EqualsBuilder;
26    import org.apache.commons.lang3.builder.HashCodeBuilder;
27    import org.slf4j.Marker;
28    import org.slf4j.helpers.MessageFormatter;
29    import org.xwiki.logging.internal.helpers.ExtendedMessageFormatter;
30    import org.xwiki.logging.marker.ContainerMarker;
31    import org.xwiki.logging.marker.TranslationMarker;
32   
33    /**
34    * A translatable message.
35    *
36    * @version $Id: ce487c2b425ba10dd236b57e4e9febb76c7ffc1f $
37    * @since 7.1M2
38    */
 
39    public class Message implements Serializable, CharSequence
40    {
41    /**
42    * Serialization identifier.
43    */
44    private static final long serialVersionUID = 1L;
45   
46    /**
47    * @see #getMarker()
48    */
49    private Marker marker;
50   
51    /**
52    * @see Message#getMessage()
53    */
54    private String message;
55   
56    /**
57    * @see #getArgumentArray()
58    */
59    private Object[] argumentArray;
60   
61    /**
62    * @see Message#getThrowable()
63    */
64    private Throwable throwable;
65   
66    /**
67    * Formatted version of the message.
68    */
69    private transient String formattedMessage;
70   
71    /**
72    * Matches any {@link Message}.
73    */
 
74  139 toggle protected Message()
75    {
76   
77    }
78   
79    /**
80    * @param message the message
81    */
 
82  106023 toggle public Message(String message)
83    {
84  106024 this(new TranslationMarker(message), message, null, null);
85    }
86   
87    /**
88    * @param message the message to copy
89    */
 
90  0 toggle public Message(Message message)
91    {
92  0 this(message.getMarker(), message.getMessage(), message.getArgumentArray(), message.getThrowable());
93    }
94   
95    /**
96    * @param message the message
97    * @param argumentArray the event arguments to insert in the message
98    * @param throwable the throwable associated to the event
99    */
 
100  0 toggle public Message(String message, Object[] argumentArray, Throwable throwable)
101    {
102  0 this(null, message, argumentArray, throwable);
103    }
104   
105    /**
106    * @param translationKey the key used to find the translation
107    * @param message the log message
108    * @param arguments the arguments to insert in the message
109    */
 
110  116879 toggle public Message(String translationKey, String message, Object... arguments)
111    {
112  116867 this(new TranslationMarker(translationKey), message, arguments, null);
113    }
114   
115    /**
116    * @param marker the log marker
117    * @param message the log message
118    * @param argumentArray the event arguments to insert in the message
119    * @param throwable the throwable associated to the event
120    */
 
121  226873 toggle public Message(Marker marker, String message, Object[] argumentArray, Throwable throwable)
122    {
123  226835 this.marker = marker;
124  226811 this.message = message;
125  226810 this.argumentArray = argumentArray;
126  226803 this.throwable = throwable;
127    }
128   
129    /**
130    * @return the log marker
131    */
 
132  1074 toggle public Marker getMarker()
133    {
134  1074 return this.marker;
135    }
136   
137    /**
138    * @return the log message
139    */
 
140  3411 toggle public String getMessage()
141    {
142  3411 return this.message;
143    }
144   
145    /**
146    * @return the event arguments to insert in the message
147    */
 
148  12502 toggle public Object[] getArgumentArray()
149    {
150  12502 return this.argumentArray;
151    }
152   
153    /**
154    * @return the throwable associated to the event
155    */
 
156  3291 toggle public Throwable getThrowable()
157    {
158  3291 return this.throwable;
159    }
160   
161    /**
162    * @return the formated version of the message
163    */
 
164  126 toggle public String getFormattedMessage()
165    {
166  126 if (this.formattedMessage != null) {
167  0 return this.formattedMessage;
168    }
169  126 if (this.argumentArray != null) {
170  126 this.formattedMessage = MessageFormatter.arrayFormat(this.message, this.argumentArray).getMessage();
171    } else {
172  0 this.formattedMessage = this.message;
173    }
174   
175  126 return this.formattedMessage;
176    }
177   
178    /**
179    * @return the log message cut in peaces
180    * @since 4.2M1
181    */
 
182  2330 toggle public List<String> getMessageElements()
183    {
184  2330 return ExtendedMessageFormatter.parseMessage(getMessage(), getArgumentArray());
185    }
186   
187    /**
188    * @return the translation key associated to the log
189    * @since 5.0M2
190    */
 
191  3 toggle public String getTranslationKey()
192    {
193  3 if (getMarker() instanceof ContainerMarker) {
194  1 ContainerMarker containerMarker = (ContainerMarker) getMarker();
195   
196  1 TranslationMarker translationMarker = containerMarker.get(TranslationMarker.NAME);
197   
198  1 if (translationMarker != null) {
199  1 return ((TranslationMarker) getMarker()).getTranslationKey();
200    }
201    }
202   
203  2 return null;
204    }
205   
206    // Object
207   
 
208  0 toggle @Override
209    public String toString()
210    {
211  0 return getFormattedMessage();
212    }
213   
 
214  0 toggle @Override
215    public int hashCode()
216    {
217  0 return new HashCodeBuilder(7, 11).append(getMarker()).append(getMessage()).append(getArgumentArray())
218    .append(getThrowable()).toHashCode();
219    }
220   
 
221  2 toggle @Override
222    public boolean equals(Object object)
223    {
224  2 if (object == null) {
225  0 return false;
226    }
227   
228  2 if (object == this) {
229  0 return true;
230    }
231   
232  2 if (object.getClass() != getClass()) {
233  0 if (object instanceof String) {
234  0 return equals((String) object);
235    } else {
236  0 return false;
237    }
238    }
239   
240  2 Message rhs = (Message) object;
241  2 return new EqualsBuilder().append(getMarker(), rhs.getMarker()).append(getMessage(), rhs.getMessage())
242    .append(getArgumentArray(), rhs.getArgumentArray()).append(getThrowable(), rhs.getThrowable()).isEquals();
243    }
244   
245    /**
246    * Helper to compare the message to the default way of displaying this {@link Message}.
247    *
248    * @param formatedMessage the formatted message
249    * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
250    */
 
251  0 toggle public boolean equals(String formatedMessage)
252    {
253  0 return getFormattedMessage().equals(formatedMessage);
254    }
255   
256    // CharSequence
257   
 
258  0 toggle @Override
259    public int length()
260    {
261  0 return getFormattedMessage().length();
262    }
263   
 
264  0 toggle @Override
265    public char charAt(int index)
266    {
267  0 return getFormattedMessage().charAt(index);
268    }
269   
 
270  0 toggle @Override
271    public CharSequence subSequence(int start, int end)
272    {
273  0 return getFormattedMessage().subSequence(start, end);
274    }
275    }