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