My file Storage.java represents an abstract superclass for an Acamedia user's cabinet, or the cabinet's folders. Both the cabinet and the folders must be able to take a new file and create an "Item" out of it. But how does one select from the many supported filetypes, and their corresponding Item subclasses?
In this post I walk you through the methods used for solving this problem: MIME content type probing, and a Java-based enumeration.
Check this out!
Java Enums
Enumerations in Java are different from most other languages, even other curly-bracket languages like C++ or C#. That's because in Java, an enum is actually an outlining of classes. That is, every item in an enumeration is, itself, an instance of a class.That means everything that goes with it is valid as well. Every item in an enum can have a constructor, its own defined methods, and you can even put static methods in your enumeration, or abstract methods to be implemented by each item.
In the file ItemType.java you will find an example of such an advanced enumeration. The capability found here is something relatively unique to Java, and one of the language's strengths. You'll find an embedded version at the end of this post.
MIME Content Types
To change gears, a Multipurpose Internet Mail Extension (MIME) content type is something defined long ago—in this document, in fact. In a nutshell, it's (hopefully) universally-unique identifier for the type of content and format of a particular file. It was, as the name implies, originally used for email attachments. Now, it's used for a whole slew of things...Like identifying the type of file you're dealing with. Sure, we could do something barbaric like look at the file extension, but that might be a misnomer—or worse, your operating system might not even use them (I'm looking at YOU *nix... get it?)
ItemType.java Contents
As you can see below, the file is pretty well-commented. But what you're seeing is this: each item in the enum ItemType must have implemented amakeInstance method, which takes a given file and returns an instance of the relevant class. But how do you use the enum to decide which one to get?That's the beauty of the static method, on line 44. Given some string, which is supposed to be a supported MIME content type, the static method
matchType will loop through every MIME type for every item in the enum (each item, such as "PDF" or "DOCX," passes into its constructor every associated MIME content type. Most have one, but ote that PDF, for example, has two). Once it finds a match, the static method returns the correct item in the enum. More specifically, it returns the instance of the class ItemType which is associated with that MIME content type.A given method can use this return to say
ItemType foo = ItemType.matchType(mime); and then, elegantly: foo.makeInstance(fileInQuestion); This of course assumes that mime was derived from fileInQuestion, probably using Files.probeContentType(fileInQuestion.toPath()); (though you have to catch an IOException and SecurityException from probeContentType, and a couple more from toPath()).See below for the actual code!
No comments:
Post a Comment