File: PhotoComparer.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (107 lines) | stat: -rw-r--r-- 2,763 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package tim.prune.data.sort;

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<DataPoint>
{
	/** Sort mode */
	private SortMode _sortMode;


	/**
	 * 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 result;
	}

	/**
	 * 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().getMillisecondsSince(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;
	}
}