001 package org.bukkit.help; 002 003 import org.bukkit.help.HelpTopic; 004 005 import java.util.Comparator; 006 007 /** 008 * Used to impose a custom total ordering on help topics. 009 * <p> 010 * All topics are listed in alphabetic order, but topics that start with a 011 * slash come after topics that don't. 012 */ 013 public class HelpTopicComparator implements Comparator<HelpTopic> { 014 015 // Singleton implementations 016 private static final TopicNameComparator tnc = new TopicNameComparator(); 017 public static TopicNameComparator topicNameComparatorInstance() { 018 return tnc; 019 } 020 021 private static final HelpTopicComparator htc = new HelpTopicComparator(); 022 public static HelpTopicComparator helpTopicComparatorInstance() { 023 return htc; 024 } 025 026 private HelpTopicComparator() {} 027 028 public int compare(HelpTopic lhs, HelpTopic rhs) { 029 return tnc.compare(lhs.getName(), rhs.getName()); 030 } 031 032 public static class TopicNameComparator implements Comparator<String> { 033 private TopicNameComparator(){} 034 035 public int compare(String lhs, String rhs) { 036 boolean lhsStartSlash = lhs.startsWith("/"); 037 boolean rhsStartSlash = rhs.startsWith("/"); 038 039 if (lhsStartSlash && !rhsStartSlash) { 040 return 1; 041 } else if (!lhsStartSlash && rhsStartSlash) { 042 return -1; 043 } else { 044 return lhs.compareToIgnoreCase(rhs); 045 } 046 } 047 } 048 }