source: other-projects/nz-flag-design/trunk/similarity-2d/flag-functions.jsp@ 29906

Last change on this file since 29906 was 29906, checked in by davidb, 9 years ago

Changes resulting from writing the TPDL submission

File size: 2.6 KB
RevLine 
[29600]1 <%@ page import="java.io.*,java.util.*,java.awt.image.*,javax.imageio.*" %>
2 <%!
3 public int GCD(int a, int b) {
4 if (b==0) return a;
5 return GCD(b,a%b);
6 }
7
8 ArrayList<File> listFilesForFolder(final File folder, String matchExt) {
9 ArrayList<File> fileList = new ArrayList<File>();
10
11 for (final File fileEntry : folder.listFiles()) {
[29621]12 if (fileEntry.isFile()) {
13 if (fileEntry.getName().toLowerCase().endsWith(matchExt)) {
14 fileList.add(fileEntry);
15 }
16 }
17 }
18
19 return fileList;
20 }
21
[29906]22
23 ArrayList<File> listFilesForFolderRE(final File folder, String matchExtRE) {
24 ArrayList<File> fileList = new ArrayList<File>();
25
26 for (final File fileEntry : folder.listFiles()) {
27 if (fileEntry.isFile()) {
28 if (fileEntry.getName().toLowerCase().matches(matchExtRE)) {
29 fileList.add(fileEntry);
30 }
31 }
32 }
33
34 return fileList;
35 }
36
37
[29621]38 ArrayList<File> listFilesForFolderRecursive(final File folder, String matchExt) {
39 ArrayList<File> fileList = new ArrayList<File>();
40
41 for (final File fileEntry : folder.listFiles()) {
[29600]42 if (fileEntry.isDirectory()) {
43 listFilesForFolder(fileEntry,matchExt);
44 } else {
45 if (fileEntry.getName().toLowerCase().endsWith(matchExt)) {
46 fileList.add(fileEntry);
47 }
48 }
49 }
50
51 return fileList;
52 }
53
54 public static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap)
55 {
56
57 // Convert Map to List
58 List<Map.Entry<String, Integer>> list
59 = new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());
60
61 // Sort list with comparator, to compare the Map values
62 Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
63 public int compare(Map.Entry<String, Integer> o1,
64 Map.Entry<String, Integer> o2) {
65 return (o2.getValue()).compareTo(o1.getValue());
66 }
67 });
68
69 // Convert sorted map back to a Map
70 Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
71 for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
72 Map.Entry<String, Integer> entry = it.next();
73 sortedMap.put(entry.getKey(), entry.getValue());
74 }
75
76 return sortedMap;
77 }
78/*
79 public static void printMap(Map<String, Integer> map)
80 {
81 for (Map.Entry<String, Integer> entry : map.entrySet()) {
82 out.println("[Key] : " + entry.getKey()
83 + " [Value] : " + entry.getValue() + "br />");
84 }
85 }
86 */
87 %>
Note: See TracBrowser for help on using the repository browser.