Add annotations to MediaFormat

This commit is contained in:
TobiGr 2023-06-17 18:01:54 +02:00
parent 53dfd871e2
commit 5809904cf7
1 changed files with 22 additions and 2 deletions

View File

@ -23,6 +23,7 @@ package org.schabi.newpipe.extractor;
*/ */
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
@ -66,11 +67,15 @@ public enum MediaFormat {
// @formatter:on // @formatter:on
public final int id; public final int id;
@Nonnull
public final String name; public final String name;
@Nonnull
public final String suffix; public final String suffix;
@Nonnull
public final String mimeType; public final String mimeType;
MediaFormat(final int id, final String name, final String suffix, final String mimeType) { MediaFormat(final int id, @Nonnull final String name,
@Nonnull final String suffix, @Nonnull final String mimeType) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.suffix = suffix; this.suffix = suffix;
@ -94,6 +99,7 @@ public enum MediaFormat {
* @return the friendly name of the MediaFormat associated with this ids, * @return the friendly name of the MediaFormat associated with this ids,
* or an empty String if none match it. * or an empty String if none match it.
*/ */
@Nonnull
public static String getNameById(final int id) { public static String getNameById(final int id) {
return getById(id, MediaFormat::getName, ""); return getById(id, MediaFormat::getName, "");
} }
@ -105,6 +111,7 @@ public enum MediaFormat {
* @return the file extension of the MediaFormat associated with this ids, * @return the file extension of the MediaFormat associated with this ids,
* or an empty String if none match it. * or an empty String if none match it.
*/ */
@Nonnull
public static String getSuffixById(final int id) { public static String getSuffixById(final int id) {
return getById(id, MediaFormat::getSuffix, ""); return getById(id, MediaFormat::getSuffix, "");
} }
@ -116,16 +123,20 @@ public enum MediaFormat {
* @return the MIME type of the MediaFormat associated with this ids, * @return the MIME type of the MediaFormat associated with this ids,
* or an empty String if none match it. * or an empty String if none match it.
*/ */
@Nullable
public static String getMimeById(final int id) { public static String getMimeById(final int id) {
return getById(id, MediaFormat::getMimeType, null); return getById(id, MediaFormat::getMimeType, null);
} }
/** /**
* Return the MediaFormat with the supplied mime type * Return the first {@link MediaFormat} with the supplied mime type.
* There might be more formats which have the same mime type.
* To retrieve those, use {@link #getAllFromMimeType(String)}.
* *
* @return MediaFormat associated with this mime type, * @return MediaFormat associated with this mime type,
* or null if none match it. * or null if none match it.
*/ */
@Nullable
public static MediaFormat getFromMimeType(final String mimeType) { public static MediaFormat getFromMimeType(final String mimeType) {
return Arrays.stream(MediaFormat.values()) return Arrays.stream(MediaFormat.values())
.filter(mediaFormat -> mediaFormat.mimeType.equals(mimeType)) .filter(mediaFormat -> mediaFormat.mimeType.equals(mimeType))
@ -152,10 +163,16 @@ public enum MediaFormat {
* @param id the id * @param id the id
* @return the id of the media format or null. * @return the id of the media format or null.
*/ */
@Nullable
public static MediaFormat getFormatById(final int id) { public static MediaFormat getFormatById(final int id) {
return getById(id, mediaFormat -> mediaFormat, null); return getById(id, mediaFormat -> mediaFormat, null);
} }
/**
* Get the first media format that has the given suffix/file extension.
* @return the matching {@link MediaFormat} or {@code null} if no associated format is found
*/
@Nullable
public static MediaFormat getFromSuffix(final String suffix) { public static MediaFormat getFromSuffix(final String suffix) {
return Arrays.stream(MediaFormat.values()) return Arrays.stream(MediaFormat.values())
.filter(mediaFormat -> mediaFormat.suffix.equals(suffix)) .filter(mediaFormat -> mediaFormat.suffix.equals(suffix))
@ -168,6 +185,7 @@ public enum MediaFormat {
* *
* @return the name of the format * @return the name of the format
*/ */
@Nonnull
public String getName() { public String getName() {
return name; return name;
} }
@ -177,6 +195,7 @@ public enum MediaFormat {
* *
* @return the filename extension * @return the filename extension
*/ */
@Nonnull
public String getSuffix() { public String getSuffix() {
return suffix; return suffix;
} }
@ -186,6 +205,7 @@ public enum MediaFormat {
* *
* @return the mime type * @return the mime type
*/ */
@Nonnull
public String getMimeType() { public String getMimeType() {
return mimeType; return mimeType;
} }