dk.netarkivet.common.utils
Class StringUtils

java.lang.Object
  extended by dk.netarkivet.common.utils.StringUtils

public final class StringUtils
extends java.lang.Object

Utilities for working with strings.


Method Summary
static
<T> java.lang.String
conjoin(java.lang.String sep, java.util.Collection<T> objects)
          Concatenate all objects in a collection with the given separator between each.
static
<T> java.lang.String
conjoin(java.lang.String separator, java.util.Collection<T> objects, int max)
          Concatenate the string representation of a maximum number of objects in a collection with a given separator between them.
static java.lang.String conjoin(java.lang.String sep, java.lang.String... strings)
          Concatenate all strings in a collection with the given separator between each.
static java.lang.String formatDate(long millis)
          Formats the given date (as elapsed milliseconds) using the default format 'yyyy/MM/dd HH:mm:ss'.
static java.lang.String formatDate(long millis, java.lang.String format)
          Formats the given date (as elapsed milliseconds) using the provided format pattern.
static java.lang.String formatDuration(long seconds)
          Formats a duration in seconds as a string of the form "3d 04:12:56".
static java.lang.String formatNumber(double number)
          Formats a number, as a decimal number with at most 2 digits.
static java.lang.String formatNumber(long number)
          Formats a number, as a decimal number with at most 2 digits.
static java.lang.String formatPercentage(double percentage)
          Formats a numeric percentage, as a decimal number with at most 2 digits.
static java.lang.String formatPercentage(long percentage)
          Formats a numeric percentage, as a decimal number with at most 2 digits.
static java.lang.String makeEllipsis(java.lang.String orgString, int maxLength)
          Generate a ellipsis of orgString.
static java.util.List<java.lang.Integer> parseIntList(java.lang.String[] stringArray)
          Change all Strings to Integers.
static java.lang.String repeat(java.lang.String s, int n)
          Repeat the string n times.
static java.lang.String replace(java.lang.String sentence, java.lang.String oldString, java.lang.String newString)
          Replace all occurrences of oldString with newString in a string.
static java.lang.String splitStringForce(java.lang.String input, int maxLineLength)
          Given a multi-line input string, this method splits the string so that no line has length greater than maxLineLength.
static java.lang.String splitStringOnWhitespace(java.lang.String input, int lineLength)
          Given an input String, this method splits the String with newlines into a multiline String with line-lengths approximately lineLength.
static java.lang.String surjoin(java.util.List<java.lang.String> strings, java.lang.String pre, java.lang.String post)
          Concatenate all strings in a collection, with the fixed strings appended and prepended to each.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

replace

public static java.lang.String replace(java.lang.String sentence,
                                       java.lang.String oldString,
                                       java.lang.String newString)
Replace all occurrences of oldString with newString in a string.

Parameters:
sentence - the string, where all occurrences of oldString are to be replaced with newString.
oldString - the oldString.
newString - the newString.
Returns:
the resulting string, where all occurrences of oldString are replaced with newString.

conjoin

public static <T> java.lang.String conjoin(java.lang.String sep,
                                           java.util.Collection<T> objects)
Concatenate all objects in a collection with the given separator between each. If the Collection is a List, this method will generate the conjoined string in list order. If the objects are not Strings, the toString method will be used to convert them to strings.

Type Parameters:
T - The type of objects to conjoin.
Parameters:
sep - A string to separate the list items.
objects - A collection of object to concatenate as a string.
Returns:
The concatenated string, or null if the list was null.

conjoin

public static <T> java.lang.String conjoin(java.lang.String separator,
                                           java.util.Collection<T> objects,
                                           int max)
                                throws ArgumentNotValid
Concatenate the string representation of a maximum number of objects in a collection with a given separator between them. If the Collection is a List, this method will generate the conjoined string in list order. If the objects are not Strings, the toString method will be used to convert them to strings.

Type Parameters:
T - The type of collection.
Parameters:
separator - The string to separate the entries in the collection with. This is allowed to be the empty string.
objects - The collection to have the string representation of its entries concatenated.
max - The maximum number of objects in the collection to concatenate. If this number is 0 or below only the first entry in the collection is returned.
Returns:
The concatenation of the string representation of a limited amount of entries in the collection.
Throws:
ArgumentNotValid - If the separator or the objects are null.

conjoin

public static java.lang.String conjoin(java.lang.String sep,
                                       java.lang.String... strings)
Concatenate all strings in a collection with the given separator between each.

Parameters:
sep - A string to separate the list items.
strings - An array of strings to concatenate.
Returns:
The concatenated string, or null if the list was null.

surjoin

public static java.lang.String surjoin(java.util.List<java.lang.String> strings,
                                       java.lang.String pre,
                                       java.lang.String post)
Concatenate all strings in a collection, with the fixed strings appended and prepended to each.

Parameters:
strings - A list of strings to join up.
pre - A string that will be put in front of each string in the list.
post - A string that will be put after each string in the list.
Returns:
The joined string, or null if strings is null.

repeat

public static java.lang.String repeat(java.lang.String s,
                                      int n)
Repeat the string n times.

Parameters:
s - A string to repeat.
n - How many times to repeat it.
Returns:
A repeated string.
Throws:
ArgumentNotValid - if a negative amount is specified.

parseIntList

public static java.util.List<java.lang.Integer> parseIntList(java.lang.String[] stringArray)
Change all Strings to Integers.

Parameters:
stringArray - the given array of Strings to convert.
Returns:
a List of Integers.

makeEllipsis

public static java.lang.String makeEllipsis(java.lang.String orgString,
                                            int maxLength)
Generate a ellipsis of orgString. If orgString is longer than maxLength, then we return a String containing the first maxLength characters and then append " ..".

Parameters:
orgString - the original string.
maxLength - the maximum length of the string before ellipsing it.
Returns:
an ellipsis of orgString.

formatDuration

public static java.lang.String formatDuration(long seconds)
Formats a duration in seconds as a string of the form "3d 04:12:56".

Parameters:
seconds - A duration in seconds
Returns:
a formatted string of the form "3d 04:12:56"

formatPercentage

public static java.lang.String formatPercentage(double percentage)
Formats a numeric percentage, as a decimal number with at most 2 digits.

Parameters:
percentage - the numeric percentage to format.
Returns:
a formatted percentage string.

formatPercentage

public static java.lang.String formatPercentage(long percentage)
Formats a numeric percentage, as a decimal number with at most 2 digits.

Parameters:
percentage - the numeric percentage to format.
Returns:
a formatted percentage string.

formatNumber

public static java.lang.String formatNumber(double number)
Formats a number, as a decimal number with at most 2 digits.

Parameters:
number - the number to format.
Returns:
a formatted number string.

formatNumber

public static java.lang.String formatNumber(long number)
Formats a number, as a decimal number with at most 2 digits.

Parameters:
number - the number to format.
Returns:
a formatted number string.

formatDate

public static java.lang.String formatDate(long millis)
Formats the given date (as elapsed milliseconds) using the default format 'yyyy/MM/dd HH:mm:ss'.

Parameters:
millis - the date
Returns:
a formatted date string

formatDate

public static java.lang.String formatDate(long millis,
                                          java.lang.String format)
Formats the given date (as elapsed milliseconds) using the provided format pattern.

Parameters:
millis - the date
format - the format pattern SimpleDateFormat
Returns:
a formatted date string

splitStringOnWhitespace

public static java.lang.String splitStringOnWhitespace(java.lang.String input,
                                                       int lineLength)
Given an input String, this method splits the String with newlines into a multiline String with line-lengths approximately lineLength. The split is made at the first blank space found at more than lineLength characters after the previous split.

Parameters:
input - the input String.
lineLength - the desired line length.
Returns:
the split String.
Throws:
ArgumentNotValid - if the input is null or the lineLength is not positive

splitStringForce

public static java.lang.String splitStringForce(java.lang.String input,
                                                int maxLineLength)
Given a multi-line input string, this method splits the string so that no line has length greater than maxLineLength. Any input lines less than or equal to this length remain unaffected.

Parameters:
input - the input String.
maxLineLength - the maximum permitted line length.
Returns:
the split multi-line String.
Throws:
ArgumentNotValid - if input is null or maxLineLength is non-positive