From 262ec248f5cc8e669703c52962d616ab5825a50c Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 10 Apr 2013 10:45:47 +0200 Subject: [PATCH] More Txt. --- .../massivecraft/factions/FactionColl.java | 3 +- src/com/massivecraft/factions/Perm.java | 13 +- .../massivecraft/factions/cmd/CmdAccess.java | 6 +- .../factions/cmd/CmdDescription.java | 5 +- .../massivecraft/factions/cmd/CmdShow.java | 11 +- .../massivecraft/factions/cmd/CmdTitle.java | 4 +- .../factions/util/HealthBarUtil.java | 6 +- .../factions/util/RelationUtil.java | 4 +- .../massivecraft/factions/zcore/MCommand.java | 14 +- .../massivecraft/factions/zcore/MPlugin.java | 3 - .../zcore/persist/EntityCollection.java | 5 +- .../factions/zcore/util/PermUtil.java | 97 ------ .../factions/zcore/util/TextUtil.java | 325 ------------------ 13 files changed, 37 insertions(+), 459 deletions(-) delete mode 100644 src/com/massivecraft/factions/zcore/util/PermUtil.java delete mode 100644 src/com/massivecraft/factions/zcore/util/TextUtil.java diff --git a/src/com/massivecraft/factions/FactionColl.java b/src/com/massivecraft/factions/FactionColl.java index f2761bd0..086f9b34 100644 --- a/src/com/massivecraft/factions/FactionColl.java +++ b/src/com/massivecraft/factions/FactionColl.java @@ -15,7 +15,6 @@ import com.massivecraft.mcore.xlib.gson.reflect.TypeToken; import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.zcore.persist.EntityCollection; -import com.massivecraft.factions.zcore.util.TextUtil; public class FactionColl extends EntityCollection { @@ -253,7 +252,7 @@ public class FactionColl extends EntityCollection tag2faction.put(ChatColor.stripColor(faction.getTag()), faction); } - String tag = TextUtil.getBestStartWithCI(tag2faction.keySet(), searchFor); + String tag = Txt.getBestCIStart(tag2faction.keySet(), searchFor); if (tag == null) return null; return tag2faction.get(tag); } diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index 319be9fd..d04249de 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -1,6 +1,8 @@ package com.massivecraft.factions; -import org.bukkit.command.CommandSender; +import org.bukkit.permissions.Permissible; + +import com.massivecraft.mcore.util.PermUtil; public enum Perm { @@ -89,13 +91,14 @@ public enum Perm // HAS // -------------------------------------------- // - public boolean has(CommandSender sender, boolean informSenderIfNot) + public boolean has(Permissible permissible, boolean informSenderIfNot) { - return Factions.get().perm.has(sender, this.node, informSenderIfNot); + return PermUtil.has(permissible, this.node, informSenderIfNot); } - public boolean has(CommandSender sender) + public boolean has(Permissible permissible) { - return has(sender, false); + return has(permissible, false); } + } diff --git a/src/com/massivecraft/factions/cmd/CmdAccess.java b/src/com/massivecraft/factions/cmd/CmdAccess.java index a069efdd..0bf9d49c 100644 --- a/src/com/massivecraft/factions/cmd/CmdAccess.java +++ b/src/com/massivecraft/factions/cmd/CmdAccess.java @@ -8,7 +8,7 @@ import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Perm; import com.massivecraft.factions.TerritoryAccess; import com.massivecraft.factions.integration.SpoutFeatures; -import com.massivecraft.factions.zcore.util.TextUtil; +import com.massivecraft.mcore.util.Txt; public class CmdAccess extends FCommand @@ -88,14 +88,14 @@ public class CmdAccess extends FCommand target = "Faction \""+targetFaction.getTag()+"\""; } - msg("%s has been %s the access list for this territory.", target, TextUtil.parseColor(added ? "added to" : "removed from")); + msg("%s has been %s the access list for this territory.", target, Txt.parse(added ? "added to" : "removed from")); SpoutFeatures.updateAccessInfoLoc(loc); showAccessList(territory, locFaction); } private void showAccessList(TerritoryAccess territory, Faction locFaction) { - msg("Host faction %s has %s in this territory.", locFaction.getTag(), TextUtil.parseColor(territory.isHostFactionAllowed() ? "normal access" : "restricted access")); + msg("Host faction %s has %s in this territory.", locFaction.getTag(), Txt.parse(territory.isHostFactionAllowed() ? "normal access" : "restricted access")); String players = territory.fplayerList(); String factions = territory.factionList(); diff --git a/src/com/massivecraft/factions/cmd/CmdDescription.java b/src/com/massivecraft/factions/cmd/CmdDescription.java index d9629403..7cd1566d 100644 --- a/src/com/massivecraft/factions/cmd/CmdDescription.java +++ b/src/com/massivecraft/factions/cmd/CmdDescription.java @@ -4,7 +4,7 @@ import com.massivecraft.factions.ConfServer; import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayerColl; import com.massivecraft.factions.Perm; -import com.massivecraft.factions.zcore.util.TextUtil; +import com.massivecraft.mcore.util.Txt; public class CmdDescription extends FCommand { @@ -32,7 +32,8 @@ public class CmdDescription extends FCommand // if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay if ( ! payForCommand(ConfServer.econCostDesc, "to change faction description", "for changing faction description")) return; - myFaction.setDescription(TextUtil.implode(args, " ").replaceAll("(&([a-f0-9]))", "& $2")); // since "&" color tags seem to work even through plain old FPlayer.sendMessage() for some reason, we need to break those up + // TODO: This must be an invalid replace-approach. The call order is wrong somehow? + myFaction.setDescription(Txt.implode(args, " ").replaceAll("(&([a-f0-9]))", "& $2")); // since "&" color tags seem to work even through plain old FPlayer.sendMessage() for some reason, we need to break those up if ( ! ConfServer.broadcastDescriptionChanges) { diff --git a/src/com/massivecraft/factions/cmd/CmdShow.java b/src/com/massivecraft/factions/cmd/CmdShow.java index 3c20fc4e..85f7b731 100644 --- a/src/com/massivecraft/factions/cmd/CmdShow.java +++ b/src/com/massivecraft/factions/cmd/CmdShow.java @@ -12,7 +12,6 @@ import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Faction; import com.massivecraft.factions.Perm; import com.massivecraft.factions.Rel; -import com.massivecraft.factions.zcore.util.TextUtil; import com.massivecraft.mcore.util.Txt; public class CmdShow extends FCommand @@ -103,11 +102,11 @@ public class CmdShow extends FCommand } else { - sendMessage(Txt.parse("In Truce with: ") + TextUtil.implode(relationTags.get(Rel.TRUCE), sepparator)); + sendMessage(Txt.parse("In Truce with: ") + Txt.implode(relationTags.get(Rel.TRUCE), sepparator)); } - sendMessage(Txt.parse("Allied to: ") + TextUtil.implode(relationTags.get(Rel.ALLY), sepparator)); - sendMessage(Txt.parse("Enemies: ") + TextUtil.implode(relationTags.get(Rel.ENEMY), sepparator)); + sendMessage(Txt.parse("Allied to: ") + Txt.implode(relationTags.get(Rel.ALLY), sepparator)); + sendMessage(Txt.parse("Enemies: ") + Txt.implode(relationTags.get(Rel.ENEMY), sepparator)); // List the members... List memberOnlineNames = new ArrayList(); @@ -160,8 +159,8 @@ public class CmdShow extends FCommand memberOfflineNames.add(follower.getNameAndTitle(fme)); } } - sendMessage(Txt.parse("Members online: ") + TextUtil.implode(memberOnlineNames, sepparator)); - sendMessage(Txt.parse("Members offline: ") + TextUtil.implode(memberOfflineNames, sepparator)); + sendMessage(Txt.parse("Members online: ") + Txt.implode(memberOnlineNames, sepparator)); + sendMessage(Txt.parse("Members offline: ") + Txt.implode(memberOfflineNames, sepparator)); } } diff --git a/src/com/massivecraft/factions/cmd/CmdTitle.java b/src/com/massivecraft/factions/cmd/CmdTitle.java index 6061cc8c..73178cb4 100644 --- a/src/com/massivecraft/factions/cmd/CmdTitle.java +++ b/src/com/massivecraft/factions/cmd/CmdTitle.java @@ -4,7 +4,7 @@ import com.massivecraft.factions.ConfServer; import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Perm; import com.massivecraft.factions.integration.SpoutFeatures; -import com.massivecraft.factions.zcore.util.TextUtil; +import com.massivecraft.mcore.util.Txt; public class CmdTitle extends FCommand { @@ -31,7 +31,7 @@ public class CmdTitle extends FCommand if (you == null) return; args.remove(0); - String title = TextUtil.implode(args, " "); + String title = Txt.implode(args, " "); if ( ! canIAdministerYou(fme, you)) return; diff --git a/src/com/massivecraft/factions/util/HealthBarUtil.java b/src/com/massivecraft/factions/util/HealthBarUtil.java index 90d3ec3a..b2a4da98 100644 --- a/src/com/massivecraft/factions/util/HealthBarUtil.java +++ b/src/com/massivecraft/factions/util/HealthBarUtil.java @@ -3,7 +3,7 @@ package com.massivecraft.factions.util; import java.util.Map.Entry; import com.massivecraft.factions.ConfServer; -import com.massivecraft.factions.zcore.util.TextUtil; +import com.massivecraft.mcore.util.Txt; public class HealthBarUtil { @@ -22,13 +22,13 @@ public class HealthBarUtil int emptyCount = (int) ((barLength - solidCount) / ConfServer.spoutHealthBarSolidsPerEmpty); // Create the non-parsed bar - String ret = ConfServer.spoutHealthBarLeft + TextUtil.repeat(ConfServer.spoutHealthBarSolid, solidCount) + ConfServer.spoutHealthBarBetween + TextUtil.repeat(ConfServer.spoutHealthBarEmpty, emptyCount) + ConfServer.spoutHealthBarRight; + String ret = ConfServer.spoutHealthBarLeft + Txt.repeat(ConfServer.spoutHealthBarSolid, solidCount) + ConfServer.spoutHealthBarBetween + Txt.repeat(ConfServer.spoutHealthBarEmpty, emptyCount) + ConfServer.spoutHealthBarRight; // Replace color tag ret = ret.replace("{c}", color); // Parse amp color codes - ret = TextUtil.parseColorAmp(ret); + ret = Txt.parse(ret); return ret; } diff --git a/src/com/massivecraft/factions/util/RelationUtil.java b/src/com/massivecraft/factions/util/RelationUtil.java index 67e773aa..e3b737f9 100644 --- a/src/com/massivecraft/factions/util/RelationUtil.java +++ b/src/com/massivecraft/factions/util/RelationUtil.java @@ -8,7 +8,7 @@ import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Faction; import com.massivecraft.factions.Rel; import com.massivecraft.factions.iface.RelationParticipator; -import com.massivecraft.factions.zcore.util.TextUtil; +import com.massivecraft.mcore.util.Txt; public class RelationUtil { @@ -57,7 +57,7 @@ public class RelationUtil if (ucfirst) { - ret = TextUtil.upperCaseFirst(ret); + ret = Txt.upperCaseFirst(ret); } return "" + getColorOfThatToMe(that, me) + ret; diff --git a/src/com/massivecraft/factions/zcore/MCommand.java b/src/com/massivecraft/factions/zcore/MCommand.java index 8ba0dadf..a346a95a 100644 --- a/src/com/massivecraft/factions/zcore/MCommand.java +++ b/src/com/massivecraft/factions/zcore/MCommand.java @@ -9,7 +9,7 @@ import org.bukkit.entity.Player; import com.massivecraft.factions.zcore.MCommand; import com.massivecraft.factions.zcore.MPlugin; -import com.massivecraft.factions.zcore.util.TextUtil; +import com.massivecraft.mcore.util.PermUtil; import com.massivecraft.mcore.util.Txt; @@ -43,7 +43,7 @@ public abstract class MCommand { if (this.helpShort == null) { - String pdesc = p.perm.getPermissionDescription(this.permission); + String pdesc = PermUtil.getDescription(permission); if (pdesc != null) { return pdesc; @@ -182,7 +182,7 @@ public abstract class MCommand public boolean validSenderPermissions(CommandSender sender, boolean informSenderIfNot) { if (this.permission == null) return true; - return p.perm.has(sender, this.permission, informSenderIfNot); + return PermUtil.has(sender, this.permission, informSenderIfNot); } public boolean validArgs(List args, CommandSender sender) @@ -203,7 +203,7 @@ public abstract class MCommand { // Get the to many string slice List theToMany = args.subList(this.requiredArgs.size() + this.optionalArgs.size(), args.size()); - msg(Lang.commandToManyArgs, TextUtil.implode(theToMany, " ")); + msg(Lang.commandToManyArgs, Txt.implode(theToMany, " ")); sender.sendMessage(this.getUseageTemplate()); } return false; @@ -227,11 +227,11 @@ public abstract class MCommand for (MCommand mc : commandChain) { - ret.append(TextUtil.implode(mc.aliases, ",")); + ret.append(Txt.implode(mc.aliases, ",")); ret.append(' '); } - ret.append(TextUtil.implode(this.aliases, ",")); + ret.append(Txt.implode(this.aliases, ",")); List args = new ArrayList(); @@ -257,7 +257,7 @@ public abstract class MCommand if (args.size() > 0) { ret.append(Txt.parse("

")); - ret.append(TextUtil.implode(args, " ")); + ret.append(Txt.implode(args, " ")); } if (addShortHelp) diff --git a/src/com/massivecraft/factions/zcore/MPlugin.java b/src/com/massivecraft/factions/zcore/MPlugin.java index cb03d725..2007f527 100644 --- a/src/com/massivecraft/factions/zcore/MPlugin.java +++ b/src/com/massivecraft/factions/zcore/MPlugin.java @@ -8,7 +8,6 @@ import org.bukkit.plugin.java.JavaPlugin; import com.massivecraft.factions.zcore.persist.EM; import com.massivecraft.factions.zcore.persist.SaveTask; -import com.massivecraft.factions.zcore.util.PermUtil; import com.massivecraft.factions.zcore.util.Persist; import com.massivecraft.mcore.util.Txt; import com.massivecraft.mcore.xlib.gson.Gson; @@ -19,7 +18,6 @@ public abstract class MPlugin extends JavaPlugin { // Some utils public Persist persist; - public PermUtil perm; // Persist related public Gson gson; @@ -45,7 +43,6 @@ public abstract class MPlugin extends JavaPlugin this.getDataFolder().mkdirs(); // Create Utility Instances - this.perm = new PermUtil(this); this.persist = new Persist(this); // GSON 2.1 is now embedded in CraftBukkit, used by the auto-updater: https://github.com/Bukkit/CraftBukkit/commit/0ed1d1fdbb1e0bc09a70bc7bfdf40c1de8411665 diff --git a/src/com/massivecraft/factions/zcore/persist/EntityCollection.java b/src/com/massivecraft/factions/zcore/persist/EntityCollection.java index 9c85534c..fa084482 100644 --- a/src/com/massivecraft/factions/zcore/persist/EntityCollection.java +++ b/src/com/massivecraft/factions/zcore/persist/EntityCollection.java @@ -7,8 +7,8 @@ import java.util.logging.Level; import java.util.Map.Entry; import org.bukkit.Bukkit; -import com.massivecraft.factions.zcore.util.TextUtil; import com.massivecraft.mcore.util.DiscUtil; +import com.massivecraft.mcore.util.Txt; import com.massivecraft.mcore.xlib.gson.Gson; import com.massivecraft.mcore.xlib.gson.JsonSyntaxException; @@ -100,7 +100,8 @@ public abstract class EntityCollection public E getBestIdMatch(String pattern) { - String id = TextUtil.getBestStartWithCI(this.id2entity.keySet(), pattern); + String id = Txt.getBestCIStart(this.id2entity.keySet(), pattern); + if (id == null) return null; return this.id2entity.get(id); } diff --git a/src/com/massivecraft/factions/zcore/util/PermUtil.java b/src/com/massivecraft/factions/zcore/util/PermUtil.java deleted file mode 100644 index 2a962bc3..00000000 --- a/src/com/massivecraft/factions/zcore/util/PermUtil.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.massivecraft.factions.zcore.util; - -import java.util.*; -import java.util.Map.Entry; - -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.permissions.Permission; - -import com.massivecraft.factions.zcore.Lang; -import com.massivecraft.factions.zcore.MPlugin; -import com.massivecraft.mcore.util.Txt; - - -public class PermUtil { - - public Map permissionDescriptions = new HashMap(); - - protected MPlugin p; - - public PermUtil(MPlugin p) - { - this.p = p; - this.setup(); - } - - public String getForbiddenMessage(String perm) - { - return Txt.parse(Lang.permForbidden, getPermissionDescription(perm)); - } - - /** - * This method hooks into all permission plugins we are supporting - */ - public final void setup() - { - for(Permission permission : p.getDescription().getPermissions()) - { - //p.log("\""+permission.getName()+"\" = \""+permission.getDescription()+"\""); - this.permissionDescriptions.put(permission.getName(), permission.getDescription()); - } - } - - public String getPermissionDescription (String perm) - { - String desc = permissionDescriptions.get(perm); - if (desc == null) - { - return Lang.permDoThat; - } - return desc; - } - - /** - * This method tests if me has a certain permission and returns - * true if me has. Otherwise false - */ - public boolean has (CommandSender me, String perm) - { - if (me == null) return false; - - if ( ! (me instanceof Player)) - { - return me.hasPermission(perm); - } - - return me.hasPermission(perm); - } - - public boolean has (CommandSender me, String perm, boolean informSenderIfNot) - { - if (has(me, perm)) - { - return true; - } - else if (informSenderIfNot && me != null) - { - me.sendMessage(this.getForbiddenMessage(perm)); - } - return false; - } - - public T pickFirstVal(CommandSender me, Map perm2val) - { - if (perm2val == null) return null; - T ret = null; - - for ( Entry entry : perm2val.entrySet()) - { - ret = entry.getValue(); - if (has(me, entry.getKey())) break; - } - - return ret; - } - -} diff --git a/src/com/massivecraft/factions/zcore/util/TextUtil.java b/src/com/massivecraft/factions/zcore/util/TextUtil.java deleted file mode 100644 index 18ace345..00000000 --- a/src/com/massivecraft/factions/zcore/util/TextUtil.java +++ /dev/null @@ -1,325 +0,0 @@ -package com.massivecraft.factions.zcore.util; - -import java.util.*; -import java.util.Map.Entry; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.bukkit.ChatColor; -import org.bukkit.Material; - -public class TextUtil -{ - public Map tags; - public TextUtil() - { - this.tags = new HashMap(); - } - - // -------------------------------------------- // - // Top-level parsing functions. - // -------------------------------------------- // - - public String parse(String str, Object... args) - { - return String.format(this.parse(str), args); - } - - public String parse(String str) - { - return this.parseTags(parseColor(str)); - } - - // -------------------------------------------- // - // Tag parsing - // -------------------------------------------- // - - public String parseTags(String str) - { - return replaceTags(str, this.tags); - } - - public static final transient Pattern patternTag = Pattern.compile("<([a-zA-Z0-9_]*)>"); - public static String replaceTags(String str, Map tags) - { - StringBuffer ret = new StringBuffer(); - Matcher matcher = patternTag.matcher(str); - while (matcher.find()) - { - String tag = matcher.group(1); - String repl = tags.get(tag); - if (repl == null) - { - matcher.appendReplacement(ret, "<"+tag+">"); - } - else - { - matcher.appendReplacement(ret, repl); - } - } - matcher.appendTail(ret); - return ret.toString(); - } - - // -------------------------------------------- // - // Color parsing - // -------------------------------------------- // - - public static String parseColor(String string) - { - string = parseColorAmp(string); - string = parseColorAcc(string); - string = parseColorTags(string); - return string; - } - - public static String parseColorAmp(String string) - { - string = string.replaceAll("(ยง([a-z0-9]))", "\u00A7$2"); - string = string.replaceAll("(&([a-z0-9]))", "\u00A7$2"); - string = string.replace("&&", "&"); - return string; - } - - public static String parseColorAcc(String string) - { - return string.replace("`e", "") - .replace("`r", ChatColor.RED.toString()) .replace("`R", ChatColor.DARK_RED.toString()) - .replace("`y", ChatColor.YELLOW.toString()) .replace("`Y", ChatColor.GOLD.toString()) - .replace("`g", ChatColor.GREEN.toString()) .replace("`G", ChatColor.DARK_GREEN.toString()) - .replace("`a", ChatColor.AQUA.toString()) .replace("`A", ChatColor.DARK_AQUA.toString()) - .replace("`b", ChatColor.BLUE.toString()) .replace("`B", ChatColor.DARK_BLUE.toString()) - .replace("`p", ChatColor.LIGHT_PURPLE.toString()) .replace("`P", ChatColor.DARK_PURPLE.toString()) - .replace("`k", ChatColor.BLACK.toString()) .replace("`s", ChatColor.GRAY.toString()) - .replace("`S", ChatColor.DARK_GRAY.toString()) .replace("`w", ChatColor.WHITE.toString()); - } - - public static String parseColorTags(String string) - { - return string.replace("", "") - .replace("", "\u00A70") - .replace("", "\u00A71") - .replace("", "\u00A72") - .replace("", "\u00A73") - .replace("", "\u00A74") - .replace("", "\u00A75") - .replace("", "\u00A76") - .replace("", "\u00A77") - .replace("", "\u00A78") - .replace("", "\u00A79") - .replace("", "\u00A7a") - .replace("", "\u00A7b") - .replace("", "\u00A7c") - .replace("", "\u00A7d") - .replace("", "\u00A7e") - .replace("", "\u00A7f"); - } - - // -------------------------------------------- // - // Standard utils like UCFirst, implode and repeat. - // -------------------------------------------- // - - public static String upperCaseFirst(String string) - { - return string.substring(0, 1).toUpperCase()+string.substring(1); - } - - public static String repeat(String s, int times) - { - if (times <= 0) return ""; - else return s + repeat(s, times-1); - } - - public static String implode(List list, String glue) - { - StringBuilder ret = new StringBuilder(); - for (int i=0; i list, String comma, String and) - { - if (list.size() == 0) return ""; - if (list.size() == 1) return list.get(0); - - String lastItem = list.get(list.size()-1); - String nextToLastItem = list.get(list.size()-2); - String merge = nextToLastItem+and+lastItem; - list.set(list.size()-2, merge); - list.remove(list.size()-1); - - return implode(list, comma); - } - public static String implodeCommaAnd(List list) - { - return implodeCommaAnd(list, ", ", " and "); - } - - // -------------------------------------------- // - // Material name tools - // -------------------------------------------- // - - public static String getMaterialName(Material material) - { - return material.toString().replace('_', ' ').toLowerCase(); - } - - public static String getMaterialName(int materialId) - { - return getMaterialName(Material.getMaterial(materialId)); - } - - // -------------------------------------------- // - // Paging and chrome-tools like titleize - // -------------------------------------------- // - - private final static String titleizeLine = repeat("_", 52); - private final static int titleizeBalance = -1; - public String titleize(String str) - { - String center = ".[ "+ parseTags("") + str + parseTags("")+ " ]."; - int centerlen = ChatColor.stripColor(center).length(); - int pivot = titleizeLine.length() / 2; - int eatLeft = (centerlen / 2) - titleizeBalance; - int eatRight = (centerlen - eatLeft) + titleizeBalance; - - if (eatLeft < pivot) - return parseTags("")+titleizeLine.substring(0, pivot - eatLeft) + center + titleizeLine.substring(pivot + eatRight); - else - return parseTags("")+center; - } - - public ArrayList getPage(List lines, int pageHumanBased, String title) - { - ArrayList ret = new ArrayList(); - int pageZeroBased = pageHumanBased - 1; - int pageheight = 9; - int pagecount = (lines.size() / pageheight)+1; - - ret.add(this.titleize(title+" "+pageHumanBased+"/"+pagecount)); - - if (pagecount == 0) - { - ret.add(this.parseTags("Sorry. No Pages available.")); - return ret; - } - else if (pageZeroBased < 0 || pageHumanBased > pagecount) - { - ret.add(this.parseTags("Invalid page. Must be between 1 and "+pagecount)); - return ret; - } - - int from = pageZeroBased * pageheight; - int to = from+pageheight; - if (to > lines.size()) - { - to = lines.size(); - } - - ret.addAll(lines.subList(from, to)); - - return ret; - } - - // -------------------------------------------- // - // Describing Time - // -------------------------------------------- // - - /** - * Using this function you transform a delta in milliseconds - * to a String like "2 weeks from now" or "7 days ago". - */ - public static final long millisPerSecond = 1000; - public static final long millisPerMinute = 60 * millisPerSecond; - public static final long millisPerHour = 60 * millisPerMinute; - public static final long millisPerDay = 24 * millisPerHour; - public static final long millisPerWeek = 7 * millisPerDay; - public static final long millisPerMonth = 31 * millisPerDay; - public static final long millisPerYear = 365 * millisPerDay; - - public static Map unitMillis; - - static - { - unitMillis = new LinkedHashMap(); - unitMillis.put("years", millisPerYear); - unitMillis.put("months", millisPerMonth); - unitMillis.put("weeks", millisPerWeek); - unitMillis.put("days", millisPerDay); - unitMillis.put("hours", millisPerHour); - unitMillis.put("minutes", millisPerMinute); - unitMillis.put("seconds", millisPerSecond); - } - - public static String getTimeDeltaDescriptionRelNow(long millis) - { - String ret = ""; - - double millisLeft = (double) Math.abs(millis); - - List unitCountParts = new ArrayList(); - for (Entry entry : unitMillis.entrySet()) - { - if (unitCountParts.size() == 3 ) break; - String unitName = entry.getKey(); - long unitSize = entry.getValue(); - long unitCount = (long) Math.floor(millisLeft / unitSize); - if (unitCount < 1) continue; - millisLeft -= unitSize*unitCount; - unitCountParts.add(unitCount+" "+unitName); - } - - if (unitCountParts.size() == 0) return "just now"; - - ret += implodeCommaAnd(unitCountParts); - ret += " "; - if (millis <= 0) - { - ret += "ago"; - } - else - { - ret += "from now"; - } - - return ret; - } - - // -------------------------------------------- // - // String comparison - // -------------------------------------------- // - - public static String getBestStartWithCI(Collection candidates, String start) - { - String ret = null; - int best = 0; - - start = start.toLowerCase(); - int minlength = start.length(); - for (String candidate : candidates) - { - if (candidate.length() < minlength) continue; - if ( ! candidate.toLowerCase().startsWith(start)) continue; - - // The closer to zero the better - int lendiff = candidate.length() - minlength; - if (lendiff == 0) - { - return candidate; - } - if (lendiff < best || best == 0) - { - best = lendiff; - ret = candidate; - } - } - return ret; - } -}