-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPrimitiveComparator.cls
63 lines (59 loc) · 3.36 KB
/
PrimitiveComparator.cls
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
/* ============================================================
* This code is part of the "apex-lang" open source project avaiable at:
*
* http://code.google.com/p/apex-lang/
*
* This code is licensed under the Apache License, Version 2.0. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
* ============================================================
*/
global class PrimitiveComparator implements ObjectComparator{
global Integer compare(Object object1, Object object2){
if(object1==null){
return -1;
} else if(object2==null){
return 1;
} else if(object1 instanceof Boolean && object2 instanceof Boolean){
return compare((Boolean)object1, (Boolean)object2);
} else if(object1 instanceof Date && object2 instanceof Date){
return compare((Date)object1, (Date)object2);
} else if(object1 instanceof Datetime && object2 instanceof Datetime){
return compare((Datetime)object1, (Datetime)object2);
} else if(object1 instanceof Integer && object2 instanceof Integer){
return compare((Integer)object1, (Integer)object2);
} else if(object1 instanceof Long && object2 instanceof Long){
return compare((Long)object1, (Long)object2);
// } else if(object1 instanceof Decimal && object2 instanceof Decimal){
// return compare((Decimal)object1, (Decimal)object2);
} else if(object1 instanceof Double && object2 instanceof Double){
return compare((Double)object1, (Double)object2);
} else if(object1 instanceof Time && object2 instanceof Time){
return compare((Time)object1, (Time)object2);
} else if(object1 instanceof String && object2 instanceof String){
return compare((String)object1, (String)object2);
} else {
throw new IllegalArgumentException(
'Both arguments must be type Boolean, Date, Datetime, Decimal, Double, ID, Integer, Long, Time, or String');
}
}
private Integer compare(Boolean b1, Boolean b2){
if(!b1 && b2){
return -1;
} else if(b1 == b2){
return 0;
} else {
return 1;
}
}
private Integer compare(Date d1, Date d2){ if(d1 < d2){ return -1; } else if(d1 == d2){ return 0; } else { return 1; } }
private Integer compare(Datetime d1, Datetime d2){ if(d1 < d2){ return -1; } else if(d1 == d2){ return 0; } else { return 1; } }
// private Integer compare(Decimal d1, Decimal d2){ if(d1 < d2){ return -1; } else if(d1 == d2){ return 0; } else { return 1; } }
private Integer compare(Double d1, Double d2){ if(d1 < d2){ return -1; } else if(d1 == d2){ return 0; } else { return 1; } }
// private Integer compare(ID i1, ID i2){ if(i1 < i2){ return -1; } else if(i1 == i2){ return 0; } else { return 1; } }
private Integer compare(Integer i1, Integer i2){ if(i1 < i2){ return -1; } else if(i1 == i2){ return 0; } else { return 1; } }
private Integer compare(Long l1, Long l2){ if(l1 < l2){ return -1; } else if(l1 == l2){ return 0; } else { return 1; } }
private Integer compare(Time t1, Time t2){ return compare(''+t1,''+t2); }
private Integer compare(String s1, String s2){ if(s1 < s2){ return -1; } else if(s1 == s2){ return 0; } else { return 1; } }
}