77import java .io .Serializable ;
88import java .util .concurrent .atomic .LongAdder ;
99
10+ import org .checkerframework .checker .nullness .qual .Nullable ;
1011import org .hibernate .cache .spi .ExtendedStatisticsSupport ;
1112import org .hibernate .cache .spi .Region ;
1213import org .hibernate .stat .CacheRegionStatistics ;
1718 * @author Alex Snaps
1819 */
1920public class CacheRegionStatisticsImpl implements CacheRegionStatistics , Serializable {
20- private final transient Region region ;
21+ private final String regionName ;
22+ private final transient @ Nullable ExtendedStatisticsSupport extendedStatisticsSupport ;
2123
2224 private final LongAdder hitCount = new LongAdder ();
2325 private final LongAdder missCount = new LongAdder ();
2426 private final LongAdder putCount = new LongAdder ();
2527 private final LongAdder removeCount = new LongAdder ();
2628
2729 CacheRegionStatisticsImpl (Region region ) {
28- this .region = region ;
30+ regionName = region .getName ();
31+ extendedStatisticsSupport =
32+ region instanceof ExtendedStatisticsSupport extended
33+ ? extended
34+ : null ;
2935 }
3036
3137 @ Override
3238 public String getRegionName () {
33- return region . getName () ;
39+ return regionName ;
3440 }
3541
3642 @ Override
@@ -55,23 +61,23 @@ public long getRemoveCount() {
5561
5662 @ Override
5763 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 () ;
6167 }
6268
6369 @ Override
6470 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 () ;
6874 }
6975
7076 @ Override
7177 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 () ;
7581 }
7682
7783 void incrementHitCount () {
@@ -93,15 +99,18 @@ public void incrementRemoveCount() {
9399
94100 @ Override
95101 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 ();
106115 }
107116}
0 commit comments