1 package tim.prune.data.sort;
3 import java.util.Comparator;
5 import tim.prune.data.DataPoint;
9 * Class for comparing photos to sort them by name or timestamp
11 public class PhotoComparer implements Comparator<DataPoint>
14 private SortMode _sortMode;
19 * @param inMode sort mode
21 public PhotoComparer(SortMode inMode)
29 public int compare(DataPoint inP1, DataPoint inP2)
31 if (inP2 == null || inP2.getPhoto() == null) return -1; // all nulls at end
32 if (inP1 == null || inP1.getPhoto() == null) return 1;
35 if (_sortMode == SortMode.SORTBY_NAME) {
36 result = compareNames(inP1, inP2);
39 result = compareTimes(inP1, inP2);
41 // check names if times didn't work
42 if (result == 0 && _sortMode == SortMode.SORTBY_TIME) {
43 result = compareNames(inP1, inP2);
45 // names and times equal, try width and height
47 result = compareSizes(inP1, inP2);
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)
58 private int compareNames(DataPoint inP1, DataPoint inP2)
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());
64 // both photos have files, so just compare the files
65 return inP1.getPhoto().getFile().compareTo(inP2.getPhoto().getFile());
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)
74 private int compareTimes(DataPoint inP1, DataPoint inP2)
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));
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)
90 private int compareSizes(DataPoint inP1, DataPoint inP2)
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);
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);