com.blackperl.Perl
Class Chop

java.lang.Object
  |
  +--com.blackperl.Perl.Chop

public final class Chop
extends java.lang.Object

Static-method implementation of the Perl chop keyword. chop removes the last character off of a string (or in this case, a StringBuffer). The return value is the character that was removed. If an array of StringBuffer objects are given as the argument, all are operated on but only the character from the very last one is returned.

Here is a simple example, based on creating one string from an array of strings with a given character separator (a task better and more efficiently done by Join, but useful as an example):

     import com.blackperl.Perl.Chop;
 
     StringBuffer buffer = new StringBuffer();
     for (int index; index < stringlist.length; index++)
     {
         buffer.append(stringlist[index]).append(',');
     }
     // Use chop to remove the extra comma at the end:
     Chop.chop(buffer);
 

To keep with the Perl idiom this emulates, the argument may also be an object that implements the Map interface. If this is the case, all the values (not keys) must be StringBuffer objects. All will be chopped, just as with the array-form above (which also means that the return value is the character from the last chop). This is the only form of chop that might throw a significant exception, in the case where the value in a Map entry cannot be cast to StringBuffer.

All components in this package provide an instance method to retrieve a singleton object which may be used to call the static methods, if the programmer prefers using an object to static invocation.

If this is the JDK 1.5 ("Tiger") edition of the package, this class is suitable for use via static import:

     import com.blackperl.Perl.Chop.chop;
 
     StringBuffer buffer = new StringBuffer();
     for (int index; index < stringlist.length; index++)
     {
         buffer.append(stringlist[index]).append(',');
     }
     // Use chop to remove the extra comma at the end:
     chop(buffer);
 


Method Summary
static char chop(java.util.Map table)
          Chop all the StringBuffer objects that are the values of the Map object passed in.
static char chop(java.lang.StringBuffer buffer)
          Chop the trailing character off of the StringBuffer object passed in.
static char chop(java.lang.StringBuffer[] buffers)
          Chop all the StringBuffer objects in the array.
static com.blackperl.Perl.Chop instance()
          The instance method is used to retrieve the Chop singleton that applications can use in lieu of invoking the methods statically.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

chop

public static char chop(java.util.Map table)
                 throws java.lang.ClassCastException
Chop all the StringBuffer objects that are the values of the Map object passed in. Throws an exception if any of them cannot be cast to StringBuffer.

Parameters:
table - An object implementing the Map interface
Returns:
The character from the last chop().
Throws:
java.lang.ClassCastException

chop

public static char chop(java.lang.StringBuffer buffer)
Chop the trailing character off of the StringBuffer object passed in.

Parameters:
buffer - StringBuffer to trim from
Returns:
The character removed (or '\0' if buffer is empty)

chop

public static char chop(java.lang.StringBuffer[] buffers)
Chop all the StringBuffer objects in the array.

Parameters:
buffers - Array of StringBuffer to operate on
Returns:
The character from the last chop()

instance

public static com.blackperl.Perl.Chop instance()
The instance method is used to retrieve the Chop singleton that applications can use in lieu of invoking the methods statically.

Returns:
Singleton instance of the Chomp class