aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java
blob: cede956dd065a19e0fcd33ccf13b2cfc80228cb7 (plain)
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
package eu.equalparts.cardbase.comparator;


final class ComparatorDelegates {

	private ComparatorDelegates() {}

	/*
	 * Delegates
	 */
	/**
	 * Compares two dirty numbers, that is, strings containing a single number
	 * and, optionally, additional non-numerical characters.
	 * For instance, when comparing "150a" and "36", this comparator delegate
	 * first compares 150 with 36 using the standard integer comparison, and
	 * returns an integer > 1 because 150 > 36.
	 * 
	 * @param field1 the first field to compare.
	 * @param field2 the second field to compare.
	 * @return a value smaller than, equal to or greater than 0 according to the standard comparison convention.
	 */
	public static Integer compareDirtyNumber(Comparable<String> field1, Comparable<String> field2) {
		// this assumes that the format is uninterrupted numbers and letters
		String number1 = ((String) field1).replaceAll("[^0-9]+", "");
		String number2 = ((String) field2).replaceAll("[^0-9]+", "");
		
		Integer int1 = number1.matches("[0-9]+") ? Integer.parseInt(number1) : null;
		Integer int2 = number2.matches("[0-9]+") ? Integer.parseInt(number2) : null;
		
		if (int1 == null) {
			if (int2 != null) {
				// field1 has no numbers but field2 does, field1 is less by default
				// if neither have numbers, letters will be looked at below
				return -1;
			}
		} else if (int2 == null) {
			// field2 has no numbers but field1 does, field1 is more by default
			return 1;
		} else {
			// both have numbers, perform comparison if not identical
			// if identical, look at letters below
			if (int1 != int2)
				return int1.compareTo(int2);
		}
		// compare by letters
		String letter1 = number1.replaceAll("[0-9]+", "");
		String letter2 = number2.replaceAll("[0-9]+", "");
		return letter1.compareTo(letter2);
	}

	public static Integer compareRarity(Comparable<String> field1, Comparable<String> field2) {
		// assign numerical values based on the different possibilities and compare those instead
		Integer value1 = getRarityValue((String) field1);
		Integer value2 = getRarityValue((String) field2);
		
		return value1.compareTo(value2);
	}
	
	/*
	 * Internally used utility functions
	 */
	private static int getRarityValue(String rarityString) {
		return rarityString.equalsIgnoreCase("Common") ? 0 :
			   rarityString.equalsIgnoreCase("Uncommon") ? 1 :
			   rarityString.equalsIgnoreCase("Rare") ? 2 :
			   rarityString.equalsIgnoreCase("Mythic Rare") ? 3 : 
			   rarityString.equalsIgnoreCase("Basic Land") ? 4 : 
			   rarityString.equalsIgnoreCase("Special") ? 5 : 6;
	}
}