|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.ceryle.util.Utilities
public class Utilities
Provides some general utilities for processing files, URLs, math, characters, strings, etc. All methods are static.
| Field Summary | |
|---|---|
static String |
EMPTY_STRING
|
static String |
RESOURCE_BUNDLE_CLASSNAME
The default ResourceBundle's class name. |
| Constructor Summary | |
|---|---|
Utilities()
|
|
| Method Summary | |
|---|---|
static boolean |
arrayContains(Object[] array,
Object o)
Returns true if the array contains the Object o. |
static boolean |
characterMatches(boolean ignoreCase,
char c1,
char c2)
Returns true if the character c1 matches character c2, ignoring case (as much as is provided by Java) if the boolean ignoreCase is true. |
static int |
charCount(String s,
char c)
Returns the number of times the provided character appears in the String. |
static File |
chooseDirectory(File defaultDirectory)
Displays a FileChooser dialog box and returns the selected directory (as a File), or null if no directory is chosen. |
static File |
chooseFile(Component parent,
String title,
File defaultDirectory,
File defaultFile,
int selectionMode,
int dialogType)
Provided with a JComponent parent, displays a FileChooser dialog box and returns the selected File, or null if no file is chosen. |
static File |
chooseFile(File defaultFile,
int dialogType)
Displays a FileChooser dialog box and returns the selected File, or null if no file is chosen. |
static double |
computeAngle(int x1,
int y1,
int x2,
int y2)
Returns the angle between the points (x1,y1) and (x2,y2), in radians. |
static double |
computeDistance(int x1,
int y1,
int x2,
int y2)
Returns the distance between the points (x1,y1) and (x2,y2) . |
static double |
computePerpendicular(int x1,
int y1,
int x2,
int y2)
Returns an angle perpendicular to the angle between the points (x1,y1) and (x2,y2), in radians. |
static boolean |
contains(String s1,
String s2,
boolean caseSensitive)
Returns true if String s1 contains s2, using case sensitive matching when the boolean caseSensitive is true. |
static String |
convertTildeEncoding(String s)
Converts all vowels followed by a tilde ('~') character to its equivalent macron'd version (in UTF-8) encoding. |
static int[] |
convertToIntArray(long[] longArray)
This converts a long[] array to an int[] array, checking each long value to see if it exceeds Integer.MAX_VALUE before proceeding, throwing an ArithmeticException on overflow. |
static List |
convertToList(String s,
String delim)
Converts the whitespace-delimited String s to a List of Strings. |
static String[] |
convertToStringArray(String s,
String delim)
Converts the whitespace-delimited String s to an array of Strings. |
static void |
delayedAction(int ms,
ActionListener listener)
After a delay of ms, fire a single ActionEvent on the supplied listener. |
static void |
deleteAllFiles(String directory,
String extension)
Delete all files having a specific extension (e.g., all "*.html" files) from the directory dir. |
static boolean |
deleteDir(MessageHandler mh,
File dir)
Recursively deletes all files and subdirectories under the directory dir. |
static String |
deWikify(String text)
Converts a WikiWord into a normal phrase by inserting spaces. |
static void |
fileCopy(File input,
File output)
Copies File input to File output. |
static File |
fileExtensionCheck(File file,
String ext)
Checks and potentially modifies the incoming File's extension. |
static Class |
findClass(List packages,
String className)
Searches the provided List of package names for className. |
static Class |
findClass(String packagename,
String className)
Calls findClass(List,String) with a single package name. |
static String |
getAlphaByNumber(int n)
Converts any number between 0 and 25 to its alphabetic character as a String. |
static String |
getBoundsAsString(Window window)
Returns the bounds of the provided Window as a String. |
static String |
getFileExtension(File file)
Returns the extension of File file. |
static String |
getFileExtension(String systemId)
Returns the file extension String provided a full system identifier. |
static File |
getFileFromURL(URL url)
Provided with a URL url, returns a File reference. |
static String |
getFilenameFromURL(URL url)
Provided with a URL url, returns the filename component. |
static long |
getFileSize(File file,
int maxfiles)
Returns the size of indicated File (or directory) file. |
static String |
getFormattedDecimal(double d,
int decimalPlaces)
Returns a String representing the double d appropriately formatted as a decimal in the default Locale. |
static String |
getFormattedDecimal(float f,
int decimalPlaces)
Returns a String representing the float f appropriately formatted as a decimal in the default Locale. |
static int |
getOS()
Returns an int representing the installed operating system. |
static String |
getStackTraceAsString(Throwable t)
Returns the stack trace from Throwable t as a single String. |
static URL |
getURLFromFile(File file)
Provided with a File file, returns a file-based URL. |
static URL |
getURLFromFilepath(String filepath)
Provided with a String filepath, returns a file-based URL. |
static String |
gsub(String src,
String srch,
String repl,
boolean ignoreCase)
Replace all (non-overlapping) occurrences of a String srch
in String src by replacement repl. |
static boolean |
hasFragmentID(String systemId)
Returns true if the provided system ID has a fragment ID. |
static String |
indent(int nest)
Returns a String indent composed of space characters given the indent nest. |
static int |
indexOf(boolean ignoreCase,
String source,
String substring,
int fromIndex)
Returns the index within String source of the first occurrence of the specified substring, starting at the specified index. |
static int |
indexOfIgnoreCase(String str,
String substring)
Returns the index within String str of the first occurrence of the specified substring, ignoring case. |
static int |
indexOfIgnoreCase(String str,
String substring,
int fromIndex)
Returns the index within String str of the first occurrence of the specified substring, ignoring case, and starting at the specified index. |
static int |
initialBucketSize(int n)
Returns an initial bucket size provided a rough estimate of a HashMap's size n. |
static Set |
intersection(Collection c1,
Collection c2)
Returns the intersection of the two Collections as a new Set. |
static boolean |
isURL(String sysid)
Returns true if the supplied sysid can be made into URL. |
static boolean |
isWS(char c)
Returns true if char c is a member of S (space) [XML 1.1 production 3]. |
static boolean |
listsAreEquivalent(List a,
List b)
Returns true if the two lists contain the same objects in the same order. |
static String |
localize(String messageId)
Return a String representing the localized message, provided the unique message identifier messageId. |
static String |
localize(String messageId,
String arg1)
Return a String representing the localized message, provided the unique message identifier messageId and one parameter arg1. |
static String |
localize(String messageId,
String arg1,
String arg2)
Return a String representing the localized message, provided the unique message identifier messageId and two parameters arg1 and arg2. |
static String |
localize(String messageId,
String arg1,
String arg2,
String arg3)
Return a String representing the localized message, provided the unique message identifier messageId and three parameters arg1, arg2, and arg3. |
static String |
localize(String messageId,
String arg1,
String arg2,
String arg3,
String arg4)
Return a String representing the localized message, provided the unique message identifier messageId and four parameters arg1, arg2, arg3, and arg4. |
static String |
localize(String messageId,
String arg1,
String arg2,
String arg3,
String arg4,
String arg5)
Return a String representing the localized message, provided the unique message identifier messageId and five parameters arg1, arg2, arg3, arg4, and arg5. |
static String |
localize(String messageId,
String arg1,
String arg2,
String arg3,
String arg4,
String arg5,
String arg6)
Return a String representing the localized message, provided the unique message identifier messageId and six parameters arg1, arg2, arg3, arg4, arg5, and arg6. |
static boolean |
matches(String s1,
String s2,
boolean caseSensitive)
Returns true if String s1 exactly matches s2, using case sensitive matching when the boolean caseSensitive is true. |
static String |
normalize(String s)
Normalizes the whitespace of the given String. |
static void |
normalize(StringBuffer sb)
Normalizes the whitespace of the given StringBuffer. |
static String |
oldGsub(String src,
String srch,
String repl,
boolean ignoreCase)
Replace all (non-overlapping) occurrences of a String srch in String
src by replacement repl. |
static Point |
parsePoint(String s)
Provided a String s containing a comma delimited x,y coordinate (eg., "210,336"), returns a Point. |
static Rectangle |
parseRectangle(String s)
Parses the provided String, returning a Rectangle. |
static String |
processMessage(String id,
Object[] args)
Get message text from the default, localized ResourceBundle using id
as key, then substitute arguments into (%1, %2, %3, etc.) placeholders in text. |
static String |
processMessage(String id,
Object[] args,
ResourceBundle bundle)
Get message text from the supplied ResourceBundle using id as
key, then substitute arguments into (%1, %2, %3, etc.) placeholders in text. |
static String |
readFile(File file)
A simple reader method to import the contents of a File file. |
static String |
repeatString(String s,
int n)
Returns a String composed of n repetitions of the provided String s. |
static String |
resolvePath(File rootDir,
String pathname)
Shortcut for resolvePath(String,String) |
static String |
resolvePath(String rootDir,
String pathname)
Given a String pathname, return as canonical pathname based on the supplied root path and content of supplied pathname. |
static String |
rsoundex(String s)
Returns a refined soundex encoding of the String s. |
static String[] |
separateFragmentID(String systemId)
Returns the system ID and fragment ID of the provided system ID as separate Strings. |
static String |
soundex(String s)
Returns a soundex encoding of the String s. |
static HashSet |
stringToSet(String s)
Processes the String s to provide a HashSet of Strings, using whitespace as the delimiter. |
static String |
sub(String src,
String srch,
String repl,
boolean ignoreCase)
Replace the first occurrence of a String srch in String
src by replacement repl. |
static void |
testAngles()
TEMP test angle methods |
static String |
toHexColor(Color color)
Converts a Color to an HTML-style hex color String. |
static Color |
toOpaqueColor(Color color)
Returns an opaque Color adjusted to roughly compensate for any transparency (alpha value), as against a white background. |
static Set |
union(Collection c1,
Collection c2)
Returns the union of the two Collections as a new Set. |
static void |
writeFile(File file,
String content)
A writer method to export a String to a File file. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String RESOURCE_BUNDLE_CLASSNAME
public static final String EMPTY_STRING
| Constructor Detail |
|---|
public Utilities()
| Method Detail |
|---|
public static int getOS()
public static URL getURLFromFile(File file)
throws MalformedURLException
MalformedURLException - if the URL cannot be created.
public static URL getURLFromFilepath(String filepath)
throws MalformedURLException
MalformedURLException - if the URL cannot be created.public static String getFilenameFromURL(URL url)
public static File getFileFromURL(URL url)
public static File fileExtensionCheck(File file,
String ext)
public static String getFileExtension(String systemId)
public static String getFileExtension(File file)
public static long getFileSize(File file,
int maxfiles)
throws IOException
IOException - if the File reference is null, does not exist, or can't be read.public static boolean hasFragmentID(String systemId)
public static String[] separateFragmentID(String systemId)
public static File chooseFile(File defaultFile,
int dialogType)
defaultFile - optional default File.dialogType - dialog type: JFileChooser.OPEN_DIALOG
or JFileChooser.SAVE_DIALOG.public static File chooseDirectory(File defaultDirectory)
defaultDirectory - optional default directory (as a File).
public static File chooseFile(Component parent,
String title,
File defaultDirectory,
File defaultFile,
int selectionMode,
int dialogType)
If a custom dialog is specified, title will be used for the dialog title; if a null value, the word "Select" will be used.
parent - parent view or window (can be null)title - optional dialog title (only used if the type is a CUSTOM_DIALOG).defaultDirectory - optional default directorydefaultFile - optional default FileselectionMode - selection mode: JFileChooser.FILES_ONLY,
JFileChooser.DIRECTORIES_ONLY, or
JFileChooser.FILES_AND_DIRECTORIES.dialogType - dialog type: JFileChooser.OPEN_DIALOG,
JFileChooser.SAVE_DIALOG.
or JFileChooser.CUSTOM_DIALOG. If the latter, a
dialog title must be supplied.
public static String readFile(File file)
throws IOException
IOException
public static void writeFile(File file,
String content)
throws IOException
readFile(File); processing is actually via a FileExporter
thread. No checking for overwrite or anything else is done. It is
assumed that the default character encoding is acceptable.
IOException
public static String resolvePath(File rootDir,
String pathname)
throws IOException
IOException
public static final String resolvePath(String rootDir,
String pathname)
public static void fileCopy(File input,
File output)
throws IOException
IOException
public static boolean deleteDir(MessageHandler mh,
File dir)
dir - the directory to be deletedmh - the optional MessageHandler to receive notification
public static void deleteAllFiles(String directory,
String extension)
throws IOException
IOExceptionpublic static final boolean isURL(String sysid)
public static Class findClass(String packagename,
String className)
throws ClassNotFoundException
findClass(List,String) with a single package name.
ClassNotFoundException
public static Class findClass(List packages,
String className)
throws ClassNotFoundException
packages - a List of Strings containing one or more package names.className - the name of the class to find.
ClassNotFoundException - if unsuccessful in finding the Class.
public static Set intersection(Collection c1,
Collection c2)
public static Set union(Collection c1,
Collection c2)
public static int initialBucketSize(int n)
public static String soundex(String s)
public static String rsoundex(String s)
public static String toHexColor(Color color)
public static Color toOpaqueColor(Color color)
public static boolean arrayContains(Object[] array,
Object o)
public static String[] convertToStringArray(String s,
String delim)
public static List convertToList(String s,
String delim)
public static boolean listsAreEquivalent(List a,
List b)
public static int[] convertToIntArray(long[] longArray)
throws ArithmeticException
ArithmeticExceptionpublic static void testAngles()
public static double computeDistance(int x1,
int y1,
int x2,
int y2)
public static double computeAngle(int x1,
int y1,
int x2,
int y2)
public static double computePerpendicular(int x1,
int y1,
int x2,
int y2)
public static void delayedAction(int ms,
ActionListener listener)
((Timer)event.getSource()).stop();
This is very much a convenience method.
public static String deWikify(String text)
text - the String to be deWikified.
public static String normalize(String s)
public static void normalize(StringBuffer sb)
public static boolean isWS(char c)
public static String getBoundsAsString(Window window)
public static Rectangle parseRectangle(String s)
throws NumberFormatException
NumberFormatException
public static Point parsePoint(String s)
throws NumberFormatException
NumberFormatException
public static String getFormattedDecimal(double d,
int decimalPlaces)
public static String getFormattedDecimal(float f,
int decimalPlaces)
This is identical to getFormattedDecimal(double,int) except for
handling floats rather than doubles.
public static String repeatString(String s,
int n)
public static HashSet stringToSet(String s)
public static String indent(int nest)
public static final int indexOfIgnoreCase(String str,
String substring)
(This will likely be rewritten at some point with an actual character-by-character implementation; due to internationalization considerations this is quite a bit more difficult than a simple character match so we're currently just relying on Java's i18n features.)
str - the source String.substring - the substring to search for.
-1 is returned.
public static final int indexOfIgnoreCase(String str,
String substring,
int fromIndex)
str - any String.substring - the substring to search for.fromIndex - the index to start the search from.
fromIndex, then the index of the first character
of the first such substring is returned. If it does not occur
as a substring starting at fromIndex or beyond,
-1 is returned.
public static int indexOf(boolean ignoreCase,
String source,
String substring,
int fromIndex)
Also, as with java.lang.String's indexOf() method, there is no restriction on the value of fromIndex: if it exceeds either start or end boundary, it is set to that boundary.
source - the String to search within.substring - the substring to search for.fromIndex - the index to start the search from.
NullPointerException - if source is null
public static boolean characterMatches(boolean ignoreCase,
char c1,
char c2)
public static boolean contains(String s1,
String s2,
boolean caseSensitive)
public static boolean matches(String s1,
String s2,
boolean caseSensitive)
public static String getAlphaByNumber(int n)
public static String localize(String messageId)
messageId - the unique message identifier.
MessageHandler.localize(String)
public static String localize(String messageId,
String arg1)
messageId - The unique message identifier.arg1 - A parameter String to be inserted at %1.
public static String localize(String messageId,
String arg1,
String arg2)
messageId - The unique message identifier.arg1 - A parameter String to be inserted at %1.arg2 - A parameter String to be inserted at %2.
public static String localize(String messageId,
String arg1,
String arg2,
String arg3)
messageId - The unique message identifier.arg1 - A parameter String to be inserted at %1.arg2 - A parameter String to be inserted at %2.arg3 - A parameter String to be inserted at %3.
public static String localize(String messageId,
String arg1,
String arg2,
String arg3,
String arg4)
messageId - The unique message identifier.arg1 - A parameter String to be inserted at %1.arg2 - A parameter String to be inserted at %2.arg3 - A parameter String to be inserted at %3.arg4 - A parameter String to be inserted at %4.
public static String localize(String messageId,
String arg1,
String arg2,
String arg3,
String arg4,
String arg5)
messageId - The unique message identifier.arg1 - A parameter String to be inserted at %1.arg2 - A parameter String to be inserted at %2.arg3 - A parameter String to be inserted at %3.arg4 - A parameter String to be inserted at %4.arg5 - A parameter String to be inserted at %5.
public static String localize(String messageId,
String arg1,
String arg2,
String arg3,
String arg4,
String arg5,
String arg6)
messageId - The unique message identifier.arg1 - A parameter String to be inserted at %1.arg2 - A parameter String to be inserted at %2.arg3 - A parameter String to be inserted at %3.arg4 - A parameter String to be inserted at %4.arg5 - A parameter String to be inserted at %5.arg6 - A parameter String to be inserted at %6.
public static final String sub(String src,
String srch,
String repl,
boolean ignoreCase)
srch in String
src by replacement repl.
If the pattern isn't found, the String is returned unchanged.
src - any String.srch - the String to search for.repl - the replacement String.ignoreCase - a boolean determining case sensitivity of matches.
public static String convertTildeEncoding(String s)
public static final String gsub(String src,
String srch,
String repl,
boolean ignoreCase)
srch
in String src by replacement repl. If the
pattern isn't found, the String is returned unchanged. A null source
String returns null.
src - any String.srch - the String to search for.repl - the replacement String.ignoreCase - a boolean determining case sensitivity of matches.
public static final String oldGsub(String src,
String srch,
String repl,
boolean ignoreCase)
srch in String
src by replacement repl. If the pattern isn't found,
the String is returned unchanged.
src - any String.srch - the String to search for.repl - the replacement String.ignoreCase - a boolean determining case sensitivity of matches.
public static int charCount(String s,
char c)
public static String getStackTraceAsString(Throwable t)
public static String processMessage(String id,
Object[] args)
id
as key, then substitute arguments into (%1, %2, %3, etc.) placeholders in text.
This uses the org.ceryle.util.Msg ResourceBundle if the ID is null or
starts with an underscore character, org.ceryle.util.Messages otherwise.
If the inferred ResourceBundle is not found the method fails silently, simply
returning the unprocessed ID String.
public static String processMessage(String id,
Object[] args,
ResourceBundle bundle)
id as
key, then substitute arguments into (%1, %2, %3, etc.) placeholders in text.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||