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 userName = new Comparator() { @Override public int compare(UserInfo o1, UserInfo o2) { return FormatHelper.userName(o1).compareTo(FormatHelper.userName(o2)); } }; public static final Comparator userNameById = new Comparator() { @Override public int compare(String o1, String o2) { return userName.compare(UserCache.find(o1), UserCache.find(o2)); } }; public static final Comparator osName = new Comparator() { @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 osNameById = new Comparator() { @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 = new Comparator() { 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 locByName = new Comparator() { 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 = new Comparator() { @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); } }; }