Merge "Simplify ResultCallbacks by not using WeakReference"
This commit is contained in:
committed by
Android (Google) Code Review
commit
5a7c74d3f4
@@ -21,7 +21,6 @@ import android.annotation.BinderThread;
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
@@ -38,15 +37,9 @@ public final class ResultCallbacks {
|
||||
|
||||
@AnyThread
|
||||
@Nullable
|
||||
private static <T> T unwrap(@NonNull AtomicReference<WeakReference<T>> atomicRef) {
|
||||
final WeakReference<T> ref = atomicRef.getAndSet(null);
|
||||
if (ref == null) {
|
||||
// Double-call is guaranteed to be ignored here.
|
||||
return null;
|
||||
}
|
||||
final T value = ref.get();
|
||||
ref.clear();
|
||||
return value;
|
||||
private static <T> T unwrap(@NonNull AtomicReference<T> atomicRef) {
|
||||
// Only the first caller will receive the non-null original object.
|
||||
return atomicRef.getAndSet(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,8 +51,7 @@ public final class ResultCallbacks {
|
||||
*/
|
||||
@AnyThread
|
||||
public static IIntResultCallback.Stub of(@NonNull Completable.Int value) {
|
||||
final AtomicReference<WeakReference<Completable.Int>>
|
||||
atomicRef = new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.Int> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new IIntResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -95,8 +87,7 @@ public final class ResultCallbacks {
|
||||
@AnyThread
|
||||
public static ICharSequenceResultCallback.Stub of(
|
||||
@NonNull Completable.CharSequence value) {
|
||||
final AtomicReference<WeakReference<Completable.CharSequence>> atomicRef =
|
||||
new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.CharSequence> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new ICharSequenceResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -122,8 +113,7 @@ public final class ResultCallbacks {
|
||||
@AnyThread
|
||||
public static IExtractedTextResultCallback.Stub of(
|
||||
@NonNull Completable.ExtractedText value) {
|
||||
final AtomicReference<WeakReference<Completable.ExtractedText>>
|
||||
atomicRef = new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.ExtractedText> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new IExtractedTextResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -149,8 +139,7 @@ public final class ResultCallbacks {
|
||||
@AnyThread
|
||||
public static ISurroundingTextResultCallback.Stub of(
|
||||
@NonNull Completable.SurroundingText value) {
|
||||
final AtomicReference<WeakReference<Completable.SurroundingText>>
|
||||
atomicRef = new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.SurroundingText> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new ISurroundingTextResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -174,8 +163,7 @@ public final class ResultCallbacks {
|
||||
*/
|
||||
@AnyThread
|
||||
public static IBooleanResultCallback.Stub of(@NonNull Completable.Boolean value) {
|
||||
final AtomicReference<WeakReference<Completable.Boolean>>
|
||||
atomicRef = new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.Boolean> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new IBooleanResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -209,8 +197,7 @@ public final class ResultCallbacks {
|
||||
*/
|
||||
@AnyThread
|
||||
public static IVoidResultCallback.Stub of(@NonNull Completable.Void value) {
|
||||
final AtomicReference<WeakReference<Completable.Void>> atomicRef =
|
||||
new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.Void> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new IVoidResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -246,8 +233,8 @@ public final class ResultCallbacks {
|
||||
@AnyThread
|
||||
public static IIInputContentUriTokenResultCallback.Stub of(
|
||||
@NonNull Completable.IInputContentUriToken value) {
|
||||
final AtomicReference<WeakReference<Completable.IInputContentUriToken>>
|
||||
atomicRef = new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.IInputContentUriToken>
|
||||
atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new IIInputContentUriTokenResultCallback.Stub() {
|
||||
@BinderThread
|
||||
|
||||
Reference in New Issue
Block a user