TextKit.java

Download TextKit.java

 1: /* TextKit.java - a utility class for text methods.  This class is part
 2:  * of the package "utils".  Currently it contains a method called
 3:  * "lineOfStars" which returns a line of asterisks as a String,
 4:  * and a method "pad" which formats number by adding leading space.
 5:  * ("pad" is no longer useful, since Java5 Formatter class and
 6:  * String.printf methods!)
 7:  *
 8:  * It may contain additional methods someday.  Also, a "main" method
 9:  * has been added to test this class.  As it is part of a package,
10:  * you must run it from the parent directory:  java utils/TextKit
11:  * (Note the forward slash, even on a Windows computer!)
12:  *
13:  * To generate the Javadocs, create utils\doc directory, then run
14:  * the following command (using a single line!):
15:  * javadoc -d utils\docs -author -version -noqualifier java.* -link
16:  *    http://java.sun.com/j2se/1.5/docs/api utils\TextKit.java
17:  *
18:  * (C) 2004 Wayne Pollock, Tampa FL USA.  All Rights Reserved.
19:  */
20: 
21: package utils;
22: 
23: /** TextKit is a collection of utility text methods.
24:  *
25:  * @author Wayne Pollock, Tampa Florida USA
26:  * @version 1.0
27:  */
28: 
29: public class TextKit
30: {
31:    /**
32:     * Creates a line of asterisks of a given length.
33:     *
34:     * @param numOfStars  The number of asterisks to be drawn
35:     * @return A <tt>String</tt> of asterisks
36:     * @throws IllegalArgumentException if <tt>num</tt> &lt; 0
37:     */
38: 
39:    public static String lineOfStars ( int numOfStars )
40:                         throws IllegalArgumentException
41:    {
42:       if ( numOfStars < 0 )
43:          throw new IllegalArgumentException( "Number of stars to draw is "
44:             + numOfStars + ", and should be greater than zero." );
45: 
46:       StringBuilder stars = new StringBuilder( numOfStars );
47:       for ( int i = 0; i < numOfStars; ++i )
48:          stars.append( "*" );
49:       return stars.toString();
50:    }
51: 
52: 
53:    /**
54:     * Formats an integer <tt>num</tt> with leading space,
55:     * so the resulting <tt>String</tt> has at least <tt>minWidth</tt>
56:     * length.
57:     *
58:     * @param num  The number to be formatted with leading spaces
59:     * @param minWidth  The minimum length of the resulting <tt>String</tt>
60:     * @return The formatted <tt>num</tt> with leading spaces
61:     * @throws IllegalArgumentException if <tt>minWidth</tt> &lt; 1
62:     */
63: 
64:    public static String pad ( int num, int minWidth )
65:                         throws IllegalArgumentException
66:    {
67:       if ( minWidth < 0 )
68:          throw new IllegalArgumentException( "Minimum width is "
69:             + minWidth + ", and should be greater than zero." );
70: 
71:       StringBuilder result = new StringBuilder();
72:       result.append( num );
73:       while ( result.length() < minWidth )
74:          result.insert( 0, ' ' );
75:       return result.toString();
76:    }
77: 
78:    /** This prevents one from creating objects of this class.
79:     */
80:    private TextKit () {}
81: 
82: 
83:    /** Main is only used to test the methods.  This is an "ad-hoc"
84:     *  method of testing.  A production quality class would use
85:     *  assertions and/or JUnit testing.
86:     */
87: 
88:    public static void main ( String [] args )
89:    {
90:       System.out.println( lineOfStars(5) );
91:       System.out.println( "Five stars should be showing above" );
92: 
93:       System.out.println( "\n*" + pad(17, 4) + "*" );
94:       System.out.println( "*  17* (two leading spaces) should be showing above" );
95:    }
96: }