|
7 | 7 | import java.lang.reflect.Modifier; |
8 | 8 | import java.math.BigDecimal; |
9 | 9 | import java.util.List; |
| 10 | +import java.util.Map; |
10 | 11 | import java.util.Objects; |
| 12 | +import java.util.concurrent.ConcurrentHashMap; |
11 | 13 |
|
12 | 14 | import ng.NGRuntimeException; |
13 | 15 |
|
@@ -114,9 +116,45 @@ public static void takeValueForKey( final Object object, final Object value, fin |
114 | 116 | } |
115 | 117 |
|
116 | 118 | /** |
117 | | - * @return A KVC binding for the given class and key. |
| 119 | + * FIXME: Most definitely not the way we're going to use to set if KVC caching is enabled // Hugi 2025-04-21 |
| 120 | + */ |
| 121 | + public static void setCachingEnabled( boolean value ) { |
| 122 | + _cachingEnabled = value; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * FIXME: Most definitely not the way we're going to use to decide if KVC caching is enabled // Hugi 2025-04-21 |
118 | 127 | */ |
| 128 | + public static boolean _cachingEnabled = true; |
| 129 | + |
| 130 | + /** |
| 131 | + * FIXME: Currently just a very primitive cache for testing // Hugi 2025-04-21 |
| 132 | + */ |
| 133 | + private static Map<CacheKey, KVCReadBinding> _readBindingCache = new ConcurrentHashMap<>(); |
| 134 | + |
| 135 | + private record CacheKey( Class<?> clazz, String key ) {} |
| 136 | + |
119 | 137 | private static KVCReadBinding readBindingForKey( final Object object, final String key ) { |
| 138 | + |
| 139 | + if( !_cachingEnabled ) { |
| 140 | + return locateBindingForKey( object, key ); |
| 141 | + } |
| 142 | + |
| 143 | + final CacheKey cacheKey = new CacheKey( object.getClass(), key ); |
| 144 | + KVCReadBinding entry = _readBindingCache.get( cacheKey ); |
| 145 | + |
| 146 | + if( entry == null ) { |
| 147 | + entry = locateBindingForKey( object, key ); |
| 148 | + _readBindingCache.put( cacheKey, entry ); |
| 149 | + } |
| 150 | + |
| 151 | + return entry; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @return A KVC binding for the given class and key. |
| 156 | + */ |
| 157 | + private static KVCReadBinding locateBindingForKey( final Object object, final String key ) { |
120 | 158 | Objects.requireNonNull( object ); |
121 | 159 | Objects.requireNonNull( key ); |
122 | 160 |
|
|
0 commit comments