Use Java 8 streams and deduplicate code in MediaFormat class

This commit is contained in:
Stypox 2022-03-17 16:05:43 +01:00 committed by litetex
parent d79e20340c
commit c2446ecff0
1 changed files with 34 additions and 33 deletions

View File

@ -22,6 +22,9 @@ package org.schabi.newpipe.extractor;
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>. * along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/ */
import java.util.Arrays;
import java.util.function.Function;
/** /**
* Static data about various media formats support by NewPipe, eg mime type, extension * Static data about various media formats support by NewPipe, eg mime type, extension
*/ */
@ -61,46 +64,47 @@ public enum MediaFormat {
this.mimeType = mimeType; this.mimeType = mimeType;
} }
private static <T> T getById(final int id,
final Function<MediaFormat, T> field,
final T orElse) {
return Arrays.stream(MediaFormat.values())
.filter(mediaFormat -> mediaFormat.id == id)
.map(field)
.findFirst()
.orElse(orElse);
}
/** /**
* Return the friendly name of the media format with the supplied id * Return the friendly name of the media format with the supplied id
* *
* @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number. * @param id the id of the media format. Currently an arbitrary, NewPipe-specific number.
* @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.
*/ */
public static String getNameById(int ident) { public static String getNameById(final int id) {
for (MediaFormat vf : MediaFormat.values()) { return getById(id, MediaFormat::getName, "");
if (vf.id == ident) return vf.name;
}
return "";
} }
/** /**
* Return the file extension of the media format with the supplied id * Return the file extension of the media format with the supplied id
* *
* @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number. * @param id the id of the media format. Currently an arbitrary, NewPipe-specific number.
* @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.
*/ */
public static String getSuffixById(int ident) { public static String getSuffixById(final int id) {
for (MediaFormat vf : MediaFormat.values()) { return getById(id, MediaFormat::getSuffix, "");
if (vf.id == ident) return vf.suffix;
}
return "";
} }
/** /**
* Return the MIME type of the media format with the supplied id * Return the MIME type of the media format with the supplied id
* *
* @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number. * @param id the id of the media format. Currently an arbitrary, NewPipe-specific number.
* @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.
*/ */
public static String getMimeById(int ident) { public static String getMimeById(final int id) {
for (MediaFormat vf : MediaFormat.values()) { return getById(id, MediaFormat::getMimeType, null);
if (vf.id == ident) return vf.mimeType;
}
return "";
} }
/** /**
@ -109,11 +113,11 @@ public enum MediaFormat {
* @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.
*/ */
public static MediaFormat getFromMimeType(String mimeType) { public static MediaFormat getFromMimeType(final String mimeType) {
for (MediaFormat vf : MediaFormat.values()) { return Arrays.stream(MediaFormat.values())
if (vf.mimeType.equals(mimeType)) return vf; .filter(mediaFormat -> mediaFormat.mimeType.equals(mimeType))
} .findFirst()
return null; .orElse(null);
} }
/** /**
@ -122,18 +126,15 @@ 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.
*/ */
public static MediaFormat getFormatById(int id) { public static MediaFormat getFormatById(final int id) {
for (MediaFormat vf : values()) { return getById(id, mediaFormat -> mediaFormat, null);
if (vf.id == id) return vf;
}
return null;
} }
public static MediaFormat getFromSuffix(String suffix) { public static MediaFormat getFromSuffix(final String suffix) {
for (MediaFormat vf : values()) { return Arrays.stream(MediaFormat.values())
if (vf.suffix.equals(suffix)) return vf; .filter(mediaFormat -> mediaFormat.suffix.equals(suffix))
} .findFirst()
return null; .orElse(null);
} }
/** /**