From 5b3ed8286cc0ffa8be3793229bf25b671fec5c47 Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 17 Feb 2020 15:15:55 +0000 Subject: [PATCH] Add non-null by default to source package (last one!) Note on UnknownNull: Where there exists a generically typed base class and both null and non-null types are permitted, we need to clear the default non-null that's otherwise propagated everywhere. This then lets the nullness of the type work properly. PiperOrigin-RevId: 295582444 --- .../android/exoplayer2/util/UnknownNull.java | 32 +++++++++++++++++++ .../source/CompositeMediaSource.java | 24 +++++++------- .../exoplayer2/source/package-info.java | 19 +++++++++++ 3 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 library/common/src/main/java/com/google/android/exoplayer2/util/UnknownNull.java create mode 100644 library/core/src/main/java/com/google/android/exoplayer2/source/package-info.java diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/UnknownNull.java b/library/common/src/main/java/com/google/android/exoplayer2/util/UnknownNull.java new file mode 100644 index 0000000000..8ca2e8f345 --- /dev/null +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/UnknownNull.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.util; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import javax.annotation.Nonnull; +import javax.annotation.meta.TypeQualifierDefault; +import javax.annotation.meta.When; + +/** + * Annotation for specifiying unknown nullness. Useful for clearing the effects of an automatically + * propagated {@link Nonnull} annotation. + */ +@Nonnull(when = When.UNKNOWN) +@TypeQualifierDefault(ElementType.TYPE_USE) +@Retention(RetentionPolicy.CLASS) +public @interface UnknownNull {} diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/CompositeMediaSource.java b/library/core/src/main/java/com/google/android/exoplayer2/source/CompositeMediaSource.java index 356fdb98ec..5876c5157d 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/CompositeMediaSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/CompositeMediaSource.java @@ -21,6 +21,7 @@ import androidx.annotation.Nullable; import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.upstream.TransferListener; import com.google.android.exoplayer2.util.Assertions; +import com.google.android.exoplayer2.util.UnknownNull; import com.google.android.exoplayer2.util.Util; import java.io.IOException; import java.util.HashMap; @@ -91,7 +92,7 @@ public abstract class CompositeMediaSource extends BaseMediaSource { * @param timeline The timeline of the child source. */ protected abstract void onChildSourceInfoRefreshed( - T id, MediaSource mediaSource, Timeline timeline); + @UnknownNull T id, MediaSource mediaSource, Timeline timeline); /** * Prepares a child source. @@ -105,7 +106,7 @@ public abstract class CompositeMediaSource extends BaseMediaSource { * @param id A unique id to identify the child source preparation. Null is allowed as an id. * @param mediaSource The child {@link MediaSource}. */ - protected final void prepareChildSource(final T id, MediaSource mediaSource) { + protected final void prepareChildSource(@UnknownNull T id, MediaSource mediaSource) { Assertions.checkArgument(!childSources.containsKey(id)); MediaSourceCaller caller = (source, timeline) -> onChildSourceInfoRefreshed(id, source, timeline); @@ -123,7 +124,7 @@ public abstract class CompositeMediaSource extends BaseMediaSource { * * @param id The unique id used to prepare the child source. */ - protected final void enableChildSource(final T id) { + protected final void enableChildSource(@UnknownNull T id) { MediaSourceAndListener enabledChild = Assertions.checkNotNull(childSources.get(id)); enabledChild.mediaSource.enable(enabledChild.caller); } @@ -133,7 +134,7 @@ public abstract class CompositeMediaSource extends BaseMediaSource { * * @param id The unique id used to prepare the child source. */ - protected final void disableChildSource(final T id) { + protected final void disableChildSource(@UnknownNull T id) { MediaSourceAndListener disabledChild = Assertions.checkNotNull(childSources.get(id)); disabledChild.mediaSource.disable(disabledChild.caller); } @@ -143,7 +144,7 @@ public abstract class CompositeMediaSource extends BaseMediaSource { * * @param id The unique id used to prepare the child source. */ - protected final void releaseChildSource(T id) { + protected final void releaseChildSource(@UnknownNull T id) { MediaSourceAndListener removedChild = Assertions.checkNotNull(childSources.remove(id)); removedChild.mediaSource.releaseSource(removedChild.caller); removedChild.mediaSource.removeEventListener(removedChild.eventListener); @@ -157,7 +158,7 @@ public abstract class CompositeMediaSource extends BaseMediaSource { * @param windowIndex A window index of the child source. * @return The corresponding window index in the composite source. */ - protected int getWindowIndexForChildWindowIndex(T id, int windowIndex) { + protected int getWindowIndexForChildWindowIndex(@UnknownNull T id, int windowIndex) { return windowIndex; } @@ -171,8 +172,9 @@ public abstract class CompositeMediaSource extends BaseMediaSource { * @return The corresponding {@link MediaPeriodId} in the composite source. Null if no * corresponding media period id can be determined. */ - protected @Nullable MediaPeriodId getMediaPeriodIdForChildMediaPeriodId( - T id, MediaPeriodId mediaPeriodId) { + @Nullable + protected MediaPeriodId getMediaPeriodIdForChildMediaPeriodId( + @UnknownNull T id, MediaPeriodId mediaPeriodId) { return mediaPeriodId; } @@ -184,7 +186,7 @@ public abstract class CompositeMediaSource extends BaseMediaSource { * @param mediaTimeMs A media time of the child source, in milliseconds. * @return The corresponding media time in the composite source, in milliseconds. */ - protected long getMediaTimeForChildMediaTime(@Nullable T id, long mediaTimeMs) { + protected long getMediaTimeForChildMediaTime(@UnknownNull T id, long mediaTimeMs) { return mediaTimeMs; } @@ -216,10 +218,10 @@ public abstract class CompositeMediaSource extends BaseMediaSource { private final class ForwardingEventListener implements MediaSourceEventListener { - private final T id; + @UnknownNull private final T id; private EventDispatcher eventDispatcher; - public ForwardingEventListener(T id) { + public ForwardingEventListener(@UnknownNull T id) { this.eventDispatcher = createEventDispatcher(/* mediaPeriodId= */ null); this.id = id; } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/package-info.java b/library/core/src/main/java/com/google/android/exoplayer2/source/package-info.java new file mode 100644 index 0000000000..adb05a46f2 --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/package-info.java @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +@NonNullApi +package com.google.android.exoplayer2.source; + +import com.google.android.exoplayer2.util.NonNullApi;