Gridarta Editor
WrappingStringBuilder.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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 General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils;
21 
27 public class WrappingStringBuilder {
28 
32  private final StringBuilder sb = new StringBuilder();
33 
37  private final int maxLineLength;
38 
42  private boolean firstWord = true;
43 
47  private int thisLineLength;
48 
53  public WrappingStringBuilder(final int maxLineLength) {
54  this.maxLineLength = maxLineLength;
55  }
56 
61  public void append(final String str) {
62  if (!firstWord) {
63  if (thisLineLength + str.length() + 1 > maxLineLength) {
64  sb.append(",\n");
65  thisLineLength = 0;
66  } else {
67  sb.append(", ");
68  thisLineLength += 2;
69  }
70  }
71  sb.append(str);
72  thisLineLength += str.length();
73  firstWord = false;
74  }
75 
80  public void append(final int value) {
81  append(Integer.toString(value));
82  }
83 
88  @Override
89  public String toString() {
90  return sb.toString();
91  }
92 
93 }
boolean firstWord
Set if no word was added yet, unset if at least one word was added.
final int maxLineLength
The maximum line length.
void append(final int value)
Append an integer value.
int thisLineLength
The length of the last line in sb.
void append(final String str)
Append a word.
WrappingStringBuilder(final int maxLineLength)
Create a new instance.
final StringBuilder sb
The StringBuilder holding the string data.
String toString()
Return the concatenated words as a string.
Implements a string buffer that separates words by "," and wraps lines at a given margin...