0

I am trying to store the bundle name and its latest version in the Map.

Below is my newDirs which is an ArrayList<Map<String, String>>() from which I am supposed to get the Bundle name and its latest version-

[{objectName=/storage/Model/Framework/1.0.0/, objectId=4fa042a5a56c861104fa05c246cf850522a2354ca223, objectType=DIRECTORY}, 
{objectName=/storage/Model/Framework/1.0.1/, objectId=4fa042a5a66c860d04fa056bbe1cf50522a14094ca3f, objectType=DIRECTORY}]

Now from that above List, I am supposed to extract the latest version of Framework bundle . So in the above case, it is 1.0.1 version and the bundle name is Framework. So my map will store Framework as the Key and 1.0.1 as the version of the bundle in the above case.

Below is my code-

final List<Map<String, String>> newDirs = new ArrayList<Map<String, String>>();

for(String ss : list) {

    //some code here for newDirs

    Map<String, String> map = storageDirectorySort(newDirs);
    System.out.println(map);
}

/**
 * Sort the list and then return the map as the Bundle Name and its Latest version
 *
 */
private static Map<String, String> storageDirectorySort(List<Map<String, String>> newDirs) {

    Map<String, String> map = new LinkedHashMap<String, String>();

    // do the sorting here and always give back the latest version of the bundle and its name

    return map;
}

Can anybody help me with this. I am not sure what is the best way of doing this?

AKIWEB
  • 19,008
  • 67
  • 180
  • 294

1 Answers1

1

You want another helper method to help parse the version number. Then call it in your storageDirectorySort method:

    private static int getVersion(Map<String, String> dir) {
        String objectName = dir.get("objectName");
    // Get the various parts of the name
        String[] nameParts = objectName.split("/");
    // Get the version from the nameParts
        String[] versionString = nameParts[nameParts.length - 1].split("\\.");
        // Parse version String into an int
        return (Integer.valueOf(versionString[0]) * 1000000)
                + (Integer.valueOf(versionString[1]) * 10000)
                + (Integer.valueOf(versionString[2]) * 100);
    }

    private static Map<String, String> storageDirectorySort(
            List<Map<String, String>> newDirs) {

        Map<String, String> latestVersion = null;
        for (Map<String, String> bundle : newDirs) {
            int version = getVersion(bundle);
            if (latestVersion == null || version > getVersion(latestVersion)) {
                latestVersion = bundle;
            }
        }

        return latestVersion;
    }

Note: There is no exception handling in this code, I would recommend adding some. Other than that, I have tested it to verify that it works.

James Dunn
  • 8,064
  • 13
  • 53
  • 87