Commit f83e6c97 authored by yu's avatar yu

add sorter

parent e20b5ac0
...@@ -162,45 +162,32 @@ public class FilenameInputPlugin ...@@ -162,45 +162,32 @@ public class FilenameInputPlugin
// We have to sort the files before we set them to the tasks. // We have to sort the files before we set them to the tasks.
// Here we get the parameter about the order // Here we get the parameter about the order
Comparator comparator;
String order = task.getLoadOrder(); String order = task.getLoadOrder();
if (order.equals("")){order = "ALPHABETICAL";} switch (order) {
case "ASCEND_MODIFIED": comparator = AscendModifiedSorter.getComparator();
break;
case "DESCEND_MODIFIED": comparator = DescendModifiedSorter.getComparator();
break;
case "ASCEND_CREATION": comparator = AscendCreationSorter.getComparator();
break;
case "DESCEND_CREATION": comparator = DescendModifiedSorter.getComparator();
break;
default: comparator = new Comparator<String>();
break;
}
// This method return the files in a directory,however the files in the varibale files is in random order. we have to sort them next // This method return the files in a directory,however the files in the varibale files is in random order. we have to sort them next
ArrayList<String> files = listFiles(task,Paths.get(dir).normalize(),lastPath,order); ArrayList<String> files = listFiles(task,Paths.get(dir).normalize(),lastPath,order);
// Sort the files if each directory
if (order.equals("ALPHABETICAL")){
Collections.sort(files); // Sort the files for each directory
} else if(order.equals("ASCEND_MODIFIED") || order.equals("DESCEND_MODIFIED")){ Collections.sort(files,comparator);
Collections.sort(files,new Comparator<String>(){
@Override
public int compare(String f1, String f2) {
try{
return getLastModifiedTime(f1).compareTo(getLastModifiedTime(f2));
} catch (IOException ex){
ex.printStackTrace();
}
return 0;
}
});
if (order.equals("DESCEND_MODIFIED")){ Collections.reverse(files); }
} else if ( order.equals("ASCEND_CREATION") || order.equals("DESCEND_CREATION") ){
Collections.sort(files,new Comparator<String>(){
@Override
public int compare(String f1, String f2) {
try{
return getCreationTime(f1).compareTo(getCreationTime(f2));
} catch (IOException ex){
ex.printStackTrace();
}
return 0;
}
});
if ( order.equals("DESCEND_CREATION") ) { Collections.reverse(files);}
} else {
throw new RuntimeException("Input a correct order");
}
// End of sort // End of sort
...@@ -398,3 +385,76 @@ public class FilenameInputPlugin ...@@ -398,3 +385,76 @@ public class FilenameInputPlugin
return fileTime; return fileTime;
} }
} }
interface Sorter {
public static Comparator getComparator();
}
class AscendModifiedSorter implements Sorter {
public static Comparator getComparator(){
return new Comparator<String>(){
@Override
public int compare(String f1, String f2) {
try{
return getLastModifiedTime(f1).compareTo(getLastModifiedTime(f2));
} catch (IOException ex){
ex.printStackTrace();
}
return 0;
}
});
}
}
class AscendCreationSorter implements Sorter {
public static Comparator getComparator(){
return new Comparator<String>(){
@Override
public int compare(String f1, String f2) {
try{
return getCreationTime(f1).compareTo(getCreationTime(f2));
} catch (IOException ex){
ex.printStackTrace();
}
return 0;
}
});
}
}
class DescendModifiedSorter implements Sorter {
public static Comparator getComparator(){
return new Comparator<String>(){
@Override
public int compare(String f1, String f2) {
try{
return - getLastModifiedTime(f1).compareTo(getLastModifiedTime(f2));
} catch (IOException ex){
ex.printStackTrace();
}
return 0;
}
});
}
}
class DescendCreationSorter implements Sorter {
public static Comparator getComparator(){
return new Comparator<String>(){
@Override
public int compare(String f1, String f2) {
try{
return - getCreationTime(f1).compareTo(getCreationTime(f2));
} catch (IOException ex){
ex.printStackTrace();
}
return 0;
}
});
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment