7
7
import java .io .Serializable ;
8
8
import java .util .concurrent .atomic .LongAdder ;
9
9
10
+ import org .checkerframework .checker .nullness .qual .Nullable ;
10
11
import org .hibernate .cache .spi .ExtendedStatisticsSupport ;
11
12
import org .hibernate .cache .spi .Region ;
12
13
import org .hibernate .stat .CacheRegionStatistics ;
17
18
* @author Alex Snaps
18
19
*/
19
20
public class CacheRegionStatisticsImpl implements CacheRegionStatistics , Serializable {
20
- private final transient Region region ;
21
+ private final String regionName ;
22
+ private final transient @ Nullable ExtendedStatisticsSupport extendedStatisticsSupport ;
21
23
22
24
private final LongAdder hitCount = new LongAdder ();
23
25
private final LongAdder missCount = new LongAdder ();
24
26
private final LongAdder putCount = new LongAdder ();
25
27
private final LongAdder removeCount = new LongAdder ();
26
28
27
29
CacheRegionStatisticsImpl (Region region ) {
28
- this .region = region ;
30
+ regionName = region .getName ();
31
+ extendedStatisticsSupport =
32
+ region instanceof ExtendedStatisticsSupport extended
33
+ ? extended
34
+ : null ;
29
35
}
30
36
31
37
@ Override
32
38
public String getRegionName () {
33
- return region . getName () ;
39
+ return regionName ;
34
40
}
35
41
36
42
@ Override
@@ -55,23 +61,23 @@ public long getRemoveCount() {
55
61
56
62
@ Override
57
63
public long getElementCountInMemory () {
58
- return region instanceof ExtendedStatisticsSupport extended
59
- ? extended . getElementCountInMemory ()
60
- : NO_EXTENDED_STAT_SUPPORT_RETURN ;
64
+ return extendedStatisticsSupport == null
65
+ ? NO_EXTENDED_STAT_SUPPORT_RETURN
66
+ : extendedStatisticsSupport . getElementCountInMemory () ;
61
67
}
62
68
63
69
@ Override
64
70
public long getElementCountOnDisk () {
65
- return region instanceof ExtendedStatisticsSupport extended
66
- ? extended . getElementCountOnDisk ()
67
- : NO_EXTENDED_STAT_SUPPORT_RETURN ;
71
+ return extendedStatisticsSupport == null
72
+ ? NO_EXTENDED_STAT_SUPPORT_RETURN
73
+ : extendedStatisticsSupport . getElementCountOnDisk () ;
68
74
}
69
75
70
76
@ Override
71
77
public long getSizeInMemory () {
72
- return region instanceof ExtendedStatisticsSupport extended
73
- ? extended . getSizeInMemory ()
74
- : NO_EXTENDED_STAT_SUPPORT_RETURN ;
78
+ return extendedStatisticsSupport == null
79
+ ? NO_EXTENDED_STAT_SUPPORT_RETURN
80
+ : extendedStatisticsSupport . getSizeInMemory () ;
75
81
}
76
82
77
83
void incrementHitCount () {
@@ -93,15 +99,18 @@ public void incrementRemoveCount() {
93
99
94
100
@ Override
95
101
public String toString () {
96
- return "CacheRegionStatistics"
97
- + "[region=" + region .getName ()
98
- + ",hitCount=" + hitCount
99
- + ",missCount=" + missCount
100
- + ",putCount=" + putCount
101
- + ",removeCount=" + removeCount
102
- + ",elementCountInMemory=" + getElementCountInMemory ()
103
- + ",elementCountOnDisk=" + getElementCountOnDisk ()
104
- + ",sizeInMemory=" + getSizeInMemory ()
105
- + ']' ;
102
+ final var string =
103
+ new StringBuilder ("CacheRegionStatistics" )
104
+ .append ( "[region=" ).append ( regionName )
105
+ .append ( ",hitCount=" ).append ( hitCount )
106
+ .append ( ",missCount=" ).append ( missCount )
107
+ .append ( ",putCount=" ).append ( putCount )
108
+ .append ( ",removeCount=" ).append ( removeCount );
109
+ if ( extendedStatisticsSupport != null ) {
110
+ string .append ( ",elementCountInMemory=" ).append ( getElementCountInMemory () )
111
+ .append ( ",elementCountOnDisk=" ).append ( getElementCountOnDisk () )
112
+ .append ( ",sizeInMemory=" ).append ( getSizeInMemory () );
113
+ }
114
+ return string .append ( ']' ).toString ();
106
115
}
107
116
}
0 commit comments