How to use WeakConcurrentMap class of org.mockito.internal.util.concurrent package

Best Mockito code snippet using org.mockito.internal.util.concurrent.WeakConcurrentMap

Source:WeakConcurrentMap.java Github

copy

Full Screen

...7import java.util.NoSuchElementException;8import java.util.concurrent.ConcurrentHashMap;9import java.util.concurrent.ConcurrentMap;10import java.util.concurrent.atomic.AtomicLong;11public class WeakConcurrentMap<K, V> extends ReferenceQueue<K> implements Runnable, Iterable<Map.Entry<K, V>> {12 private static final AtomicLong ID = new AtomicLong();13 public final ConcurrentMap<WeakKey<K>, V> target = new ConcurrentHashMap();14 private final Thread thread;15 /* access modifiers changed from: protected */16 public V defaultValue(K k) {17 return null;18 }19 public WeakConcurrentMap(boolean z) {20 if (z) {21 Thread thread2 = new Thread(this);22 this.thread = thread2;23 thread2.setName("weak-ref-cleaner-" + ID.getAndIncrement());24 this.thread.setPriority(1);25 this.thread.setDaemon(true);26 this.thread.start();27 return;28 }29 this.thread = null;30 }31 /* JADX WARNING: Code restructure failed: missing block: B:5:0x0015, code lost:32 r4 = r3.target.putIfAbsent(new org.mockito.internal.util.concurrent.WeakConcurrentMap.WeakKey(r4, r3), r0);33 */34 /* Code decompiled incorrectly, please refer to instructions dump. */35 public V get(K r4) {36 /*37 r3 = this;38 if (r4 == 0) goto L_0x002439 java.util.concurrent.ConcurrentMap<org.mockito.internal.util.concurrent.WeakConcurrentMap$WeakKey<K>, V> r0 = r3.target40 org.mockito.internal.util.concurrent.WeakConcurrentMap$LatentKey r1 = new org.mockito.internal.util.concurrent.WeakConcurrentMap$LatentKey41 r1.<init>(r4)42 java.lang.Object r0 = r0.get(r1)43 if (r0 != 0) goto L_0x002344 java.lang.Object r0 = r3.defaultValue(r4)45 if (r0 == 0) goto L_0x002346 java.util.concurrent.ConcurrentMap<org.mockito.internal.util.concurrent.WeakConcurrentMap$WeakKey<K>, V> r1 = r3.target47 org.mockito.internal.util.concurrent.WeakConcurrentMap$WeakKey r2 = new org.mockito.internal.util.concurrent.WeakConcurrentMap$WeakKey48 r2.<init>(r4, r3)49 java.lang.Object r4 = r1.putIfAbsent(r2, r0)50 if (r4 == 0) goto L_0x002351 r0 = r452 L_0x0023:53 return r054 L_0x0024:55 r4 = 056 throw r457 */58 throw new UnsupportedOperationException("Method not decompiled: org.mockito.internal.util.concurrent.WeakConcurrentMap.get(java.lang.Object):java.lang.Object");59 }60 public boolean containsKey(K k) {61 if (k != null) {62 return this.target.containsKey(new LatentKey(k));63 }64 throw null;65 }66 public V put(K k, V v) {67 if (k != null && v != null) {68 return this.target.put(new WeakKey(k, this), v);69 }70 throw null;71 }72 public V remove(K k) {73 if (k != null) {74 return this.target.remove(new LatentKey(k));75 }76 throw null;77 }78 public void clear() {79 this.target.clear();80 }81 public Thread getCleanerThread() {82 return this.thread;83 }84 public void expungeStaleEntries() {85 while (true) {86 Reference poll = poll();87 if (poll != null) {88 this.target.remove(poll);89 } else {90 return;91 }92 }93 }94 public int approximateSize() {95 return this.target.size();96 }97 public void run() {98 while (true) {99 try {100 this.target.remove(remove());101 } catch (InterruptedException unused) {102 clear();103 return;104 }105 }106 }107 public Iterator<Map.Entry<K, V>> iterator() {108 return new EntryIterator(this.target.entrySet().iterator());109 }110 private static class WeakKey<T> extends WeakReference<T> {111 private final int hashCode;112 WeakKey(T t, ReferenceQueue<? super T> referenceQueue) {113 super(t, referenceQueue);114 this.hashCode = System.identityHashCode(t);115 }116 public int hashCode() {117 return this.hashCode;118 }119 public boolean equals(Object obj) {120 if (obj instanceof LatentKey) {121 if (((LatentKey) obj).key == get()) {122 return true;123 }124 return false;125 } else if (((WeakKey) obj).get() == get()) {126 return true;127 } else {128 return false;129 }130 }131 }132 private static class LatentKey<T> {133 private final int hashCode;134 final T key;135 LatentKey(T t) {136 this.key = t;137 this.hashCode = System.identityHashCode(t);138 }139 public boolean equals(Object obj) {140 if (obj instanceof LatentKey) {141 if (((LatentKey) obj).key == this.key) {142 return true;143 }144 return false;145 } else if (((WeakKey) obj).get() == this.key) {146 return true;147 } else {148 return false;149 }150 }151 public int hashCode() {152 return this.hashCode;153 }154 }155 public static class WithInlinedExpunction<K, V> extends WeakConcurrentMap<K, V> {156 public WithInlinedExpunction() {157 super(false);158 }159 public V get(K k) {160 expungeStaleEntries();161 return WeakConcurrentMap.super.get(k);162 }163 public boolean containsKey(K k) {164 expungeStaleEntries();165 return WeakConcurrentMap.super.containsKey(k);166 }167 public V put(K k, V v) {168 expungeStaleEntries();169 return WeakConcurrentMap.super.put(k, v);170 }171 public V remove(K k) {172 expungeStaleEntries();173 return WeakConcurrentMap.super.remove(k);174 }175 public Iterator<Map.Entry<K, V>> iterator() {176 expungeStaleEntries();177 return WeakConcurrentMap.super.iterator();178 }179 public int approximateSize() {180 expungeStaleEntries();181 return WeakConcurrentMap.super.approximateSize();182 }183 }184 private class EntryIterator implements Iterator<Map.Entry<K, V>> {185 private final Iterator<Map.Entry<WeakKey<K>, V>> iterator;186 private Map.Entry<WeakKey<K>, V> nextEntry;187 private K nextKey;188 private EntryIterator(Iterator<Map.Entry<WeakKey<K>, V>> it) {189 this.iterator = it;190 findNext();191 }192 private void findNext() {193 while (this.iterator.hasNext()) {194 Map.Entry<WeakKey<K>, V> next = this.iterator.next();195 this.nextEntry = next;...

Full Screen

Full Screen

Source:DetachedThreadLocal.java Github

copy

Full Screen

1package org.mockito.internal.util.concurrent;2import org.mockito.internal.util.concurrent.WeakConcurrentMap;3public class DetachedThreadLocal<T> implements Runnable {4 final WeakConcurrentMap<Thread, T> map;5 public enum Cleaner {6 THREAD,7 INLINE,8 MANUAL9 }10 /* access modifiers changed from: protected */11 public T inheritValue(T t) {12 return t;13 }14 /* access modifiers changed from: protected */15 public T initialValue(Thread thread) {16 return null;17 }18 /* renamed from: org.mockito.internal.util.concurrent.DetachedThreadLocal$3 reason: invalid class name */19 static /* synthetic */ class AnonymousClass3 {20 static final /* synthetic */ int[] $SwitchMap$org$mockito$internal$util$concurrent$DetachedThreadLocal$Cleaner;21 /* JADX WARNING: Can't wrap try/catch for region: R(6:0|1|2|3|4|(3:5|6|8)) */22 /* JADX WARNING: Failed to process nested try/catch */23 /* JADX WARNING: Missing exception handler attribute for start block: B:3:0x0012 */24 /* JADX WARNING: Missing exception handler attribute for start block: B:5:0x001d */25 static {26 /*27 org.mockito.internal.util.concurrent.DetachedThreadLocal$Cleaner[] r0 = org.mockito.internal.util.concurrent.DetachedThreadLocal.Cleaner.values()28 int r0 = r0.length29 int[] r0 = new int[r0]30 $SwitchMap$org$mockito$internal$util$concurrent$DetachedThreadLocal$Cleaner = r031 org.mockito.internal.util.concurrent.DetachedThreadLocal$Cleaner r1 = org.mockito.internal.util.concurrent.DetachedThreadLocal.Cleaner.THREAD // Catch:{ NoSuchFieldError -> 0x0012 }32 int r1 = r1.ordinal() // Catch:{ NoSuchFieldError -> 0x0012 }33 r2 = 134 r0[r1] = r2 // Catch:{ NoSuchFieldError -> 0x0012 }35 L_0x0012:36 int[] r0 = $SwitchMap$org$mockito$internal$util$concurrent$DetachedThreadLocal$Cleaner // Catch:{ NoSuchFieldError -> 0x001d }37 org.mockito.internal.util.concurrent.DetachedThreadLocal$Cleaner r1 = org.mockito.internal.util.concurrent.DetachedThreadLocal.Cleaner.MANUAL // Catch:{ NoSuchFieldError -> 0x001d }38 int r1 = r1.ordinal() // Catch:{ NoSuchFieldError -> 0x001d }39 r2 = 240 r0[r1] = r2 // Catch:{ NoSuchFieldError -> 0x001d }41 L_0x001d:42 int[] r0 = $SwitchMap$org$mockito$internal$util$concurrent$DetachedThreadLocal$Cleaner // Catch:{ NoSuchFieldError -> 0x0028 }43 org.mockito.internal.util.concurrent.DetachedThreadLocal$Cleaner r1 = org.mockito.internal.util.concurrent.DetachedThreadLocal.Cleaner.INLINE // Catch:{ NoSuchFieldError -> 0x0028 }44 int r1 = r1.ordinal() // Catch:{ NoSuchFieldError -> 0x0028 }45 r2 = 346 r0[r1] = r2 // Catch:{ NoSuchFieldError -> 0x0028 }47 L_0x0028:48 return49 */50 throw new UnsupportedOperationException("Method not decompiled: org.mockito.internal.util.concurrent.DetachedThreadLocal.AnonymousClass3.<clinit>():void");51 }52 }53 public DetachedThreadLocal(Cleaner cleaner) {54 int i = AnonymousClass3.$SwitchMap$org$mockito$internal$util$concurrent$DetachedThreadLocal$Cleaner[cleaner.ordinal()];55 boolean z = true;56 if (i == 1 || i == 2) {57 this.map = new WeakConcurrentMap<Thread, T>(cleaner != Cleaner.THREAD ? false : z) {58 /* access modifiers changed from: protected */59 public T defaultValue(Thread thread) {60 return DetachedThreadLocal.this.initialValue(thread);61 }62 };63 } else if (i == 3) {64 this.map = new WeakConcurrentMap.WithInlinedExpunction<Thread, T>() {65 /* access modifiers changed from: protected */66 public T defaultValue(Thread thread) {67 return DetachedThreadLocal.this.initialValue(thread);68 }69 };70 } else {71 throw new AssertionError();72 }73 }74 public T get() {75 return this.map.get(Thread.currentThread());76 }77 public void set(T t) {78 this.map.put(Thread.currentThread(), t);79 }80 public void clear() {81 this.map.remove(Thread.currentThread());82 }83 public void clearAll() {84 this.map.clear();85 }86 public T pushTo(Thread thread) {87 T t = get();88 if (t != null) {89 this.map.put(thread, inheritValue(t));90 }91 return t;92 }93 public T fetchFrom(Thread thread) {94 T t = this.map.get(thread);95 if (t != null) {96 set(inheritValue(t));97 }98 return t;99 }100 public T get(Thread thread) {101 return this.map.get(thread);102 }103 public void define(Thread thread, T t) {104 this.map.put(thread, t);105 }106 public WeakConcurrentMap<Thread, T> getBackingMap() {107 return this.map;108 }109 public void run() {110 this.map.run();111 }112}...

Full Screen

Full Screen

Source:WeakConcurrentSet.java Github

copy

Full Screen

1package org.mockito.internal.util.concurrent;2import java.util.Iterator;3import java.util.Map;4import org.mockito.internal.util.concurrent.WeakConcurrentMap;5public class WeakConcurrentSet<V> implements Runnable, Iterable<V> {6 final WeakConcurrentMap<V, Boolean> target;7 public enum Cleaner {8 THREAD,9 INLINE,10 MANUAL11 }12 /* renamed from: org.mockito.internal.util.concurrent.WeakConcurrentSet$1 reason: invalid class name */13 static /* synthetic */ class AnonymousClass1 {14 static final /* synthetic */ int[] $SwitchMap$org$mockito$internal$util$concurrent$WeakConcurrentSet$Cleaner;15 /* JADX WARNING: Can't wrap try/catch for region: R(6:0|1|2|3|4|(3:5|6|8)) */16 /* JADX WARNING: Failed to process nested try/catch */17 /* JADX WARNING: Missing exception handler attribute for start block: B:3:0x0012 */18 /* JADX WARNING: Missing exception handler attribute for start block: B:5:0x001d */19 static {20 /*21 org.mockito.internal.util.concurrent.WeakConcurrentSet$Cleaner[] r0 = org.mockito.internal.util.concurrent.WeakConcurrentSet.Cleaner.values()22 int r0 = r0.length23 int[] r0 = new int[r0]24 $SwitchMap$org$mockito$internal$util$concurrent$WeakConcurrentSet$Cleaner = r025 org.mockito.internal.util.concurrent.WeakConcurrentSet$Cleaner r1 = org.mockito.internal.util.concurrent.WeakConcurrentSet.Cleaner.INLINE // Catch:{ NoSuchFieldError -> 0x0012 }26 int r1 = r1.ordinal() // Catch:{ NoSuchFieldError -> 0x0012 }27 r2 = 128 r0[r1] = r2 // Catch:{ NoSuchFieldError -> 0x0012 }29 L_0x0012:30 int[] r0 = $SwitchMap$org$mockito$internal$util$concurrent$WeakConcurrentSet$Cleaner // Catch:{ NoSuchFieldError -> 0x001d }31 org.mockito.internal.util.concurrent.WeakConcurrentSet$Cleaner r1 = org.mockito.internal.util.concurrent.WeakConcurrentSet.Cleaner.THREAD // Catch:{ NoSuchFieldError -> 0x001d }32 int r1 = r1.ordinal() // Catch:{ NoSuchFieldError -> 0x001d }33 r2 = 234 r0[r1] = r2 // Catch:{ NoSuchFieldError -> 0x001d }35 L_0x001d:36 int[] r0 = $SwitchMap$org$mockito$internal$util$concurrent$WeakConcurrentSet$Cleaner // Catch:{ NoSuchFieldError -> 0x0028 }37 org.mockito.internal.util.concurrent.WeakConcurrentSet$Cleaner r1 = org.mockito.internal.util.concurrent.WeakConcurrentSet.Cleaner.MANUAL // Catch:{ NoSuchFieldError -> 0x0028 }38 int r1 = r1.ordinal() // Catch:{ NoSuchFieldError -> 0x0028 }39 r2 = 340 r0[r1] = r2 // Catch:{ NoSuchFieldError -> 0x0028 }41 L_0x0028:42 return43 */44 throw new UnsupportedOperationException("Method not decompiled: org.mockito.internal.util.concurrent.WeakConcurrentSet.AnonymousClass1.<clinit>():void");45 }46 }47 public WeakConcurrentSet(Cleaner cleaner) {48 int i = AnonymousClass1.$SwitchMap$org$mockito$internal$util$concurrent$WeakConcurrentSet$Cleaner[cleaner.ordinal()];49 boolean z = true;50 if (i == 1) {51 this.target = new WeakConcurrentMap.WithInlinedExpunction();52 } else if (i == 2 || i == 3) {53 this.target = new WeakConcurrentMap<>(cleaner != Cleaner.THREAD ? false : z);54 } else {55 throw new AssertionError();56 }57 }58 public boolean add(V v) {59 return this.target.put(v, Boolean.TRUE) == null;60 }61 public boolean contains(V v) {62 return this.target.containsKey(v);63 }64 public boolean remove(V v) {65 return this.target.remove(v).booleanValue();66 }67 public void clear() {...

Full Screen

Full Screen

WeakConcurrentMap

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.util.concurrent.WeakConcurrentMap;2import java.util.concurrent.ConcurrentMap;3public class 1 {4public static void main(String[] args) {5ConcurrentMap map = new WeakConcurrentMap();6map.put("key1", "value1");7map.put("key2", "value2");8map.put("key3", "value3");9map.put("key4", "value4");10System.out.println(map);11}12}13{key1=value1, key2=value2, key3=value3, key4=value4}14import org.mockito.internal.util.concurrent.WeakConcurrentSet;15import java.util.Set;16public class 2 {17public static void main(String[] args) {18Set set = new WeakConcurrentSet();19set.add("value1");20set.add("value2");21set.add("value3");22set.add("value4");23System.out.println(set);24}25}26import org.mockito.internal.util.concurrent.WeakConcurrentMap;27import java.util.concurrent.ConcurrentMap;28public class 3 {29public static void main(String[] args) {30ConcurrentMap map = new WeakConcurrentMap();31map.put("key1", "value1");32map.put("key2", "value2");33map.put("key3", "value3");34map.put("key4", "value4");35System.out.println(map);36}37}38{key1=value1, key2=value2, key3=value3, key4=value4}39import org.mockito.internal.util.concurrent.WeakConcurrentSet;40import java.util.Set;41public class 4 {42public static void main(String[] args) {43Set set = new WeakConcurrentSet();44set.add("value1");45set.add("value2");46set.add("value3");47set.add("value4");48System.out.println(set);49}50}51import org.mockito.internal.util.concurrent.Weak

Full Screen

Full Screen

WeakConcurrentMap

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.util.concurrent;2import java.util.concurrent.ConcurrentMap;3import java.util.concurrent.atomic.AtomicInteger;4import java.util.concurrent.atomic.AtomicReference;5import java.util.concurrent.atomic.AtomicReferenceArray;6import java.util.concurrent.locks.Lock;7import java.util.concurrent.locks.ReentrantLock;8import java.util.concurrent.locks.ReentrantReadWriteLock;9import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;10import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;11import java.util.concurrent.locks.StampedLock;12import java.util.concurrent.locks.LockSupport;13import java.util.concurrent.locks.Lock;14import java.util.concurrent.locks.ReentrantLock;15import java.util.concurrent.locks.ReentrantReadWriteLock;16import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;17import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;18import java.util.concurrent.locks.StampedLock;19import java.util.concurrent.locks.LockSupport;20import java.util.concurrent.locks.Lock;21import java.util.concurrent.locks.ReentrantLock;22import java.util.concurrent.locks.ReentrantReadWriteLock;23import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;24import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;25import java.util.concurrent.locks.StampedLock;26import java.util.concurrent.locks.LockSupport;27import java.util.concurrent.locks.Lock;28import java.util.concurrent.locks.ReentrantLock;29import java.util.concurrent.locks.ReentrantReadWriteLock;30import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;31import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;32import java.util.concurrent.locks.StampedLock;33import java.util.concurrent.locks.LockSupport;34import java.util.concurrent.locks.Lock;35import java.util.concurrent.locks.ReentrantLock;36import java.util.concurrent.locks.ReentrantReadWriteLock;37import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;38import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;39import java.util.concurrent.locks.StampedLock;40import java.util.concurrent.locks.LockSupport;41import java.util.concurrent.locks.Lock;42import java.util.concurrent.locks.ReentrantLock;43import java.util.concurrent.locks.ReentrantReadWriteLock;44import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;45import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;46import java.util.concurrent.locks.StampedLock;47import java.util.concurrent.locks.LockSupport;48import java.util.concurrent.locks.Lock;49import java.util.concurrent.locks.Re

Full Screen

Full Screen

WeakConcurrentMap

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.util.concurrent.WeakConcurrentMap;2import java.util.Map;3import java.util.HashMap;4import java.util.WeakHashMap;5import java.util.Map.Entry;6public class WeakConcurrentMapTest {7 public static void main(String[] args) {8 Map<String, String> weakMap = new WeakConcurrentMap<String, String>();9 weakMap.put("key1", "value1");10 weakMap.put("key2", "value2");11 weakMap.put("key3", "value3");12 weakMap.put("key4", "value4");13 weakMap.put("key5", "value5");14 System.out.println("Map size: " + weakMap.size());15 System.out.println("Map keys: " + weakMap.keySet());16 System.out.println("Map values: " + weakMap.values());17 System.out.println("Entries: ");18 for (Entry<String, String> entry : weakMap.entrySet()) {19 System.out.println(entry.getKey() + " : " + entry.getValue());20 }21 System.out.println("Removing key1");22 weakMap.remove("key1");23 System.out.println("Map size: " + weakMap.size());24 System.out.println("Map keys: " + weakMap.keySet());25 System.out.println("Map values: " + weakMap.values());26 System.out.println("Entries: ");27 for (Entry<String, String> entry : weakMap.entrySet()) {28 System.out.println(entry.getKey() + " : " + entry.getValue());29 }30 String key2 = null;31 System.gc();32 System.out.println("After GC");33 System.out.println("Map size: " + weakMap.size());34 System.out.println("Map keys: " + weakMap.keySet());35 System.out.println("Map values: " + weakMap.values());36 System.out.println("Entries: ");37 for (Entry<String, String> entry : weakMap.entrySet()) {38 System.out.println(entry.getKey() + " : " + entry.getValue());39 }40 }41}

Full Screen

Full Screen

WeakConcurrentMap

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.util.concurrent.WeakConcurrentMap;2import java.util.Map;3public class WeakConcurrentMapExample {4 public static void main(String[] args) {5 Map<String, String> map = new WeakConcurrentMap<String, String>();6 map.put("Hello", "World");7 System.out.println(map.get("Hello"));8 }9}

Full Screen

Full Screen

WeakConcurrentMap

Using AI Code Generation

copy

Full Screen

1import java.util.*;2import java.util.concurrent.*;3import java.lang.ref.*;4import org.mockito.internal.util.concurrent.WeakConcurrentMap;5public class TestWeakConcurrentMap {6 public static void main(String[] args) {7 WeakConcurrentMap<String, String> map = new WeakConcurrentMap<String, String>(new ConcurrentHashMap<String, WeakReference<String>>());8 map.put("key", "value");

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful