]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/PhotoComparer.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / function / PhotoComparer.java
1 package tim.prune.function;
2
3 import java.util.Comparator;
4
5 import tim.prune.data.DataPoint;
6
7 /**
8  * Class for comparing photos to sort them by name or timestamp
9  */
10 public class PhotoComparer implements Comparator<DataPoint>
11 {
12         public enum SortMode {
13                 SORTBY_NAME, SORTBY_TIME
14         };
15
16         /** Sort mode */
17         private SortMode _sortMode = SortMode.SORTBY_NAME;
18
19         /**
20          * Constructor
21          * @param inMode sort mode
22          */
23         public PhotoComparer(SortMode inMode)
24         {
25                 _sortMode = inMode;
26         }
27
28         /**
29          * Main compare method
30          */
31         public int compare(DataPoint inP1, DataPoint inP2)
32         {
33                 if (inP2 == null || inP2.getPhoto() == null) return -1; // all nulls at end
34                 if (inP1 == null || inP1.getPhoto() == null) return 1;
35                 // Sort by name
36                 int result = 0;
37                 if (_sortMode == SortMode.SORTBY_NAME) {
38                         result = compareNames(inP1, inP2);
39                 }
40                 if (result == 0) {
41                         result = compareTimes(inP1, inP2);
42                 }
43                 // check names if times didn't work
44                 if (result == 0 && _sortMode == SortMode.SORTBY_TIME) {
45                         result = compareNames(inP1, inP2);
46                 }
47                 // names and times equal, try width and height
48                 if (result == 0) {
49                         result = compareSizes(inP1, inP2);
50                 }
51                 return 0;
52         }
53
54         /**
55          * Compare the names of the two photo points
56          * @param inP1 first point
57          * @param inP2 second point
58          * @return compare value (-1,0,1)
59          */
60         private int compareNames(DataPoint inP1, DataPoint inP2)
61         {
62                 // If the files can't be compared, use the photo names
63                 if (inP1.getPhoto().getFile() == null || inP2.getPhoto().getFile() == null) {
64                         return inP1.getPhoto().getName().compareTo(inP2.getPhoto().getName());
65                 }
66                 // both photos have files, so just compare the files
67                 return inP1.getPhoto().getFile().compareTo(inP2.getPhoto().getFile());
68         }
69
70         /**
71          * Compare the timestamps of the two photo points
72          * @param inP1 first point
73          * @param inP2 second point
74          * @return compare value (-1,0,1)
75          */
76         private int compareTimes(DataPoint inP1, DataPoint inP2)
77         {
78                 // Photos might not have timestamps
79                 if (!inP2.hasTimestamp()) return -1;
80                 if (!inP1.hasTimestamp()) return 1;
81                 // Compare the timestamps
82                 long secDiff = inP1.getPhoto().getTimestamp().getSecondsSince(inP2.getPhoto().getTimestamp());
83                 return (secDiff<0?-1:(secDiff==0?0:1));
84         }
85
86         /**
87          * Compare the sizes of the two photos
88          * @param inP1 first point
89          * @param inP2 second point
90          * @return compare value (-1,0,1)
91          */
92         private int compareSizes(DataPoint inP1, DataPoint inP2)
93         {
94                 // Try the widths
95                 int w1 = inP1.getPhoto().getWidth();
96                 int w2 = inP2.getPhoto().getWidth();
97                 if (w2 <= 0) return -1;
98                 if (w1 <= 0) return 1;
99                 if (w1 != w2) return (w2 > w1 ? 1 : -1);
100                 // Try the heights
101                 int h1 = inP1.getPhoto().getHeight();
102                 int h2 = inP2.getPhoto().getHeight();
103                 if (h2 <= 0) return -1;
104                 if (h1 <= 0) return 1;
105                 if (h1 != h2) return (h2 > h1 ? 1 : -1);
106                 // sizes same
107                 return 0;
108         }
109 }