summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/Sorters.java
blob: e1a6d877809d339c4d9198f7951e9d1188394876 (plain) (blame)
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
package org.openslx.dozmod.thrift;

import java.util.Comparator;

import org.openslx.bwlp.thrift.iface.Location;
import org.openslx.bwlp.thrift.iface.NetShareAuth;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Organization;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.dozmod.thrift.cache.MetaDataCache;
import org.openslx.dozmod.thrift.cache.UserCache;
import org.openslx.dozmod.util.FormatHelper;

/**
 * Collection of comparators targeted at sorting different objects in a
 * human-suitable way (e.g. sort users by their display name)
 */
public class Sorters {

	public static final Comparator<UserInfo> userName = new Comparator<UserInfo>() {
		@Override
		public int compare(UserInfo o1, UserInfo o2) {
			return FormatHelper.userName(o1).compareTo(FormatHelper.userName(o2));
		}
	};

	public static final Comparator<String> userNameById = new Comparator<String>() {
		@Override
		public int compare(String o1, String o2) {
			return userName.compare(UserCache.find(o1), UserCache.find(o2));
		}
	};

	public static final Comparator<OperatingSystem> osName = new Comparator<OperatingSystem>() {
		@Override
		public int compare(OperatingSystem o1, OperatingSystem o2) {
			if (o1 == null) {
				if (o2 == null)
					return 0;
				return 1;
			} else if (o2 == null) {
				return -1;
			}
			return FormatHelper.osName(o1).compareTo(FormatHelper.osName(o2));
		}
	};

	public static final Comparator<Integer> osNameById = new Comparator<Integer>() {
		@Override
		public int compare(Integer o1, Integer o2) {
			if (o1 == null) {
				o1 = 0;
			}
			if (o2 == null) {
				o2 = 0;
			}
			return osName.compare(MetaDataCache.getOsById(o1), MetaDataCache.getOsById(o2));
		}
	};

	public static Comparator<Organization> organization = new Comparator<Organization>() {
		public int compare(Organization o1, Organization o2) {
			if (o1 == null || o1.displayName == null) {
				if (o2 == null)
					return 0;
				return -1;
			} else if (o2 == null) {
				return 1;
			}
			return o1.displayName.compareTo(o2.displayName);
		}
	};

	public static Comparator<Location> locByName = new Comparator<Location>() {
		public int compare(Location o1, Location o2) {
			if (o1 == null || o1.locationName == null) {
				if (o2 == null)
					return 0;
				return -1;
			} else if (o2 == null) {
				return 1;
			}
			return o1.locationName.compareTo(o2.locationName);
		}
	};

	public static final Comparator<NetShareAuth> netShareAuth = new Comparator<NetShareAuth>() {
		@Override
		public int compare(NetShareAuth o1, NetShareAuth o2) {
			// just need a sorter, doesn't really matter how to sort them
			if (o1 == null) {
				if (o2 == null)
					return 0;
				return -1;
			} else if (o2 == null) {
				return 1;
			}
			return o1.compareTo(o2);
		}
	};

}