|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.starhope.util.LibMisc
public class LibMisc
Miscellaneous utility methods that might be useful elsewhere
Field Summary | |
---|---|
private static String[] |
cardinalNumberWords_English
Word-forms of English cardinal numbers |
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 Map<String,Properties> |
messages
The currently-loaded message catalogs |
Constructor Summary | |
---|---|
LibMisc()
|
Method Summary | ||
---|---|---|
static boolean |
areWeThereYet(AbstractUser thing,
long when)
WRITEME: document this method (brpocock@star-hope.org, Nov 24, 2009) |
|
private static String |
cardinalNumber_English(int number)
Get the English string form of a cardinal number |
|
static void |
closeAll(Object... things)
Close a bunch of things carefully, ignoring exceptions. |
|
static void |
commandJSON(String cmd,
org.json.JSONObject jso,
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
|
condense(Collection<Collection<T>> sets)
Condense a collection of collections down to just a collection. |
|
static double |
distance(AbstractUser from,
AbstractUser to,
long when)
WRITEME: document this method (brpocock@star-hope.org, Nov 24, 2009) |
|
static double |
distance(double x1,
double y1,
double x2,
double y2)
WRITEME: document this method (brpocock@star-hope.org, Nov 24, 2009) |
|
static void |
executeCommand(String cmd,
org.json.JSONObject jso,
CanProcessCommands commandThread,
AbstractUser user,
Method commandProcessor)
WRITEME: Document this method brpocock@star-hope.org |
|
private static String |
formatFutureDate_English(Date targetDate)
|
|
static String |
formatFutureDate(Date targetDate)
WRITEME: document this method (brpocock@star-hope.org, Jan 18, 2010) |
|
static String |
formatFutureDate(Date targetDate,
String language_dialect)
WRITEME brpocock@star-hope.org Jul 21, 2010 |
|
static String |
formatFutureDate(Date targetDate,
String language,
String dialect)
WRITEME: document this method (brpocock@star-hope.org, 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. |
|
private static String |
formatPastDate_English(Date targetDate)
|
|
static String |
formatPastDate(Date pastDate)
See formatFutureDate(Date, String, String) ; applies the
currently-selected default language of the thread context |
|
static String |
formatPastDate(Date targetDate,
String language,
String dialect)
WRITEME: document this method (brpocock@star-hope.org, Jan 18, 2010) |
|
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_dialect)
WRITEME brpocock@star-hope.org Jul 21, 2010 |
|
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 String |
getTextOrDefault(String key,
String language,
String dialect,
String fallback)
WRITEME: Document this method brpocock@star-hope.org |
|
static boolean |
hasText(String string)
WRITEME: document this method (brpocock@star-hope.org, Jan 5, 2010) |
|
static boolean |
hasText(String string,
String language,
String dialect)
WRITEME: document this method (brpocock@star-hope.org, Jan 5, 2010) |
|
static String |
hexify(byte[] input)
Convert an array of bytes into a string of ASCII hexadecimal nybbles. |
|
private static void |
initMessages()
Initialise the configured message catalogues. |
|
static double |
limit(double value,
double min,
double max)
Limit a value to being between the minimum and maximum allowed |
|
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)
WRITEME: document this method (brpocock@star-hope.org, 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)
WRITEME: document this method (brpocock@star-hope.org, Dec 1, 2009) |
|
static Map<Integer,String> |
randomize(Map<Integer,String> map)
twheys@gmail.com Feb 5, 2010 |
|
static String |
rot13(String msg)
Perform rot-13 on basic alphabetic characters in the font |
|
(package private) static void |
sendJSONCommandBugReport(String cmd,
org.json.JSONObject jso,
CanProcessCommands commandThread,
AbstractUser user,
InvocationTargetException e)
Send a bug report on something that occurred while attempting to process a JSON-based command |
|
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. |
|
private static void |
stringify_column(ResultSet rs,
StringBuilder s,
ResultSetMetaData meta,
int i)
WRITEME: Document this method brpocock@star-hope.org |
|
private static void |
stringify_form(ResultSet rs,
StringBuilder s,
ResultSetMetaData meta,
int numCols)
WRITEME: Document this method brpocock@star-hope.org |
|
private static void |
stringify_tabular(ResultSet rs,
StringBuilder s,
ResultSetMetaData meta,
int numCols)
WRITEME: Document this method brpocock@star-hope.org |
|
static String |
stringify(Map<? extends Object,? extends Object> map)
Convert a map into a string, mostly for debugging purposes. |
|
static String |
stringify(Object... crap)
|
|
static String |
stringify(ResultSet rs)
Dump an SQL ResultSet as a string table |
|
static String |
stringify(StackTraceElement[] stackTrace)
Create a pure string version of a stack backtrace |
|
static String |
stringify(Throwable e)
|
|
static String |
stringJoin(String[] array,
String delimiter)
Joins an array of strings together using the given delimiter |
|
static long |
timeToTarget(AbstractUser thing,
long when)
Deprecated. use Geometry.updateUserPositioning(AbstractUser, long, boolean) |
|
static String |
toJavaCase(String source)
convert a user-visible string into a javaCased moniker |
|
static String |
trimWhiteSpace(String string)
twheys@gmail.com Feb 17, 2010 |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final String[] cardinalNumberWords_English
private static final HashMap<Class<?>,Class<?>> extensionClasses
static int MAX_SHUTDOWN_TRIES
private static Map<String,Properties> messages
Constructor Detail |
---|
public LibMisc()
Method Detail |
---|
public static boolean areWeThereYet(AbstractUser thing, long when)
thing
- Who are we?when
- When are we?
private static String cardinalNumber_English(int number)
number
- a number
public static void closeAll(Object... things)
This is mostly meant for “finally” clauses.
things
- A set of SQL statements, result sets, and database
connectionspublic static void commandJSON(String cmd, org.json.JSONObject jso, 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.
cmd
- The command's string namejso
- JSON parameters to that commandcommandThread
- The command processor threaduser
- The user initiating the commandklass
- The command-processor class in which to search for
the commandpublic static <T> Collection<T> condense(Collection<Collection<T>> sets)
T
- whatever type of stuff you want to put into itsets
- a collection of collections
public static double distance(AbstractUser from, AbstractUser to, long when)
from
- one thingto
- another thingwhen
- when is it
public static double distance(double x1, double y1, double x2, double y2)
x1
- start x abcessay1
- start y ordinatex2
- end x abcessay2
- end y ordinate
public static void executeCommand(String cmd, org.json.JSONObject jso, CanProcessCommands commandThread, AbstractUser user, Method commandProcessor)
cmd
- WRITEMEjso
- WRITEMEcommandThread
- WRITEMEuser
- WRITEMEcommandProcessor
- WRITEMEpublic static String formatFutureDate(Date targetDate)
targetDate
- a date in the future
public static String formatFutureDate(Date targetDate, String language_dialect)
targetDate
- WRITEMElanguage_dialect
- WRITEME
public static String formatFutureDate(Date targetDate, String language, String dialect)
targetDate
- a date in the futurelanguage
- the user's languagedialect
- the user's dialect
private static String formatFutureDate_English(Date targetDate)
targetDate
- a date in the future
public static String formatMemory(long numBytes)
numBytes
- a number of bytes (size_t)
public static String formatPastDate(Date pastDate)
formatFutureDate(Date, String, String)
; applies the
currently-selected default language of the thread context
pastDate
- the date to be thus described
public static String formatPastDate(Date targetDate, String language, String dialect)
targetDate
- a date in the futurelanguage
- the user's languagedialect
- the user's dialect
private static String formatPastDate_English(Date targetDate)
targetDate
- a date in the future
public static String genFakeIP(Object o)
o
- The object for which to generate a fake IP address
public static String getText(String string)
string
- WRITEME
public static String getText(String string, String language_dialect)
string
- WRITEMElanguage_dialect
- WRITEME
public static String getText(String string, String language, String dialect)
string
- WRITEMElanguage
- WRITEMEdialect
- WRITEME
public static String getTextOrDefault(String key, String fallback)
key
- The key name which should exist in the message
catalogue.fallback
- The fallback text to be used if that message does
not exist.
public static String getTextOrDefault(String key, String language, String dialect, String fallback)
key
- language
- dialect
- fallback
-
public static boolean hasText(String string)
string
- WRITEME
public static boolean hasText(String string, String language, String dialect)
string
- WRITEMElanguage
- WRITEMEdialect
- WRITEME
public static String hexify(byte[] input)
input
- The bytes to be converted to hex
private static void initMessages()
Initialise the configured message catalogues.
Each message catalogue is loaded based upon the key named org.starhope.messages.LANG_DIALECT, and must be enumerated in the “master” list of org.starhope.messages.
In the absence of either key, the default message language set is simply en_US (US English), and the default message catalogues are /etc/appius/messages/$LANG_DIALECT.properties, e.g. /etc/appius/messages/en_US.properties
public static double limit(double value, double min, double max)
value
- the valuemin
- the least permitted valuemax
- the max permitted value
public static String listToDisplay(Collection<? extends Object> words, String language, String dialect)
words
- WRITEMElanguage
- WRITEMEdialect
- WRITEME
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
words
- A list of words.language
- The user's display languagedialect
- The user's sublanguage dialect
public static String listToDisplay(Object[] set, String language, String dialect)
set
- WRITEMElanguage
- WRITEMEdialect
- WRITEME
private static String listToDisplay_English(List<String> words)
listToDisplay(List, String, String)
for English.
words
- word list
private static String listToDisplay_Español(List<String> words)
listToDisplay(List, String, String)
for Spanish.
words
- word list
private static String listToDisplay_فرسئ(List<String> words)
listToDisplay(List, String, String)
for Persian.
words
- word list
public static Class<?> loadExtension(Class<?> klass)
klass
- The class for which an extension might exist
public static int makeHashCode(String string)
string
- WRITEME
public static Map<Integer,String> randomize(Map<Integer,String> map)
twheys@gmail.com Feb 5, 2010TO randomize return a shuffled map.
map
- the hashmap to shuffle
public static String rot13(String msg)
msg
- The string to be rot-13:ed
static void sendJSONCommandBugReport(String cmd, org.json.JSONObject jso, CanProcessCommands commandThread, AbstractUser user, InvocationTargetException e)
cmd
- WRITEMEjso
- WRITEMEcommandThread
- WRITEMEuser
- WRITEMEe
- WRITEMEpublic static void setMaxShutdownTries(int mst)
mst
- maximum number of tries to shut down a socket input
bufferpublic 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
socket
- The socket whose input stream is to be shut down
IOException
- If the input stream cannot be shut down
despite best effortspublic static LinkedHashMap<Integer,Integer> sortHashMapByValues(Map<Integer,Integer> stuffToSort)
stuffToSort
- The hash table to be sorted
public static String stringify(Map<? extends Object,? extends Object> map)
map
- a map object to be stringified
public static String stringify(Object... crap)
crap
- arbitrary stuff to stringify
public static String stringify(ResultSet rs)
rs
- result set to dump
public static String stringify(StackTraceElement[] stackTrace)
stackTrace
- An array of StackTraceElements:s
public static String stringify(Throwable e)
e
- A Throwable to be stringified into a backtrace
private static void stringify_column(ResultSet rs, StringBuilder s, ResultSetMetaData meta, int i) throws SQLException
rs
- WRITEMEs
- WRITEMEmeta
- WRITEMEi
- WRITEME
SQLException
- WRITEMEprivate static void stringify_form(ResultSet rs, StringBuilder s, ResultSetMetaData meta, int numCols) throws SQLException
rs
- WRITEMEs
- WRITEMEmeta
- WRITEMEnumCols
- WRITEME
SQLException
- WRITEMEprivate static void stringify_tabular(ResultSet rs, StringBuilder s, ResultSetMetaData meta, int numCols) throws SQLException
rs
- WRITEMEs
- WRITEMEmeta
- WRITEMEnumCols
- WRITEME
SQLException
- WRITEMEpublic static String stringJoin(String[] array, String delimiter)
array
- The array of stringsdelimiter
- The string to append between the other strings
@Deprecated public static long timeToTarget(AbstractUser thing, long when)
Geometry.updateUserPositioning(AbstractUser, long, boolean)
Determine the object's current position, and the time until it reaches its target (from now). Note that this was the preferred routine to be used to update the position of an object. Now, use #updateUserPositioning(AbstractUser,long)
When called through this interface, always executes side-effects
Identical to
Geometry.updateUserPositioning(AbstractUser, long, boolean)
with the last parameter as “true.”
thing
- what is movingwhen
- what time is it now
Geometry.getTimeToTarget(AbstractUser, long)
,
Geometry.updateUserPositioning(AbstractUser)
,
Geometry.updateUserPositioning(AbstractUser, long)
,
Geometry.updateUserPositioning(AbstractUser, long, boolean)
public static String toJavaCase(String source)
source
- the string provided
public static String trimWhiteSpace(String string)
twheys@gmail.com Feb 17, 2010Replace all groups of whitespace including return lines and tabs with a single space and trims the leading and trailing whitespace.
string
- A string to be trimmed.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |