All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.archive.util.PaddingStringBuffer Maven / Gradle / Ivy

Go to download

The Archive Commons Code Libraries project contains general Java utility libraries, as used by the Heritrix crawler and other projects.

The newest version!
/*
 *  This file is part of the Heritrix web crawler (crawler.archive.org).
 *
 *  Licensed to the Internet Archive (IA) by one or more individual 
 *  contributors. 
 *
 *  The IA licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.archive.util;

/**
 * StringBuffer-like utility which can add spaces to reach a certain column.  It
 * allows you to append {@link String}, long and ints
 * to the buffer.
 * 

* Note: This class counts from 1, not 0. *

* It uses a StringBuffer behind the scenes. *

* To write a string with multiple lines, it is advisible to use the * {@link #newline() newline()} function. Regular appending of strings with * newlines (\n) character should be safe though. Right appending of strings * with such characters is not safe. * * @author Gordon Mohr */ public final class PaddingStringBuffer { // The buffer. protected StringBuffer buffer; // Location in current line protected int linePos; /** * Create a new PaddingStringBuffer * */ public PaddingStringBuffer() { buffer = new StringBuffer(); linePos=0; } /** append a string directly to the buffer * @param string the string to append * @return This wrapped buffer w/ the passed string appended. */ public PaddingStringBuffer append(String string) { buffer.append(string); if ( string.indexOf('\n') == -1 ){ linePos+=string.length(); } else { while ( string.indexOf('\n') == -1 ){ string = string.substring(string.indexOf('\n')); } linePos=string.length(); } return this; } /** * Append a string, right-aligned to the given columm. If the buffer * length is already greater than the column specified, it simply appends * the string * * @param col the column to right-align to * @param string the string, must not contain multiple lines. * @return This wrapped buffer w/ append string, right-aligned to the * given column. */ public PaddingStringBuffer raAppend(int col, String string) { padTo(col-string.length()); append(string); return this; } /** Pad to a given column. If the buffer size is already greater than the * column, nothing is done. * @param col * @return The buffer padded to i. */ public PaddingStringBuffer padTo(int col) { while(linePosint to the buffer. * @param i the int to append * @return This wrapped buffer with i appended. */ public PaddingStringBuffer append(int i) { append(Integer.toString(i)); return this; } /** * Append an int right-aligned to the given column. If the * buffer length is already greater than the column specified, it simply * appends the int. * * @param col the column to right-align to * @param i the int to append * @return This wrapped buffer w/ appended int, right-aligned to the * given column. */ public PaddingStringBuffer raAppend(int col, int i) { return raAppend(col,Integer.toString(i)); } /** append a long to the buffer. * @param lo the long to append * @return This wrapped buffer w/ appended long. */ public PaddingStringBuffer append(long lo) { append(Long.toString(lo)); return this; } /**Append a long, right-aligned to the given column. If the * buffer length is already greater than the column specified, it simply * appends the long. * @param col the column to right-align to * @param lo the long to append * @return This wrapped buffer w/ appended long, right-aligned to the * given column. */ public PaddingStringBuffer raAppend(int col, long lo) { return raAppend(col,Long.toString(lo)); } /** reset the buffer back to empty */ public void reset() { buffer = new StringBuffer(); linePos = 0; } /* (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { return buffer.toString(); } /** * Forces a new line in the buffer. */ public PaddingStringBuffer newline() { buffer.append("\n"); linePos = 0; return this; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy