Skip to content

Commit

Permalink
fix: not using HasSet.
Browse files Browse the repository at this point in the history
Using HashSet here is a waste of memory. HashSet stands on HashMap, which will alloc amount of memory. The only usage is to travel the contents of the Set, ArraySet matches the scene.
Besides, it's a really bad practice to use inner class to "initialize" a set. It will produce more classes.
  • Loading branch information
周骞 authored and gejiaheng committed Sep 21, 2017
1 parent f911be8 commit 29ebd62
Showing 1 changed file with 54 additions and 76 deletions.
130 changes: 54 additions & 76 deletions matisse/src/main/java/com/zhihu/matisse/MimeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import android.content.ContentResolver;
import android.net.Uri;
import android.support.v4.util.ArraySet;
import android.webkit.MimeTypeMap;

import com.zhihu.matisse.internal.utils.PhotoMetadataUtils;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

Expand All @@ -36,84 +37,57 @@
*/
@SuppressWarnings("unused")
public enum MimeType {

// ============== images ==============
JPEG("image/jpeg", new HashSet<String>() {
{
add("jpg");
add("jpeg");
}
}),
PNG("image/png", new HashSet<String>() {
{
add("png");
}
}),
GIF("image/gif", new HashSet<String>() {
{
add("gif");
}
}),
BMP("image/x-ms-bmp", new HashSet<String>() {
{
add("bmp");
}
}),
WEBP("image/webp", new HashSet<String>() {
{
add("webp");
}
}),
JPEG("image/jpeg", arraySetOf(
"jpg",
"jpeg"
)),
PNG("image/png", arraySetOf(
"png"
)),
GIF("image/gif", arraySetOf(
"gif"
)),
BMP("image/x-ms-bmp", arraySetOf(
"bmp"
)),
WEBP("image/webp", arraySetOf(
"webp"
)),

// ============== videos ==============
MPEG("video/mpeg", new HashSet<String>() {
{
add("mpeg");
add("mpg");
}
}),
MP4("video/mp4", new HashSet<String>() {
{
add("mp4");
add("m4v");
}
}),
QUICKTIME("video/quicktime", new HashSet<String>() {
{
add("mov");
}
}),
THREEGPP("video/3gpp", new HashSet<String>() {
{
add("3gp");
add("3gpp");
}
}),
THREEGPP2("video/3gpp2", new HashSet<String>() {
{
add("3g2");
add("3gpp2");
}
}),
MKV("video/x-matroska", new HashSet<String>() {
{
add("mkv");
}
}),
WEBM("video/webm", new HashSet<String>() {
{
add("webm");
}
}),
TS("video/mp2ts", new HashSet<String>() {
{
add("ts");
}
}),
AVI("video/avi", new HashSet<String>() {
{
add("avi");
}
});
MPEG("video/mpeg", arraySetOf(
"mpeg",
"mpg"
)),
MP4("video/mp4", arraySetOf(
"mp4",
"m4v"
)),
QUICKTIME("video/quicktime", arraySetOf(
"mov"
)),
THREEGPP("video/3gpp", arraySetOf(
"3gp",
"3gpp"
)),
THREEGPP2("video/3gpp2", arraySetOf(
"3g2",
"3gpp2"
)),
MKV("video/x-matroska", arraySetOf(
"mkv"
)),
WEBM("video/webm", arraySetOf(
"webm"
)),
TS("video/mp2ts", arraySetOf(
"ts"
)),
AVI("video/avi", arraySetOf(
"avi"
));

private final String mMimeTypeName;
private final Set<String> mExtensions;
Expand All @@ -139,6 +113,10 @@ public static Set<MimeType> ofVideo() {
return EnumSet.of(MPEG, MP4, QUICKTIME, THREEGPP, THREEGPP2, MKV, WEBM, TS, AVI);
}

private static Set<String> arraySetOf(String... suffixes) {
return new ArraySet<>(Arrays.asList(suffixes));
}

@Override
public String toString() {
return mMimeTypeName;
Expand Down

0 comments on commit 29ebd62

Please sign in to comment.