Crossfire JXClient, Trunk  R20561
StringSplitter.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.util;
23 
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import org.jetbrains.annotations.NotNull;
27 
32 public class StringSplitter {
33 
37  private static final int PREFERRED_LINE_LENGTH = 50;
38 
42  private static final int MAXIMUM_LINE_LENGTH = 80;
43 
47  private StringSplitter() {
48  }
49 
56  @NotNull
57  public static String splitAsHtml(@NotNull final String message) {
58  final StringBuilder sb = new StringBuilder();
59  for (final String line : split(message)) {
60  if (sb.length() > 0) {
61  sb.append("<br>");
62  }
63  sb.append(line);
64  }
65  return sb.toString();
66  }
67 
73  @NotNull
74  private static Iterable<String> split(@NotNull final String message) {
75  final String paddedMessage = message.trim()+" ";
76 
77  final Collection<String> result = new ArrayList<>();
78  int start = 0;
79  while (true) {
80  while (start < paddedMessage.length() && paddedMessage.charAt(start) == ' ') {
81  start++;
82  }
83  if (start >= paddedMessage.length()) {
84  break;
85  }
86 
87  final int nextSpace = paddedMessage.indexOf(' ', Math.min(start+PREFERRED_LINE_LENGTH, paddedMessage.length()-1));
88  assert nextSpace != -1;
89  if (nextSpace-start <= PREFERRED_LINE_LENGTH) {
90  result.add(paddedMessage.substring(start, nextSpace));
91  start = nextSpace+1;
92  } else {
93  final int prevSpace = paddedMessage.lastIndexOf(' ', nextSpace-1);
94  if (prevSpace != -1 && prevSpace > start) {
95  result.add(paddedMessage.substring(start, prevSpace));
96  start = prevSpace+1;
97  } else if (nextSpace-start <= MAXIMUM_LINE_LENGTH) {
98  result.add(paddedMessage.substring(start, nextSpace));
99  start = nextSpace+1;
100  } else {
101  final int end = Math.min(start+MAXIMUM_LINE_LENGTH, paddedMessage.length());
102  result.add(paddedMessage.substring(start, end));
103  start = end;
104  }
105  }
106  }
107  return result;
108  }
109 
110 }
static String splitAsHtml(@NotNull final String message)
Splits the given string into lines and returns the lines separated by "&lt;br&gt;".
static Iterable< String > split(@NotNull final String message)
Splits the given string into lines.
StringSplitter()
Private constructor to prevent instantiation.
static final int MAXIMUM_LINE_LENGTH
The maximum line length in characters.
Utility class for splitting strings.
static final int PREFERRED_LINE_LENGTH
The preferred line length in characters.