2

I have never used an enum before. So please be patient with me a little bit.

App background:

I created an application in which the user can add three type of sources, *music* *video* and *pictures* from the external storage USB device.
The user selects a folder onto the usd drive and adds it to the source and then adds a category for it.

THIS HAS BEEN DONE

The Issue is

Now I am trying to read all the files from the folder which has a specific extension, as defined by category.

Following are the extensions for three categories that will be stored in an xml file

<?xml version="1.0" encoding="UTF-8"?>

<Picture>
    <ext>jpg</ext>
    <ext>png</ext>
    <ext>gif</ext>
    <ext>jpeg</ext>
    <ext>pdf</ext>
    <ext>pcd</ext>
    <ext>jpx</ext>
    <ext>tif</ext>
</Picture>

<Videos>
    <ext>mpeg</ext>
    <ext>mpg</ext>
    <ext>mpe</ext>
    <ext>mpeg-1</ext>
    <ext>mpeg-2</ext>
    <ext>mp4</ext>
    <ext>mpa</ext>
    <ext>wmv</ext>
    <ext>wma</ext>
    <ext>wmx</ext>
    <ext>rm</ext>
    <ext>ra</ext>
    <ext>3gp</ext>
</Videos>

<Music>
    <ext>mp3</ext>
    <ext>wav</ext>
    <ext>dct</ext>
    <ext>gsm</ext>
    <ext>flac</ext>
    <ext>au</ext>
    <ext>ogg</ext>
    <ext>raw</ext>
    <ext>wma</ext>
    <ext>msv</ext>
</Music>

So, for instance, if a user has selected the video category I will read all files which have the extensions similar to video in my xml

My Qustion Is:

I am trying to do this thing by using an ENUM, I know I can do it with simple if/else statements as well, but I need to know how the ENUM will work.

I have created a Media Source Enum that has the categories defined.
Here is the code:

public enum Media_source {

    Pitures("P"), Videos("V"), Music("M");

    private String statusCode;

    private Media_source(String s) {
        statusCode = s;
    }

    public String get_media_source() {
        return statusCode;
    }

}

Then I have a Source class that checks which category is selected.

public class Source {

        public static String source_path;
        public static String source_name;
        Media_source mSource;

        public static void main(String[] args) {

            System.out.println(Media_source.Pitures.get_media_source());

        }


        public Source(String name, String path) {
            Source.source_name = name;
            Source.source_path = path;
        }
        public static String getSource_path() {
            return source_path;
        }
        public static void setSource_path(String source_path) {
            Source.source_path = source_path;
        }
        public static String getSource_name() {
            return source_name;
        }
        public static void setSource_name(String source_name) {
            Source.source_name = source_name;
        }

}

Now how can I actually select all files with the extensions for the selected category?
How should I use the enum?

I am not asking for code, though any helping material would be appreciated - I just need to get the concept clear.

Thank you

3 Answers 3

2

This is a great lecture: Google I/O 2008 lecture (Effective Java Reloaded)
The Enum part is around here.
Since the video’s quality very poor, here are the slides. https://sites.google.com/site/io/effective-java-reloaded

Sign up to request clarification or add additional context in comments.

Comments

1

"Then I have a Source class that checks which category is selected." - I don't see any code checking the type of the Enum.

This is how I use my Enums, you can do something similar.

Declare your enum:

public enum Media {
    IMAGE, VIDEO, AUDIO;
}

Create a the method which takes the enum as the parameter:

public selectStuff(Media m) {

    switch(m) {

        case IMAGE:
            //Do image stuff
            break;

        case VIDEO:
            //Do video stuff
            break;

        case AUDIO:
            //Do audio stuff
            break;

        default:
            break;

    }
}

And this is how you would use it:

selectMedia(Media.IMAGE);

There is a similar example in the Oracle Docs.

1 Comment

where do i put this function? in the enum?
1

You might want to create an EnumMap somewhere in your code, with Media_source as a key and a Set<String> of extensions as a value. You then use that map to retrieve the appropriate extensions for the media type

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.