Skip to content

Commit c2446ec

Browse files
Stypoxlitetex
authored andcommitted
Use Java 8 streams and deduplicate code in MediaFormat class
1 parent d79e203 commit c2446ec

1 file changed

Lines changed: 34 additions & 33 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/MediaFormat.java

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2323
*/
2424

25+
import java.util.Arrays;
26+
import java.util.function.Function;
27+
2528
/**
2629
* Static data about various media formats support by NewPipe, eg mime type, extension
2730
*/
@@ -61,46 +64,47 @@ public enum MediaFormat {
6164
this.mimeType = mimeType;
6265
}
6366

67+
private static <T> T getById(final int id,
68+
final Function<MediaFormat, T> field,
69+
final T orElse) {
70+
return Arrays.stream(MediaFormat.values())
71+
.filter(mediaFormat -> mediaFormat.id == id)
72+
.map(field)
73+
.findFirst()
74+
.orElse(orElse);
75+
}
76+
6477
/**
6578
* Return the friendly name of the media format with the supplied id
6679
*
67-
* @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number.
80+
* @param id the id of the media format. Currently an arbitrary, NewPipe-specific number.
6881
* @return the friendly name of the MediaFormat associated with this ids,
6982
* or an empty String if none match it.
7083
*/
71-
public static String getNameById(int ident) {
72-
for (MediaFormat vf : MediaFormat.values()) {
73-
if (vf.id == ident) return vf.name;
74-
}
75-
return "";
84+
public static String getNameById(final int id) {
85+
return getById(id, MediaFormat::getName, "");
7686
}
7787

7888
/**
7989
* Return the file extension of the media format with the supplied id
8090
*
81-
* @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number.
91+
* @param id the id of the media format. Currently an arbitrary, NewPipe-specific number.
8292
* @return the file extension of the MediaFormat associated with this ids,
8393
* or an empty String if none match it.
8494
*/
85-
public static String getSuffixById(int ident) {
86-
for (MediaFormat vf : MediaFormat.values()) {
87-
if (vf.id == ident) return vf.suffix;
88-
}
89-
return "";
95+
public static String getSuffixById(final int id) {
96+
return getById(id, MediaFormat::getSuffix, "");
9097
}
9198

9299
/**
93100
* Return the MIME type of the media format with the supplied id
94101
*
95-
* @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number.
102+
* @param id the id of the media format. Currently an arbitrary, NewPipe-specific number.
96103
* @return the MIME type of the MediaFormat associated with this ids,
97104
* or an empty String if none match it.
98105
*/
99-
public static String getMimeById(int ident) {
100-
for (MediaFormat vf : MediaFormat.values()) {
101-
if (vf.id == ident) return vf.mimeType;
102-
}
103-
return "";
106+
public static String getMimeById(final int id) {
107+
return getById(id, MediaFormat::getMimeType, null);
104108
}
105109

106110
/**
@@ -109,11 +113,11 @@ public static String getMimeById(int ident) {
109113
* @return MediaFormat associated with this mime type,
110114
* or null if none match it.
111115
*/
112-
public static MediaFormat getFromMimeType(String mimeType) {
113-
for (MediaFormat vf : MediaFormat.values()) {
114-
if (vf.mimeType.equals(mimeType)) return vf;
115-
}
116-
return null;
116+
public static MediaFormat getFromMimeType(final String mimeType) {
117+
return Arrays.stream(MediaFormat.values())
118+
.filter(mediaFormat -> mediaFormat.mimeType.equals(mimeType))
119+
.findFirst()
120+
.orElse(null);
117121
}
118122

119123
/**
@@ -122,18 +126,15 @@ public static MediaFormat getFromMimeType(String mimeType) {
122126
* @param id the id
123127
* @return the id of the media format or null.
124128
*/
125-
public static MediaFormat getFormatById(int id) {
126-
for (MediaFormat vf : values()) {
127-
if (vf.id == id) return vf;
128-
}
129-
return null;
129+
public static MediaFormat getFormatById(final int id) {
130+
return getById(id, mediaFormat -> mediaFormat, null);
130131
}
131132

132-
public static MediaFormat getFromSuffix(String suffix) {
133-
for (MediaFormat vf : values()) {
134-
if (vf.suffix.equals(suffix)) return vf;
135-
}
136-
return null;
133+
public static MediaFormat getFromSuffix(final String suffix) {
134+
return Arrays.stream(MediaFormat.values())
135+
.filter(mediaFormat -> mediaFormat.suffix.equals(suffix))
136+
.findFirst()
137+
.orElse(null);
137138
}
138139

139140
/**

0 commit comments

Comments
 (0)