Skip to content

Commit

Permalink
Make RequestBuilder consistently support lock and autoClone
Browse files Browse the repository at this point in the history
Previously these methods worked, but only for the methods in RequestBuilder that came from BaseRequestOptions. This changes makes sure the remaining methods in RequestBuilder (and the generated equivalent) obey those two methods in a consistent manner.

PiperOrigin-RevId: 347879132
  • Loading branch information
sjudd authored and glide-copybara-robot committed Dec 16, 2020
1 parent 332807a commit 11530fd
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ final class RequestBuilderGenerator {
private static final String TRANSCODE_TYPE_NAME = "TranscodeType";
/** A set of method names to avoid overriding from RequestOptions. */
private static final ImmutableSet<String> EXCLUDED_METHODS_FROM_BASE_REQUEST_OPTIONS =
ImmutableSet.of("clone", "apply", "autoLock", "lock", "autoClone");
ImmutableSet.of("clone", "apply");

private final ProcessingEnvironment processingEnv;
private final ProcessorUtil processorUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,22 @@ public GlideRequest<TranscodeType> dontAnimate() {
return (GlideRequest<TranscodeType>) super.dontAnimate();
}

/**
* @see GlideOptions#lock()
*/
@NonNull
public GlideRequest<TranscodeType> lock() {
return (GlideRequest<TranscodeType>) super.lock();
}

/**
* @see GlideOptions#autoClone()
*/
@NonNull
public GlideRequest<TranscodeType> autoClone() {
return (GlideRequest<TranscodeType>) super.autoClone();
}

@Override
@NonNull
@CheckResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,22 @@ public GlideRequest<TranscodeType> dontAnimate() {
return (GlideRequest<TranscodeType>) super.dontAnimate();
}

/**
* @see GlideOptions#lock()
*/
@NonNull
public GlideRequest<TranscodeType> lock() {
return (GlideRequest<TranscodeType>) super.lock();
}

/**
* @see GlideOptions#autoClone()
*/
@NonNull
public GlideRequest<TranscodeType> autoClone() {
return (GlideRequest<TranscodeType>) super.autoClone();
}

@Override
@NonNull
@CheckResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,22 @@ public GlideRequest<TranscodeType> dontAnimate() {
return (GlideRequest<TranscodeType>) super.dontAnimate();
}

/**
* @see GlideOptions#lock()
*/
@NonNull
public GlideRequest<TranscodeType> lock() {
return (GlideRequest<TranscodeType>) super.lock();
}

/**
* @see GlideOptions#autoClone()
*/
@NonNull
public GlideRequest<TranscodeType> autoClone() {
return (GlideRequest<TranscodeType>) super.autoClone();
}

@Override
@NonNull
@CheckResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,22 @@ public GlideRequest<TranscodeType> dontAnimate() {
return (GlideRequest<TranscodeType>) super.dontAnimate();
}

/**
* @see GlideOptions#lock()
*/
@NonNull
public GlideRequest<TranscodeType> lock() {
return (GlideRequest<TranscodeType>) super.lock();
}

/**
* @see GlideOptions#autoClone()
*/
@NonNull
public GlideRequest<TranscodeType> autoClone() {
return (GlideRequest<TranscodeType>) super.autoClone();
}

@Override
@NonNull
@CheckResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,22 @@ public GlideRequest<TranscodeType> dontAnimate() {
return (GlideRequest<TranscodeType>) super.dontAnimate();
}

/**
* @see GlideOptions#lock()
*/
@NonNull
public GlideRequest<TranscodeType> lock() {
return (GlideRequest<TranscodeType>) super.lock();
}

/**
* @see GlideOptions#autoClone()
*/
@NonNull
public GlideRequest<TranscodeType> autoClone() {
return (GlideRequest<TranscodeType>) super.autoClone();
}

@Override
@NonNull
@CheckResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,22 @@ public GlideRequest<TranscodeType> dontAnimate() {
return (GlideRequest<TranscodeType>) super.dontAnimate();
}

/**
* @see GlideOptions#lock()
*/
@NonNull
public GlideRequest<TranscodeType> lock() {
return (GlideRequest<TranscodeType>) super.lock();
}

/**
* @see GlideOptions#autoClone()
*/
@NonNull
public GlideRequest<TranscodeType> autoClone() {
return (GlideRequest<TranscodeType>) super.autoClone();
}

@Override
@NonNull
@CheckResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,22 @@ public GlideRequest<TranscodeType> dontAnimate() {
return (GlideRequest<TranscodeType>) super.dontAnimate();
}

/**
* @see GlideOptions#lock()
*/
@NonNull
public GlideRequest<TranscodeType> lock() {
return (GlideRequest<TranscodeType>) super.lock();
}

/**
* @see GlideOptions#autoClone()
*/
@NonNull
public GlideRequest<TranscodeType> autoClone() {
return (GlideRequest<TranscodeType>) super.autoClone();
}

@Override
@NonNull
@CheckResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,22 @@ public GlideRequest<TranscodeType> dontAnimate() {
return (GlideRequest<TranscodeType>) super.dontAnimate();
}

/**
* @see GlideOptions#lock()
*/
@NonNull
public GlideRequest<TranscodeType> lock() {
return (GlideRequest<TranscodeType>) super.lock();
}

/**
* @see GlideOptions#autoClone()
*/
@NonNull
public GlideRequest<TranscodeType> autoClone() {
return (GlideRequest<TranscodeType>) super.autoClone();
}

@Override
@NonNull
@CheckResult
Expand Down
33 changes: 27 additions & 6 deletions library/src/main/java/com/bumptech/glide/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,12 @@ public RequestBuilder<TranscodeType> apply(@NonNull BaseRequestOptions<?> reques
@CheckResult
public RequestBuilder<TranscodeType> transition(
@NonNull TransitionOptions<?, ? super TranscodeType> transitionOptions) {
if (isAutoCloneEnabled()) {
return clone().transition(transitionOptions);
}
this.transitionOptions = Preconditions.checkNotNull(transitionOptions);
isDefaultTransitionOptionsSet = false;
return this;
return selfOrThrowIfLocked();
}

/**
Expand All @@ -173,6 +176,9 @@ public RequestBuilder<TranscodeType> transition(
@SuppressWarnings("unchecked")
public RequestBuilder<TranscodeType> listener(
@Nullable RequestListener<TranscodeType> requestListener) {
if (isAutoCloneEnabled()) {
return clone().listener(requestListener);
}
this.requestListeners = null;
return addListener(requestListener);
}
Expand All @@ -188,13 +194,16 @@ public RequestBuilder<TranscodeType> listener(
@CheckResult
public RequestBuilder<TranscodeType> addListener(
@Nullable RequestListener<TranscodeType> requestListener) {
if (isAutoCloneEnabled()) {
return clone().addListener(requestListener);
}
if (requestListener != null) {
if (this.requestListeners == null) {
this.requestListeners = new ArrayList<>();
}
this.requestListeners.add(requestListener);
}
return this;
return selfOrThrowIfLocked();
}

/**
Expand All @@ -221,8 +230,11 @@ public RequestBuilder<TranscodeType> addListener(
*/
@NonNull
public RequestBuilder<TranscodeType> error(@Nullable RequestBuilder<TranscodeType> errorBuilder) {
if (isAutoCloneEnabled()) {
return clone().error(errorBuilder);
}
this.errorBuilder = errorBuilder;
return this;
return selfOrThrowIfLocked();
}

/**
Expand Down Expand Up @@ -279,9 +291,12 @@ private RequestBuilder<TranscodeType> cloneWithNullErrorAndThumbnail() {
@SuppressWarnings("unchecked")
public RequestBuilder<TranscodeType> thumbnail(
@Nullable RequestBuilder<TranscodeType> thumbnailRequest) {
if (isAutoCloneEnabled()) {
return clone().thumbnail(thumbnailRequest);
}
this.thumbnailBuilder = thumbnailRequest;

return this;
return selfOrThrowIfLocked();
}

/**
Expand Down Expand Up @@ -413,12 +428,15 @@ public RequestBuilder<TranscodeType> thumbnail(
@CheckResult
@SuppressWarnings("unchecked")
public RequestBuilder<TranscodeType> thumbnail(float sizeMultiplier) {
if (isAutoCloneEnabled()) {
return clone().thumbnail(sizeMultiplier);
}
if (sizeMultiplier < 0f || sizeMultiplier > 1f) {
throw new IllegalArgumentException("sizeMultiplier must be between 0 and 1");
}
this.thumbSizeMultiplier = sizeMultiplier;

return this;
return selfOrThrowIfLocked();
}

/**
Expand All @@ -437,9 +455,12 @@ public RequestBuilder<TranscodeType> load(@Nullable Object model) {

@NonNull
private RequestBuilder<TranscodeType> loadGeneric(@Nullable Object model) {
if (isAutoCloneEnabled()) {
return clone().loadGeneric(model);
}
this.model = model;
isModelSet = true;
return this;
return selfOrThrowIfLocked();
}
/**
* Returns an object to load the given {@link Bitmap}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,14 +1283,14 @@ public T autoClone() {

@NonNull
@SuppressWarnings("unchecked")
private T selfOrThrowIfLocked() {
protected final T selfOrThrowIfLocked() {
if (isLocked) {
throw new IllegalStateException("You cannot modify locked T, consider clone()");
}
return self();
}

protected boolean isAutoCloneEnabled() {
protected final boolean isAutoCloneEnabled() {
return isAutoCloneEnabled;
}

Expand Down

0 comments on commit 11530fd

Please sign in to comment.