Skip to content

Commit a957f24

Browse files
stefanhausteinGoogle Java Core Libraries
authored andcommitted
Enable limited guava cache support (matching j2cl) for j2kt-native
RELNOTES=n/a PiperOrigin-RevId: 874014907
1 parent b7188ea commit a957f24

12 files changed

Lines changed: 191 additions & 88 deletions

File tree

android/guava-tests/test/com/google/common/cache/CacheBuilderTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import com.google.common.annotations.GwtCompatible;
3434
import com.google.common.annotations.GwtIncompatible;
35+
import com.google.common.annotations.J2ktIncompatible;
3536
import com.google.common.base.Ticker;
3637
import com.google.common.cache.TestingRemovalListeners.CountingRemovalListener;
3738
import com.google.common.cache.TestingRemovalListeners.QueuingRemovalListener;
@@ -78,6 +79,7 @@ public void testInitialCapacity_setTwice() {
7879
assertThrows(IllegalStateException.class, () -> builder.initialCapacity(16));
7980
}
8081

82+
@J2ktIncompatible
8183
@GwtIncompatible // CacheTesting
8284
public void testInitialCapacity_small() {
8385
LoadingCache<?, ?> cache = CacheBuilder.newBuilder().initialCapacity(5).build(identityLoader());
@@ -90,6 +92,7 @@ public void testInitialCapacity_small() {
9092
assertThat(map.segments[3].table.length()).isEqualTo(2);
9193
}
9294

95+
@J2ktIncompatible
9396
@GwtIncompatible // CacheTesting
9497
public void testInitialCapacity_smallest() {
9598
LoadingCache<?, ?> cache = CacheBuilder.newBuilder().initialCapacity(0).build(identityLoader());
@@ -119,6 +122,7 @@ public void testConcurrencyLevel_setTwice() {
119122
assertThrows(IllegalStateException.class, () -> builder.concurrencyLevel(16));
120123
}
121124

125+
@J2ktIncompatible
122126
@GwtIncompatible // CacheTesting
123127
public void testConcurrencyLevel_small() {
124128
LoadingCache<?, ?> cache =
@@ -142,12 +146,14 @@ public void testMaximumSize_setTwice() {
142146
assertThrows(IllegalStateException.class, () -> builder.maximumSize(16));
143147
}
144148

149+
@J2ktIncompatible
145150
@GwtIncompatible // maximumWeight
146151
public void testMaximumSize_andWeight() {
147152
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().maximumSize(16);
148153
assertThrows(IllegalStateException.class, () -> builder.maximumWeight(16));
149154
}
150155

156+
@J2ktIncompatible
151157
@GwtIncompatible // digs into internals of the non-GWT implementation
152158
public void testMaximumSize_largerThanInt() {
153159
CacheBuilder<Object, Object> builder =
@@ -156,31 +162,36 @@ public void testMaximumSize_largerThanInt() {
156162
assertThat(cache.segments.length * cache.segments[0].table.length()).isEqualTo(512);
157163
}
158164

165+
@J2ktIncompatible
159166
@GwtIncompatible // maximumWeight
160167
public void testMaximumWeight_negative() {
161168
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
162169
assertThrows(IllegalArgumentException.class, () -> builder.maximumWeight(-1));
163170
}
164171

172+
@J2ktIncompatible
165173
@GwtIncompatible // maximumWeight
166174
public void testMaximumWeight_setTwice() {
167175
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().maximumWeight(16);
168176
assertThrows(IllegalStateException.class, () -> builder.maximumWeight(16));
169177
assertThrows(IllegalStateException.class, () -> builder.maximumSize(16));
170178
}
171179

180+
@J2ktIncompatible
172181
@GwtIncompatible // maximumWeight
173182
public void testMaximumWeight_withoutWeigher() {
174183
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().maximumWeight(1);
175184
assertThrows(IllegalStateException.class, () -> builder.build(identityLoader()));
176185
}
177186

187+
@J2ktIncompatible
178188
@GwtIncompatible // weigher
179189
public void testWeigher_withoutMaximumWeight() {
180190
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().weigher(constantWeigher(42));
181191
assertThrows(IllegalStateException.class, () -> builder.build(identityLoader()));
182192
}
183193

194+
@J2ktIncompatible
184195
@GwtIncompatible // weigher
185196
public void testWeigher_withMaximumSize() {
186197
assertThrows(
@@ -191,12 +202,14 @@ public void testWeigher_withMaximumSize() {
191202
() -> CacheBuilder.newBuilder().maximumSize(1).weigher(constantWeigher(42)));
192203
}
193204

205+
@J2ktIncompatible
194206
@GwtIncompatible // weakKeys
195207
public void testKeyStrengthSetTwice() {
196208
CacheBuilder<Object, Object> builder1 = CacheBuilder.newBuilder().weakKeys();
197209
assertThrows(IllegalStateException.class, () -> builder1.weakKeys());
198210
}
199211

212+
@J2ktIncompatible
200213
@GwtIncompatible // weakValues
201214
public void testValueStrengthSetTwice() {
202215
CacheBuilder<Object, Object> builder1 = CacheBuilder.newBuilder().weakValues();
@@ -208,6 +221,7 @@ public void testValueStrengthSetTwice() {
208221
assertThrows(IllegalStateException.class, () -> builder2.weakValues());
209222
}
210223

224+
@J2ktIncompatible
211225
@GwtIncompatible // Duration
212226
@IgnoreJRERequirement // No more dangerous than wherever the caller got the Duration from
213227
public void testLargeDurationsAreOk() {
@@ -224,6 +238,7 @@ public void testTimeToLive_negative() {
224238
assertThrows(IllegalArgumentException.class, () -> builder.expireAfterWrite(-1, SECONDS));
225239
}
226240

241+
@J2ktIncompatible
227242
@GwtIncompatible // Duration
228243
public void testTimeToLive_negative_duration() {
229244
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
@@ -243,6 +258,7 @@ public void testTimeToLive_setTwice() {
243258
assertThrows(IllegalStateException.class, () -> builder.expireAfterWrite(3600, SECONDS));
244259
}
245260

261+
@J2ktIncompatible
246262
@GwtIncompatible // Duration
247263
public void testTimeToLive_setTwice_duration() {
248264
CacheBuilder<Object, Object> builder =
@@ -255,6 +271,7 @@ public void testTimeToIdle_negative() {
255271
assertThrows(IllegalArgumentException.class, () -> builder.expireAfterAccess(-1, SECONDS));
256272
}
257273

274+
@J2ktIncompatible
258275
@GwtIncompatible // Duration
259276
public void testTimeToIdle_negative_duration() {
260277
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
@@ -274,6 +291,7 @@ public void testTimeToIdle_setTwice() {
274291
assertThrows(IllegalStateException.class, () -> builder.expireAfterAccess(3600, SECONDS));
275292
}
276293

294+
@J2ktIncompatible
277295
@GwtIncompatible // Duration
278296
public void testTimeToIdle_setTwice_duration() {
279297
CacheBuilder<Object, Object> builder =
@@ -290,25 +308,29 @@ public void testTimeToIdleAndToLive() {
290308
// well, it didn't blow up.
291309
}
292310

311+
@J2ktIncompatible
293312
@GwtIncompatible // refreshAfterWrite
294313
public void testRefresh_zero() {
295314
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
296315
assertThrows(IllegalArgumentException.class, () -> builder.refreshAfterWrite(0, SECONDS));
297316
}
298317

318+
@J2ktIncompatible
299319
@GwtIncompatible // Duration
300320
public void testRefresh_zero_duration() {
301321
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
302322
assertThrows(IllegalArgumentException.class, () -> builder.refreshAfterWrite(Duration.ZERO));
303323
}
304324

325+
@J2ktIncompatible
305326
@GwtIncompatible // refreshAfterWrite
306327
public void testRefresh_setTwice() {
307328
CacheBuilder<Object, Object> builder =
308329
CacheBuilder.newBuilder().refreshAfterWrite(3600, SECONDS);
309330
assertThrows(IllegalStateException.class, () -> builder.refreshAfterWrite(3600, SECONDS));
310331
}
311332

333+
@J2ktIncompatible
312334
@GwtIncompatible // Duration
313335
public void testRefresh_setTwice_duration() {
314336
CacheBuilder<Object, Object> builder =
@@ -332,6 +354,7 @@ public void testValuesIsNotASet() {
332354
assertThat(CacheBuilder.newBuilder().build().asMap().values() instanceof Set).isFalse();
333355
}
334356

357+
@J2ktIncompatible
335358
@GwtIncompatible // CacheTesting
336359
public void testNullCache() {
337360
CountingRemovalListener<Object, Object> listener = countingRemovalListener();
@@ -345,6 +368,7 @@ public void testNullCache() {
345368
CacheTesting.checkEmpty(nullCache.asMap());
346369
}
347370

371+
@J2ktIncompatible
348372
@GwtIncompatible // QueuingRemovalListener
349373
public void testRemovalNotification_clear() throws InterruptedException {
350374
// If a clear() happens while a computation is pending, we should not get a removal
@@ -415,6 +439,7 @@ public void run() {
415439
* removal listener), or else is not affected by the {@code clear()} (and therefore exists in the
416440
* cache afterward).
417441
*/
442+
@J2ktIncompatible
418443
@GwtIncompatible // QueuingRemovalListener
419444

420445
@SuppressWarnings("ThreadPriorityCheck") // TODO: b/175898629 - Consider onSpinWait.
@@ -502,6 +527,7 @@ public void run() {
502527
* Calls get() repeatedly from many different threads, and tests that all of the removed entries
503528
* (removed because of size limits or expiration) trigger appropriate removal notifications.
504529
*/
530+
@J2ktIncompatible
505531
@GwtIncompatible // QueuingRemovalListener
506532

507533
public void testRemovalNotification_get_basher() throws InterruptedException {
@@ -585,13 +611,15 @@ public void run() {
585611
assertThat(cache.size() + removalListener.size()).isEqualTo(computeCount.get());
586612
}
587613

614+
@J2ktIncompatible
588615
@GwtIncompatible // NullPointerTester
589616
public void testNullParameters() throws Exception {
590617
NullPointerTester tester = new NullPointerTester();
591618
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
592619
tester.testAllPublicInstanceMethods(builder);
593620
}
594621

622+
@J2ktIncompatible
595623
@GwtIncompatible // CacheTesting
596624
public void testSizingDefaults() {
597625
LoadingCache<?, ?> cache = CacheBuilder.newBuilder().build(identityLoader());
@@ -600,6 +628,7 @@ public void testSizingDefaults() {
600628
assertThat(map.segments[0].table.length()).isEqualTo(4); // capacity / conc level
601629
}
602630

631+
@J2ktIncompatible
603632
@GwtIncompatible // CountDownLatch
604633
static final class DelayingIdentityLoader<T> extends CacheLoader<T, T> {
605634
private final AtomicBoolean shouldWait;

android/guava-tests/test/com/google/common/cache/TestingCacheLoaders.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.annotations.GwtCompatible;
2121
import com.google.common.annotations.GwtIncompatible;
22+
import com.google.common.annotations.J2ktIncompatible;
2223
import com.google.common.util.concurrent.ListenableFuture;
2324
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2425
import java.util.HashMap;
@@ -144,6 +145,7 @@ public Integer load(Integer key) {
144145
return key;
145146
}
146147

148+
@J2ktIncompatible
147149
@GwtIncompatible // reload
148150
@Override
149151
public ListenableFuture<Integer> reload(Integer key, Integer oldValue) {

android/guava-tests/test/com/google/common/cache/TestingRemovalListeners.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616

1717
import com.google.common.annotations.GwtCompatible;
1818
import com.google.common.annotations.GwtIncompatible;
19+
import com.google.common.annotations.J2ktIncompatible;
1920
import java.util.concurrent.ConcurrentLinkedQueue;
2021
import java.util.concurrent.atomic.AtomicInteger;
21-
import org.jspecify.annotations.NullUnmarked;
22+
import org.jspecify.annotations.Nullable;
2223

2324
/**
2425
* Utility {@link RemovalListener} implementations intended for use in testing.
2526
*
2627
* @author mike nonemacher
2728
*/
2829
@GwtCompatible
29-
@NullUnmarked
3030
final class TestingRemovalListeners {
3131

3232
/** Returns a new no-op {@code RemovalListener}. */
@@ -35,6 +35,7 @@ static <K, V> NullRemovalListener<K, V> nullRemovalListener() {
3535
}
3636

3737
/** Type-inferring factory method for creating a {@link QueuingRemovalListener}. */
38+
@J2ktIncompatible
3839
@GwtIncompatible // ConcurrentLinkedQueue
3940
static <K, V> QueuingRemovalListener<K, V> queuingRemovalListener() {
4041
return new QueuingRemovalListener<>();
@@ -46,6 +47,7 @@ static <K, V> CountingRemovalListener<K, V> countingRemovalListener() {
4647
}
4748

4849
/** {@link RemovalListener} that adds all {@link RemovalNotification} objects to a queue. */
50+
@J2ktIncompatible
4951
@GwtIncompatible // ConcurrentLinkedQueue
5052
static class QueuingRemovalListener<K, V> extends ConcurrentLinkedQueue<RemovalNotification<K, V>>
5153
implements RemovalListener<K, V> {
@@ -62,7 +64,7 @@ public void onRemoval(RemovalNotification<K, V> notification) {
6264
*/
6365
static class CountingRemovalListener<K, V> implements RemovalListener<K, V> {
6466
private final AtomicInteger count = new AtomicInteger();
65-
private volatile RemovalNotification<K, V> lastNotification;
67+
private volatile @Nullable RemovalNotification<K, V> lastNotification;
6668

6769
@Override
6870
public void onRemoval(RemovalNotification<K, V> notification) {

android/guava/src/com/google/common/cache/CacheBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.annotations.GwtCompatible;
2323
import com.google.common.annotations.GwtIncompatible;
24+
import com.google.common.annotations.J2ktIncompatible;
2425
import com.google.common.base.Ascii;
2526
import com.google.common.base.Equivalence;
2627
import com.google.common.base.MoreObjects;
@@ -723,6 +724,7 @@ Strength getValueStrength() {
723724
*/
724725
@J2ObjCIncompatible
725726
@GwtIncompatible // Duration
727+
@J2ktIncompatible
726728
@SuppressWarnings("GoodTime") // Duration decomposition
727729
@IgnoreJRERequirement // No more dangerous than wherever the caller got the Duration from
728730
@CanIgnoreReturnValue
@@ -797,6 +799,7 @@ long getExpireAfterWriteNanos() {
797799
* href="https://github.com/google/guava#guava-google-core-libraries-for-java">flavor</a>)
798800
*/
799801
@J2ObjCIncompatible
802+
@J2ktIncompatible
800803
@GwtIncompatible // Duration
801804
@SuppressWarnings("GoodTime") // Duration decomposition
802805
@IgnoreJRERequirement // No more dangerous than wherever the caller got the Duration from
@@ -881,6 +884,7 @@ long getExpireAfterAccessNanos() {
881884
* href="https://github.com/google/guava#guava-google-core-libraries-for-java">flavor</a>)
882885
*/
883886
@J2ObjCIncompatible
887+
@J2ktIncompatible
884888
@GwtIncompatible // Duration
885889
@SuppressWarnings("GoodTime") // Duration decomposition
886890
@IgnoreJRERequirement // No more dangerous than wherever the caller got the Duration from
@@ -1126,6 +1130,7 @@ public String toString() {
11261130
* {@link Long#MAX_VALUE} or {@link Long#MIN_VALUE}. This behavior can be useful when decomposing
11271131
* a duration in order to call a legacy API which requires a {@code long, TimeUnit} pair.
11281132
*/
1133+
@J2ktIncompatible
11291134
@GwtIncompatible // Duration
11301135
@SuppressWarnings("GoodTime") // Duration decomposition
11311136
@IgnoreJRERequirement // No more dangerous than wherever the caller got the Duration from

android/guava/src/com/google/common/cache/ReferenceEntry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.common.cache;
1616

1717
import com.google.common.annotations.GwtIncompatible;
18+
import com.google.common.annotations.J2ktIncompatible;
1819
import com.google.common.cache.LocalCache.ValueReference;
1920
import org.jspecify.annotations.Nullable;
2021

@@ -39,6 +40,7 @@
3940
* </ul>
4041
*/
4142
@GwtIncompatible
43+
@J2ktIncompatible
4244
interface ReferenceEntry<K, V> {
4345
/** Returns the value reference from this entry. */
4446
@Nullable ValueReference<K, V> getValueReference();

0 commit comments

Comments
 (0)