1 package tim.prune.function;
3 import java.util.Comparator;
5 import tim.prune.data.DataPoint;
8 * Class for comparing photos to sort them by name or timestamp
10 public class PhotoComparer implements Comparator<DataPoint>
12 public enum SortMode {
13 SORTBY_NAME, SORTBY_TIME
17 private SortMode _sortMode = SortMode.SORTBY_NAME;
21 * @param inMode sort mode
23 public PhotoComparer(SortMode inMode)
31 public int compare(DataPoint inP1, DataPoint inP2)
33 if (inP2 == null || inP2.getPhoto() == null) return -1; // all nulls at end
34 if (inP1 == null || inP1.getPhoto() == null) return 1;
37 if (_sortMode == SortMode.SORTBY_NAME) {
38 result = compareNames(inP1, inP2);
41 result = compareTimes(inP1, inP2);
43 // check names if times didn't work
44 if (result == 0 && _sortMode == SortMode.SORTBY_TIME) {
45 result = compareNames(inP1, inP2);
47 // names and times equal, try width and height
49 result = compareSizes(inP1, inP2);
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)
60 private int compareNames(DataPoint inP1, DataPoint inP2)
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());
66 // both photos have files, so just compare the files
67 return inP1.getPhoto().getFile().compareTo(inP2.getPhoto().getFile());
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)
76 private int compareTimes(DataPoint inP1, DataPoint inP2)
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));
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)
92 private int compareSizes(DataPoint inP1, DataPoint inP2)
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);
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);