Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static List<String> getSortedGPXFilenamesByDate(File dir, boolean absolutePath) {
        final Map<String, Long> mp = new HashMap<String, Long>();
        readGpxDirectory(dir, mp, "", absolutePath);
        ArrayList<String> list = new ArrayList<String>(mp.keySet());
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String object1, String object2) {
                Long l1 = mp.get(object1);
                Long l2 = mp.get(object2);
                long lhs = l1 == null ? 0 : l1.longValue();
                long rhs = l2 == null ? 0 : l2.longValue();
                return lhs < rhs ? 1 : (lhs == rhs ? 0 : -1);
            }
        });
        return list;
    }

    private static void readGpxDirectory(File dir, final Map<String, Long> map, String parent,
            boolean absolutePath) {
        if (dir != null && dir.canRead()) {
            File[] files = dir.listFiles();
            if (files != null) {
                for (File f : files) {
                    if (f.getName().toLowerCase().endsWith(".gpx")) { //$NON-NLS-1$
                        map.put(absolutePath ? f.getAbsolutePath() : parent + f.getName(), f.lastModified());
                    } else if (f.isDirectory()) {
                        readGpxDirectory(f, map, parent + f.getName() + "/", absolutePath);
                    }
                }
            }
        }
    }
}