001package ezvcard.util; 002 003/* 004 Copyright (c) 2012-2023, Michael Angstadt 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without 008 modification, are permitted provided that the following conditions are met: 009 010 1. Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 2. Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 020 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 026 027 The views and conclusions contained in the software and documentation are those 028 of the authors and should not be interpreted as representing official policies, 029 either expressed or implied, of the FreeBSD Project. 030 */ 031 032/** 033 * Wraps a {@link StringBuilder} object, providing utility methods for clearing 034 * it. 035 */ 036public class ClearableStringBuilder { 037 private final StringBuilder sb = new StringBuilder(); 038 039 /** 040 * Clears the buffer. 041 * @return this 042 */ 043 public ClearableStringBuilder clear() { 044 sb.setLength(0); 045 return this; 046 } 047 048 /** 049 * Gets the buffer's contents. 050 * @return the buffer's contents 051 */ 052 public String get() { 053 return sb.toString(); 054 } 055 056 /** 057 * Gets the buffer's contents, then clears it. 058 * @return the buffer's contents 059 */ 060 public String getAndClear() { 061 String string = get(); 062 clear(); 063 return string; 064 } 065 066 /** 067 * Appends a character to the buffer. 068 * @param ch the character to append 069 * @return this 070 */ 071 public ClearableStringBuilder append(char ch) { 072 sb.append(ch); 073 return this; 074 } 075 076 /** 077 * Appends a character sequence to the buffer. 078 * @param string the character sequence to append 079 * @return this 080 */ 081 public ClearableStringBuilder append(CharSequence string) { 082 sb.append(string); 083 return this; 084 } 085 086 /** 087 * Appends a character array to the buffer. 088 * @param buffer the characters to append 089 * @param start the index of the first char to append 090 * @param length the number of chars to append 091 * @return this 092 */ 093 public ClearableStringBuilder append(char[] buffer, int start, int length) { 094 sb.append(buffer, start, length); 095 return this; 096 } 097 098 /** 099 * Removes the last character from the buffer. 100 * @return this 101 */ 102 public ClearableStringBuilder chop() { 103 sb.setLength(sb.length() - 1); 104 return this; 105 } 106 107 /** 108 * Gets the length of the buffer. 109 * @return the buffer's length 110 */ 111 public int length() { 112 return sb.length(); 113 } 114}