X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Ffunction%2FPhotoComparer.java;fp=tim%2Fprune%2Ffunction%2FPhotoComparer.java;h=86252d199bfd7d804c744f9a700ad4d6997a6d67;hb=4d5796d02a15808311c09448d79e6e7d1de9d636;hp=0000000000000000000000000000000000000000;hpb=f1b92378a792131ac8fb33a869405851d5b2d1f7;p=GpsPrune.git diff --git a/tim/prune/function/PhotoComparer.java b/tim/prune/function/PhotoComparer.java new file mode 100644 index 0000000..86252d1 --- /dev/null +++ b/tim/prune/function/PhotoComparer.java @@ -0,0 +1,109 @@ +package tim.prune.function; + +import java.util.Comparator; + +import tim.prune.data.DataPoint; + +/** + * Class for comparing photos to sort them by name or timestamp + */ +public class PhotoComparer implements Comparator +{ + public enum SortMode { + SORTBY_NAME, SORTBY_TIME + }; + + /** Sort mode */ + private SortMode _sortMode = SortMode.SORTBY_NAME; + + /** + * Constructor + * @param inMode sort mode + */ + public PhotoComparer(SortMode inMode) + { + _sortMode = inMode; + } + + /** + * Main compare method + */ + public int compare(DataPoint inP1, DataPoint inP2) + { + if (inP2 == null || inP2.getPhoto() == null) return -1; // all nulls at end + if (inP1 == null || inP1.getPhoto() == null) return 1; + // Sort by name + int result = 0; + if (_sortMode == SortMode.SORTBY_NAME) { + result = compareNames(inP1, inP2); + } + if (result == 0) { + result = compareTimes(inP1, inP2); + } + // check names if times didn't work + if (result == 0 && _sortMode == SortMode.SORTBY_TIME) { + result = compareNames(inP1, inP2); + } + // names and times equal, try width and height + if (result == 0) { + result = compareSizes(inP1, inP2); + } + return 0; + } + + /** + * Compare the names of the two photo points + * @param inP1 first point + * @param inP2 second point + * @return compare value (-1,0,1) + */ + private int compareNames(DataPoint inP1, DataPoint inP2) + { + // If the files can't be compared, use the photo names + if (inP1.getPhoto().getFile() == null || inP2.getPhoto().getFile() == null) { + return inP1.getPhoto().getName().compareTo(inP2.getPhoto().getName()); + } + // both photos have files, so just compare the files + return inP1.getPhoto().getFile().compareTo(inP2.getPhoto().getFile()); + } + + /** + * Compare the timestamps of the two photo points + * @param inP1 first point + * @param inP2 second point + * @return compare value (-1,0,1) + */ + private int compareTimes(DataPoint inP1, DataPoint inP2) + { + // Photos might not have timestamps + if (!inP2.hasTimestamp()) return -1; + if (!inP1.hasTimestamp()) return 1; + // Compare the timestamps + long secDiff = inP1.getPhoto().getTimestamp().getSecondsSince(inP2.getPhoto().getTimestamp()); + return (secDiff<0?-1:(secDiff==0?0:1)); + } + + /** + * Compare the sizes of the two photos + * @param inP1 first point + * @param inP2 second point + * @return compare value (-1,0,1) + */ + private int compareSizes(DataPoint inP1, DataPoint inP2) + { + // Try the widths + int w1 = inP1.getPhoto().getWidth(); + int w2 = inP2.getPhoto().getWidth(); + if (w2 <= 0) return -1; + if (w1 <= 0) return 1; + if (w1 != w2) return (w2 > w1 ? 1 : -1); + // Try the heights + int h1 = inP1.getPhoto().getHeight(); + int h2 = inP2.getPhoto().getHeight(); + if (h2 <= 0) return -1; + if (h1 <= 0) return 1; + if (h1 != h2) return (h2 > h1 ? 1 : -1); + // sizes same + return 0; + } +}