简体   繁体   中英

Huge Map of <String,“Some Class”> in Java

In my application I need to show information about files as a friendly readable string, based on the file's MIME String. I have the information I need (FYI taken from /usr/share/mime/package/freedesktop.org.xml on Ubuntu 14.04), and I'd like to put everything as a static map<"String","MIMEInfoObject">.

protected class MIMEInfoObject {
    //Getters ...
    //Setters ...
    ...
    private String mimeType; //Example: "application/pdf
    private String defaultDescription; // "PDF document"
    private String localDescription; // "Documento PDF"
    private String acronym; // "PDF"
    private String extendedAcronym; // "Portable Document Format"
    private List<String> extensionList; // "pdf .."
    private List<String> aliasList; // "application/x-pdf, application/acrobat, ... "
}

I made a shell script that outputs the linux file like this:

<mime-type type="image/vnd.adobe.photoshop">
<comment>Photoshop image</comment>
<comment xml:lang="pt_BR">Imagem do Photoshop</comment>
<glob pattern="*.psd"/>
<alias type="image/psd"/>
<alias type="image/x-psd"/>
<alias type="image/photoshop"/>
<alias type="image/x-photoshop"/>
<alias type="application/photoshop"/>
<alias type="application/x-photoshop"/>
++++END+++

and I used that output in another shell script that generates java code like this:

MIMEInfoObject obj;
obj.setMimeType("application/pdf");
obj.setDefaultDescription("PDF document");
obj.setLocalDescription("Documento PDF");
obj.setAcronym("PDF");
obj.setExtendedAcronym("Portable Document Format");
if (obj.getExtensionList() == null) {
    obj.setExtensionList(new ArrayList<String>());
}
extList = obj.getExtensionList();
extList.add("*.pdf");
obj.setExtensionList(extList);
if (obj.getAliasList() == null) {
    obj.setAliasList(new ArrayList<String>());
}
aliasList = obj.getAliasList();
aliasList.add("application/x-pdf");
obj.setAliasList(aliasList);
if (obj.getAliasList() == null) {
    obj.setAliasList(new ArrayList<String>());
}
aliasList = obj.getAliasList();
aliasList.add("image/pdf");
obj.setAliasList(aliasList);
if (obj.getAliasList() == null) {
    obj.setAliasList(new ArrayList<String>());
}
aliasList = obj.getAliasList();
aliasList.add("application/acrobat");
obj.setAliasList(aliasList);
mimeString2Obj.put(obj.getMimeType(), obj);
for (String alias : obj.getAliasList()) {
    mimeString2Obj.put(alias, obj);
}

I know the code above is stupid, but it's how I can generate it, but the problem is that it generates about 17000 lines of code like that. Java is complaining about the method's bytes limit, which is 65535, and it's exceeded.

My concern is if anyone has a better idea of how to do this, taking in mind that I'm using GWTP (so I can use JavaScript although I'm not an expert) and my application will run both on Desktop/Mobile browsers and Mobile App (phonegap).

Thanks in advance.

The problem if I understand it correctly is that you're generating java code to explicitly load each object, as opposed to reusing java code to load objects from a data file. Don't do that. If you have more files, for instance, you would have to regenerate your code etc.

You need to create some kind of standardized data file, and create a method to read it. So if your data is CSV, or XML, then you can dump then in one/many files, and then it doesn't matter how much data you have, the java code will look the same. Just a simple loop which keeps reading and loading stuff in your map.

in pseudocode:

while (records.hasMoreRecords()) {
    records.read()...
    add record to map()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM