public class Unbox
extends java.lang.Object
Example usage:
import static org.apache.logging.log4j.util.Unbox.box; ... long longValue = 123456L; double doubleValue = 3.14; // prevent primitive values from being auto-boxed logger.debug("Long value={}, double value={}", box(longValue), box(doubleValue));
This class manages a small thread-local ring buffer of StringBuilders.
Each time one of the box()
methods is called, the next slot in the ring buffer is used, until the ring
buffer is full and the first slot is reused. By default the Unbox ring buffer has 32 slots, so user code can
have up to 32 boxed primitives in a single logger call.
If more slots are required, set system property log4j.unbox.ringbuffer.size
to the desired ring buffer size.
Note that the specified number will be rounded up to the nearest power of 2.
Modifier and Type | Class and Description |
---|---|
private static class |
Unbox.State |
private static class |
Unbox.WebSafeState
State implementation that only puts JDK classes in ThreadLocals, so this is safe to be used from
web applications.
|
Modifier and Type | Field and Description |
---|---|
private static int |
BITS_PER_INT |
private static Logger |
LOGGER |
private static int |
MASK |
private static int |
RINGBUFFER_MIN_SIZE |
private static int |
RINGBUFFER_SIZE |
private static java.lang.ThreadLocal<Unbox.State> |
threadLocalState |
private static Unbox.WebSafeState |
webSafeState |
Modifier | Constructor and Description |
---|---|
private |
Unbox() |
Modifier and Type | Method and Description |
---|---|
static java.lang.StringBuilder |
box(boolean value)
Returns a
StringBuilder containing the text representation of the specified primitive value. |
static java.lang.StringBuilder |
box(byte value)
Returns a
StringBuilder containing the text representation of the specified primitive value. |
static java.lang.StringBuilder |
box(char value)
Returns a
StringBuilder containing the text representation of the specified primitive value. |
static java.lang.StringBuilder |
box(double value)
Returns a
StringBuilder containing the text representation of the specified primitive value. |
static java.lang.StringBuilder |
box(float value)
Returns a
StringBuilder containing the text representation of the specified primitive value. |
static java.lang.StringBuilder |
box(int value)
Returns a
StringBuilder containing the text representation of the specified primitive value. |
static java.lang.StringBuilder |
box(long value)
Returns a
StringBuilder containing the text representation of the specified primitive value. |
static java.lang.StringBuilder |
box(short value)
Returns a
StringBuilder containing the text representation of the specified primitive value. |
private static int |
calculateRingBufferSize(java.lang.String propertyName) |
private static int |
ceilingNextPowerOfTwo(int x)
Calculate the next power of 2, greater than or equal to x.
|
(package private) static int |
getRingbufferSize()
For testing.
|
private static java.lang.StringBuilder |
getSB() |
private static Unbox.State |
getState() |
private static final Logger LOGGER
private static final int BITS_PER_INT
private static final int RINGBUFFER_MIN_SIZE
private static final int RINGBUFFER_SIZE
private static final int MASK
private static java.lang.ThreadLocal<Unbox.State> threadLocalState
private static Unbox.WebSafeState webSafeState
private static int calculateRingBufferSize(java.lang.String propertyName)
private static int ceilingNextPowerOfTwo(int x)
From Hacker's Delight, Chapter 3, Harry S. Warren Jr.
x
- Value to round uppublic static java.lang.StringBuilder box(float value)
StringBuilder
containing the text representation of the specified primitive value.
This method will not allocate temporary objects.value
- the value whose text representation to returnStringBuilder
containing the text representation of the specified primitive valuepublic static java.lang.StringBuilder box(double value)
StringBuilder
containing the text representation of the specified primitive value.
This method will not allocate temporary objects.value
- the value whose text representation to returnStringBuilder
containing the text representation of the specified primitive valuepublic static java.lang.StringBuilder box(short value)
StringBuilder
containing the text representation of the specified primitive value.
This method will not allocate temporary objects.value
- the value whose text representation to returnStringBuilder
containing the text representation of the specified primitive valuepublic static java.lang.StringBuilder box(int value)
StringBuilder
containing the text representation of the specified primitive value.
This method will not allocate temporary objects.value
- the value whose text representation to returnStringBuilder
containing the text representation of the specified primitive valuepublic static java.lang.StringBuilder box(char value)
StringBuilder
containing the text representation of the specified primitive value.
This method will not allocate temporary objects.value
- the value whose text representation to returnStringBuilder
containing the text representation of the specified primitive valuepublic static java.lang.StringBuilder box(long value)
StringBuilder
containing the text representation of the specified primitive value.
This method will not allocate temporary objects.value
- the value whose text representation to returnStringBuilder
containing the text representation of the specified primitive valuepublic static java.lang.StringBuilder box(byte value)
StringBuilder
containing the text representation of the specified primitive value.
This method will not allocate temporary objects.value
- the value whose text representation to returnStringBuilder
containing the text representation of the specified primitive valuepublic static java.lang.StringBuilder box(boolean value)
StringBuilder
containing the text representation of the specified primitive value.
This method will not allocate temporary objects.value
- the value whose text representation to returnStringBuilder
containing the text representation of the specified primitive valueprivate static Unbox.State getState()
private static java.lang.StringBuilder getSB()
static int getRingbufferSize()