org.starhope.util
Class LibMisc

java.lang.Object
  extended by org.starhope.util.LibMisc

public class LibMisc
extends Object

Miscellaneous utility methods that might be useful elsewhere

Author:
brpocock

Field Summary
private static HashMap<Class<?>,Class<?>> extensionClasses
          An extension class can be registered to supplement or replace methods in the basic built-in command interpreters.
(package private) static int MAX_SHUTDOWN_TRIES
          Maximum number of times to clear the socket input buffer.
private static Properties messages
          TODO: document this field (brpocock, Nov 19, 2009) messages (LibMisc)
 
Constructor Summary
LibMisc()
           
 
Method Summary
static boolean areWeThereYet(AbstractUser thing, long when)
          TODO: document this method (brpocock, Nov 24, 2009)
static void commandJSON(String cmd, org.json.JSONObject jso, org.starhope.util.types.CanProcessCommands commandThread, AbstractUser user, Class<?> klass)
           Execute a command with a certain method signature from klass, passing it the JSON parameters as well as the environment (thread, zone, and user) to run with.
static double distance(AbstractUser from, AbstractUser to, long when)
          TODO: document this method (brpocock, Nov 24, 2009)
private static double distance(double x1, double y1, double x2, double y2)
          TODO: document this method (brpocock, Nov 24, 2009)
private static String formatFutureDate_English(Date targetDate)
           
static String formatFutureDate(Date targetDate)
          TODO: document this method (brpocock, Jan 18, 2010)
static String formatFutureDate(Date targetDate, String language, String dialect)
          TODO: document this method (brpocock, Jan 18, 2010)
static String formatMemory(long numBytes)
          Create an user-visible string using metric figures accurate to 1 decimal place, expressing an amount of memory in KiB, MiB, &c.
static String genFakeIP(Object o)
          Generate something that resembles an IP address, but is clearly not a valid host address.
static String getText(String string)
           
static String getText(String string, String language, String dialect)
           
static String getTextOrDefault(String key, String fallback)
          Look for a string key in the message catalogue.
static boolean hasText(String string)
          TODO: document this method (brpocock, Jan 5, 2010)
static boolean hasText(String string, String language, String dialect)
          TODO: document this method (brpocock, Jan 5, 2010)
static String hexify(byte[] input)
          Convert an array of bytes into a string of ASCII hexadecimal nybbles.
private static void initMessages()
          Initialize the configured message catalog.
private static String listToDisplay_English(List<String> words)
          Internal helper method for listToDisplay(List, String, String) for English.
private static String listToDisplay_Español(List<String> words)
          Internal helper method for listToDisplay(List, String, String) for Spanish.
private static String listToDisplay_فرسئ(List<String> words)
          Internal helper method for listToDisplay(List, String, String) for Persian.
static String listToDisplay(Collection<? extends Object> words, String language, String dialect)
          TODO: document this method (brpocock, Jan 12, 2010)
static String listToDisplay(List<String> words, String language, String dialect)
           Given a list of strings, combine then into a string for display purposes.
static String listToDisplay(Object[] set, String language, String dialect)
           
static Class<?> loadExtension(Class<?> klass)
          Find a substitute class for a command interpreter class with local extensions.
static int makeHashCode(String string)
          TODO: document this method (brpocock, Dec 1, 2009)
static String rot13(String msg)
          Perform rot-13 on basic alphabetic characters in the font
static void setMaxShutdownTries(int mst)
          Set the maximum number of times to clear the socket input buffer.
static void shutdownInput(Socket socket)
           This method taken from Apache Tomcat:
static LinkedHashMap<Integer,Integer> sortHashMapByValues(Map<Integer,Integer> stuffToSort)
          Sort the contents of a hash map based upon comparing the values of its keys.
static String stringify(Map<Object,Object> map)
          Convert a map into a string, mostly for debugging purposes.
static long timeToTarget(AbstractUser thing, long when)
          TODO: document this method (brpocock, Nov 24, 2009)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

extensionClasses

private static final HashMap<Class<?>,Class<?>> extensionClasses
An extension class can be registered to supplement or replace methods in the basic built-in command interpreters. This is where they're stored.


MAX_SHUTDOWN_TRIES

static int MAX_SHUTDOWN_TRIES
Maximum number of times to clear the socket input buffer.


messages

private static Properties messages
TODO: document this field (brpocock, Nov 19, 2009) messages (LibMisc)

Constructor Detail

LibMisc

public LibMisc()
Method Detail

areWeThereYet

public static boolean areWeThereYet(AbstractUser thing,
                                    long when)
TODO: document this method (brpocock, Nov 24, 2009)

Parameters:
thing - Who are we?
when - When are we?
Returns:
true if we've arrived on station

commandJSON

public static void commandJSON(String cmd,
                               org.json.JSONObject jso,
                               org.starhope.util.types.CanProcessCommands commandThread,
                               AbstractUser user,
                               Class<?> klass)

Execute a command with a certain method signature from klass, passing it the JSON parameters as well as the environment (thread, zone, and user) to run with.

The class klass is searched for a method with the name "do_COMMAND", which must take a Zone, JSONObject, User, and Integer (as a room number in which the user is standing) as input parameters. If no such method is found, the extension class (if any) defined for klass using loadExtension(Class) will also be searched, before reporting an error back to the command thread.

Parameters:
cmd - The command's string name
jso - JSON parameters to that command
commandThread - The command processor thread
user - The user initiating the command
klass - The command-processor class in which to search for the command

distance

public static double distance(AbstractUser from,
                              AbstractUser to,
                              long when)
TODO: document this method (brpocock, Nov 24, 2009)

Parameters:
from - one thing
to - another thing
when - when is it
Returns:
the distance between the two objects

distance

private static double distance(double x1,
                               double y1,
                               double x2,
                               double y2)
TODO: document this method (brpocock, Nov 24, 2009)

Parameters:
x1 - start x abcessa
y1 - start y ordinate
x2 - end x abcessa
y2 - end y ordinate
Returns:
distance

formatFutureDate

public static String formatFutureDate(Date targetDate)
TODO: document this method (brpocock, Jan 18, 2010)

Parameters:
targetDate - a date in the future
Returns:
a human-legible expression describing the time in the future

formatFutureDate

public static String formatFutureDate(Date targetDate,
                                      String language,
                                      String dialect)
TODO: document this method (brpocock, Jan 18, 2010)

Parameters:
targetDate - a date in the future
language - the user's language
dialect - the user's dialect
Returns:
a human-legible expression describing the time in the future

formatFutureDate_English

private static String formatFutureDate_English(Date targetDate)
Parameters:
targetDate - a date in the future
Returns:
a human-legible expression describing the time in the future

formatMemory

public static String formatMemory(long numBytes)
Create an user-visible string using metric figures accurate to 1 decimal place, expressing an amount of memory in KiB, MiB, &c. to one decimal place. For example, “12.1 KiB” or “4.2 GiB”.

Parameters:
numBytes - a number of bytes (size_t)
Returns:
a human-legible string

genFakeIP

public static String genFakeIP(Object o)
Generate something that resembles an IP address, but is clearly not a valid host address. Nonetheless, this address will contain valid byte values. Some byte in the address will be a 255 or the address will begin or end with a zero byte. Since this method uses the object's hash code, it will be consistent for an object.

Parameters:
o - The object for which to generate a fake IP address
Returns:
a string in dotted-quad form

getText

public static String getText(String string)
Parameters:
string - WRITEME
Returns:
WRITEME

getText

public static String getText(String string,
                             String language,
                             String dialect)
Parameters:
string - WRITEME
language - WRITEME
dialect - WRITEME
Returns:
WRITEME

getTextOrDefault

public static String getTextOrDefault(String key,
                                      String fallback)
Look for a string key in the message catalogue. If a given key does exist, return that message. If not, fall back to the provided default text.

Parameters:
key - The key name which should exist in the message catalogue.
fallback - The fallback text to be used if that message does not exist.
Returns:
Either the message from the catalogue, or the fallback message if one is unavailable.

hasText

public static boolean hasText(String string)
TODO: document this method (brpocock, Jan 5, 2010)

Parameters:
string - WRITEME
Returns:
WRITEME

hasText

public static boolean hasText(String string,
                              String language,
                              String dialect)
TODO: document this method (brpocock, Jan 5, 2010)

Parameters:
string - WRITEME
language - WRITEME
dialect - WRITEME
Returns:
WRITEME

hexify

public static String hexify(byte[] input)
Convert an array of bytes into a string of ASCII hexadecimal nybbles.

Parameters:
input - The bytes to be converted to hex
Returns:
the hex equivalent

initMessages

private static void initMessages()
Initialize the configured message catalog. Does not currently cope with languages and dialects properly. (TODO: have multiple properties files, for various languages and dialects, and replace the single "messages" attribute with a Map or similar.)


listToDisplay

public static String listToDisplay(Collection<? extends Object> words,
                                   String language,
                                   String dialect)
TODO: document this method (brpocock, Jan 12, 2010)

Parameters:
words - WRITEME
language - WRITEME
dialect - WRITEME
Returns:
WRITEME

listToDisplay

public static String listToDisplay(List<String> words,
                                   String language,
                                   String dialect)

Given a list of strings, combine then into a string for display purposes.

For English, the list will obey the traditional grammatical usage of commas: List elements are joined with commas, except that the conjunction (in our case, always “and”) occurs penultimate, and two or three element lists do not use commas.

For Spanish, works essentially the same way.

For other languages, we just join the words with commas and omit the conjunction

Parameters:
words - A list of words.
language - The user's display language
dialect - The user's sublanguage dialect
Returns:
The list formatted into a string.

listToDisplay

public static String listToDisplay(Object[] set,
                                   String language,
                                   String dialect)
Parameters:
set - WRITEME
language - WRITEME
dialect - WRITEME
Returns:
WRITEME

listToDisplay_English

private static String listToDisplay_English(List<String> words)
Internal helper method for listToDisplay(List, String, String) for English.

Parameters:
words - word list
Returns:
list formatted for display in English

listToDisplay_Español

private static String listToDisplay_Español(List<String> words)
Internal helper method for listToDisplay(List, String, String) for Spanish.

Parameters:
words - word list
Returns:
list formatted for display in Spanish

listToDisplay_فرسئ

private static String listToDisplay_فرسئ(List<String> words)
Internal helper method for listToDisplay(List, String, String) for Persian.

Parameters:
words - word list
Returns:
list formatted for display in Persian

loadExtension

public static Class<?> loadExtension(Class<?> klass)
Find a substitute class for a command interpreter class with local extensions. These are defined by specifying the canonical class name in the configuration as a parameter to a key of the form "xtn." plus the canonical class name of the main class to which the extension applies.

Parameters:
klass - The class for which an extension might exist
Returns:
An extension class, if one exists; else, the same class as passed-in

makeHashCode

public static int makeHashCode(String string)
TODO: document this method (brpocock, Dec 1, 2009)

Parameters:
string - WRITEME
Returns:
WRITEME

rot13

public static String rot13(String msg)
Perform rot-13 on basic alphabetic characters in the font

Parameters:
msg - The string to be rot-13:ed
Returns:
The string after rot-13

setMaxShutdownTries

public static void setMaxShutdownTries(int mst)
Set the maximum number of times to clear the socket input buffer.

Parameters:
mst - maximum number of tries to shut down a socket input buffer

shutdownInput

public static void shutdownInput(Socket socket)
                          throws IOException

This method taken from Apache Tomcat:

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Shut down the input stream of a connection

Parameters:
socket - The socket whose input stream is to be shut down
Throws:
IOException - If the input stream cannot be shut down despite best efforts

sortHashMapByValues

public static LinkedHashMap<Integer,Integer> sortHashMapByValues(Map<Integer,Integer> stuffToSort)
Sort the contents of a hash map based upon comparing the values of its keys. This implementation takes a hash map tying integers to integers, and is used for e.g. sorting high scores by players

Parameters:
stuffToSort - The hash table to be sorted
Returns:
the stuff all sorted

stringify

public static String stringify(Map<Object,Object> map)
Convert a map into a string, mostly for debugging purposes.

Parameters:
map - a map object to be stringified
Returns:
a string containing all keys and values in the map

timeToTarget

public static long timeToTarget(AbstractUser thing,
                                long when)
TODO: document this method (brpocock, Nov 24, 2009)

Parameters:
thing - what is moving
when - what time is it now
Returns:
how long until it gets there