Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
HelpTopicComparator.java
1 package org.bukkit.help;
2 
3 import org.bukkit.help.HelpTopic;
4 
5 import java.util.Comparator;
6 
7 /**
8  * Used to impose a custom total ordering on help topics.
9  * <p>
10  * All topics are listed in alphabetic order, but topics that start with a
11  * slash come after topics that don't.
12  */
13 public class HelpTopicComparator implements Comparator<HelpTopic> {
14 
15  // Singleton implementations
16  private static final TopicNameComparator tnc = new TopicNameComparator();
17  public static TopicNameComparator topicNameComparatorInstance() {
18  return tnc;
19  }
20 
21  private static final HelpTopicComparator htc = new HelpTopicComparator();
22  public static HelpTopicComparator helpTopicComparatorInstance() {
23  return htc;
24  }
25 
26  private HelpTopicComparator() {}
27 
28  public int compare(HelpTopic lhs, HelpTopic rhs) {
29  return tnc.compare(lhs.getName(), rhs.getName());
30  }
31 
32  public static class TopicNameComparator implements Comparator<String> {
33  private TopicNameComparator(){}
34 
35  public int compare(String lhs, String rhs) {
36  boolean lhsStartSlash = lhs.startsWith("/");
37  boolean rhsStartSlash = rhs.startsWith("/");
38 
39  if (lhsStartSlash && !rhsStartSlash) {
40  return 1;
41  } else if (!lhsStartSlash && rhsStartSlash) {
42  return -1;
43  } else {
44  return lhs.compareToIgnoreCase(rhs);
45  }
46  }
47  }
48 }