LANG-487 - Make default style thread-safe
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@829991 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
61800cce67
commit
d55299a055
|
@ -94,26 +94,50 @@ public class ToStringBuilder {
|
|||
/**
|
||||
* The default style of output to use.
|
||||
*/
|
||||
private static ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE;
|
||||
private static volatile ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Gets the default <code>ToStringStyle</code> to use.</p>
|
||||
*
|
||||
* <p>This could allow the <code>ToStringStyle</code> to be
|
||||
* controlled for an entire application with one call.</p>
|
||||
*
|
||||
* <p>This might be used to have a verbose
|
||||
* <code>ToStringStyle</code> during development and a compact
|
||||
* <code>ToStringStyle</code> in production.</p>
|
||||
*
|
||||
* @return the default <code>ToStringStyle</code>
|
||||
* <p>This method gets a singleton default value, typically for the whole JVM.
|
||||
* Changing this default should generally only be done during application startup.
|
||||
* It is recommended to pass a <code>ToStringStyle</code> to the constructor instead
|
||||
* of using this global default.</p>
|
||||
*
|
||||
* <p>This method is thread-safe, as a <code>volatile</code variable is used internally.</p>
|
||||
*
|
||||
* <p>One reason for changing the default could be to have a verbose style during
|
||||
* development and a compact style in production.</p>
|
||||
*
|
||||
* @return the default <code>ToStringStyle</code>, never null
|
||||
*/
|
||||
public static ToStringStyle getDefaultStyle() {
|
||||
return defaultStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Sets the default <code>ToStringStyle</code> to use.</p>
|
||||
*
|
||||
* <p>This method sets a singleton default value, typically for the whole JVM.
|
||||
* Changing this default should generally only be done during application startup.
|
||||
* It is recommended to pass a <code>ToStringStyle</code> to the constructor instead
|
||||
* of changing this global default.</p>
|
||||
*
|
||||
* <p>This method is thread-safe, as a <code>volatile</code variable is used internally.</p>
|
||||
*
|
||||
* @param style the default <code>ToStringStyle</code>
|
||||
* @throws IllegalArgumentException if the style is <code>null</code>
|
||||
*/
|
||||
public static void setDefaultStyle(ToStringStyle style) {
|
||||
if (style == null) {
|
||||
throw new IllegalArgumentException("The style must not be null");
|
||||
}
|
||||
defaultStyle = style;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
|
||||
*
|
||||
|
@ -169,18 +193,7 @@ public class ToStringBuilder {
|
|||
return ReflectionToStringBuilder.toString(object, style, outputTransients, false, reflectUpToClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Sets the default <code>ToStringStyle</code> to use.</p>
|
||||
*
|
||||
* @param style the default <code>ToStringStyle</code>
|
||||
* @throws IllegalArgumentException if the style is <code>null</code>
|
||||
*/
|
||||
public static void setDefaultStyle(ToStringStyle style) {
|
||||
if (style == null) {
|
||||
throw new IllegalArgumentException("The style must not be null");
|
||||
}
|
||||
defaultStyle = style;
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Current toString buffer.
|
||||
|
@ -208,7 +221,7 @@ public class ToStringBuilder {
|
|||
* <code>null</code>
|
||||
*/
|
||||
public ToStringBuilder(Object object) {
|
||||
this(object, getDefaultStyle(), null);
|
||||
this(object, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue