Class Utility

java.lang.Object
  |
  +--Utility

public abstract class Utility
extends java.lang.Object

Class with assorted utility functions. It's declared as abstract to ensure that nobody confusedly tries to instantiate any objects in it.


Field Summary
(package private) static javax.swing.JFileChooser fc
          We keep a file chooser sitting around to preserve the user's preferences.
(package private) static java.awt.Frame myFrame
          frame to be used as a default parent where needed.
 
Constructor Summary
Utility()
           
 
Method Summary
static void fileSave(java.lang.String defaultFileName, java.lang.String contents)
          Save a string into a file.
(package private) static boolean processFile(java.lang.String fileName, LineProcessor lp)
          Process a file, with callbacks for each line.
(package private) static java.lang.String removeTrailing(java.lang.String longString, java.lang.String trash)
          Remove trailing characters from a string.
(package private) static java.lang.String replace(java.lang.String longString, java.lang.String delimiter, java.lang.String replaceString)
          Within a long string, replace a single occurence of one substring with another.
static void setClipboard(java.lang.String s)
          Put a string on the system clipboard
static void setFrame(java.awt.Frame f)
          Set the default parent frame for popup windows.
(package private) static java.lang.String stringPiece(java.lang.String longString, java.lang.String delimiter, int piece)
          Extract a delimited substring from a string.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fc

static javax.swing.JFileChooser fc
We keep a file chooser sitting around to preserve the user's preferences.

myFrame

static java.awt.Frame myFrame
frame to be used as a default parent where needed.
See Also:
setFrame(java.awt.Frame)
Constructor Detail

Utility

public Utility()
Method Detail

fileSave

public static void fileSave(java.lang.String defaultFileName,
                            java.lang.String contents)
Save a string into a file.
Parameters:
defaultFileName - file name intended to be could be displayed to the user as a default. Not used in this implementation due to lack of time and know-how.
contents - a string to be written. If the string is delimited by newlines, each line will be written separately. This means that the file will contain the proper line breaks for the local operating system.

processFile

static boolean processFile(java.lang.String fileName,
                           LineProcessor lp)
Process a file, with callbacks for each line.
Parameters:
fileName - the name of the file to be processed
lp - an object with methods to handle lines and exceptions
Returns:
true if the file was successfully processed; false if either there was an IO exception or the line processor indicated that it could not process a line

removeTrailing

static java.lang.String removeTrailing(java.lang.String longString,
                                       java.lang.String trash)
Remove trailing characters from a string.
Parameters:
longString - the string on which we're operating
trash - a collection of characters to be removed from the end
Returns:
the original string, minus the longest trailing substring containing only characters which appear in trash.

replace

static java.lang.String replace(java.lang.String longString,
                                java.lang.String delimiter,
                                java.lang.String replaceString)
Within a long string, replace a single occurence of one substring with another.
Parameters:
longString - the string on which we're operating
delimiter - the substring to be replaced
replaceString - the new substring
Returns:
the original string with the first occurence of the delimiter string replaced by the new string. If the delimiter string never appears, the original string is returned.

setClipboard

public static void setClipboard(java.lang.String s)
Put a string on the system clipboard
Parameters:
s - the string to be placed on the clipboard

setFrame

public static void setFrame(java.awt.Frame f)
Set the default parent frame for popup windows. Some Swing calls (such as JFileChooser.showSaveDialog to create windows require (and others prefer) a parent frame to be passed in. This call sets a private variable to be used as the parent in subsequent Swing calls.

stringPiece

static java.lang.String stringPiece(java.lang.String longString,
                                    java.lang.String delimiter,
                                    int piece)
Extract a delimited substring from a string. Sort of a static version of a StringTokenizer. My background always had this available, so I've come to think this way.
Parameters:
longString - the original string, containing delimiters
delimiter - character(s) which break up the original string into pieces. Unlike StringTokenizer, if this is longer than one character, the entire sequence is treated as a single delimiter, rather than having the string represent a collection of one-character alternative delimiters.
piece - which piece is wanted, counting from 1
Returns:
the selected delimited piece from the original string. if piece is less than 1 or greater than the number of pieces in the original string, an empty string is returned.