KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > PaddingStringBuffer


1 /* Copyright (C) 2003 Internet Archive.
2  *
3  * This file is part of the Heritrix web crawler (crawler.archive.org).
4  *
5  * Heritrix is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser Public License as published by
7  * the Free Software Foundation; either version 2.1 of the License, or
8  * any later version.
9  *
10  * Heritrix is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser Public License
16  * along with Heritrix; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * PaddingStringBuffer.java
20  * Created on Oct 23, 2003
21  *
22  * $Header: /cvsroot/archive-crawler/ArchiveOpenCrawler/src/java/org/archive/util/PaddingStringBuffer.java,v 1.7 2004/08/06 21:05:54 stack-sf Exp $
23  */

24 package org.archive.util;
25
26 /**
27  * StringBuffer-like utility which can add spaces to reach a certain column. It
28  * allows you to append {@link String}, <code>long</code> and <code>int</code>s
29  * to the buffer.
30  * <p>
31  * Note: This class counts from 1, not 0.
32  * <p>
33  * It uses a StringBuffer behind the scenes.
34  * <p>
35  * To write a string with multiple lines, it is advisible to use the
36  * {@link #newline() newline()} function. Regular appending of strings with
37  * newlines (\n) character should be safe though. Right appending of strings
38  * with such characters is <i>not</i> safe.
39  *
40  * @author Gordon Mohr
41  */

42 public final class PaddingStringBuffer {
43     // The buffer.
44
StringBuffer JavaDoc buffer;
45     // Location in current line
46
int linePos;
47
48     /**
49      * Create a new PaddingStringBuffer
50      *
51      */

52     public PaddingStringBuffer() {
53         buffer = new StringBuffer JavaDoc();
54         linePos=0;
55     }
56
57     /** append a string directly to the buffer
58      * @param string the string to append
59      * @return This wrapped buffer w/ the passed string appended.
60      */

61     public PaddingStringBuffer append(String JavaDoc string) {
62         buffer.append(string);
63         if ( string.indexOf('\n') == -1 ){
64             linePos+=string.length();
65         } else {
66             while ( string.indexOf('\n') == -1 ){
67                 string = string.substring(string.indexOf('\n'));
68             }
69             linePos=string.length();
70         }
71         return this;
72     }
73
74     /**
75      * Append a string, right-aligned to the given columm. If the buffer
76      * length is already greater than the column specified, it simply appends
77      * the string
78      *
79      * @param col the column to right-align to
80      * @param string the string, must not contain multiple lines.
81      * @return This wrapped buffer w/ append string, right-aligned to the
82      * given column.
83      */

84     public PaddingStringBuffer raAppend(int col, String JavaDoc string) {
85         padTo(col-string.length());
86         append(string);
87         return this;
88     }
89
90     /** Pad to a given column. If the buffer size is already greater than the
91      * column, nothing is done.
92      * @param col
93      * @return The buffer padded to <code>i</code>.
94      */

95     public PaddingStringBuffer padTo(int col) {
96         while(linePos<col) {
97             buffer.append(" ");
98             linePos++;
99         }
100         return this;
101     }
102
103     /** append an <code>int</code> to the buffer.
104      * @param i the int to append
105      * @return This wrapped buffer with <code>i</code> appended.
106      */

107     public PaddingStringBuffer append(int i) {
108         append(Integer.toString(i));
109         return this;
110     }
111
112
113     /**
114      * Append an <code>int</code> right-aligned to the given column. If the
115      * buffer length is already greater than the column specified, it simply
116      * appends the <code>int</code>.
117      *
118      * @param col the column to right-align to
119      * @param i the int to append
120      * @return This wrapped buffer w/ appended int, right-aligned to the
121      * given column.
122      */

123     public PaddingStringBuffer raAppend(int col, int i) {
124         return raAppend(col,Integer.toString(i));
125     }
126
127     /** append a <code>long</code> to the buffer.
128      * @param lo the <code>long</code> to append
129      * @return This wrapped buffer w/ appended long.
130      */

131     public PaddingStringBuffer append(long lo) {
132         append(Long.toString(lo));
133         return this;
134     }
135
136     /**Append a <code>long</code>, right-aligned to the given column. If the
137      * buffer length is already greater than the column specified, it simply
138      * appends the <code>long</code>.
139      * @param col the column to right-align to
140      * @param lo the long to append
141      * @return This wrapped buffer w/ appended long, right-aligned to the
142      * given column.
143      */

144     public PaddingStringBuffer raAppend(int col, long lo) {
145         return raAppend(col,Long.toString(lo));
146     }
147
148     /** reset the buffer back to empty */
149     public void reset() {
150         buffer = new StringBuffer JavaDoc();
151         linePos = 0;
152     }
153
154     /* (non-Javadoc)
155      * @see java.lang.Object#toString()
156      */

157     public String JavaDoc toString() {
158         return buffer.toString();
159     }
160
161     /**
162      * Forces a new line in the buffer.
163      */

164     public PaddingStringBuffer newline() {
165         buffer.append("\n");
166         linePos = 0;
167         return this;
168     }
169
170 }
171
Popular Tags