de.danielbechler.util.Strings Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-object-diff Show documentation
Show all versions of java-object-diff Show documentation
Framework to detect and handle differences between Java objects
/*
* Copyright 2012 Daniel Bechler
*
* Licensed 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 de.danielbechler.util;
import java.util.*;
import java.util.regex.*;
/** @author Daniel Bechler */
public class Strings
{
private static final Pattern LINE_BREAK_PATTERN = Pattern.compile("\\s*\\n\\s*");
private Strings()
{
}
@SuppressWarnings("SizeReplaceableByIsEmpty") // String.isEmpty() is a Java 1.6 feature
public static boolean hasText(final String s)
{
return s != null && s.trim().length() != 0;
}
public static boolean isEmpty(final String s)
{
return !hasText(s);
}
/**
* Joins all non-null elements of the given elements
into one String.
*
* @param delimiter Inserted as separator between consecutive elements.
* @param elements The elements to join.
*
* @return A long string containing all non-null elements.
*/
public static String join(final String delimiter, final Object... elements)
{
final StringBuilder sb = new StringBuilder();
for (final Object part : elements)
{
if (part == null)
{
continue;
}
if (sb.length() > 0)
{
sb.append(delimiter);
}
sb.append(part.toString());
}
return sb.toString();
}
/**
* Same as {@link #join(String, Object...)} but with a {@link Collection} instead of an Array for the
* elements.
*
* @see #join(String, java.util.Collection)
*/
public static String join(final String delimiter, final Collection> elements)
{
if (elements == null || elements.isEmpty())
{
return "";
}
return join(delimiter, elements.toArray(new Object[elements.size()]));
}
public static String toSingleLineString(final Object object)
{
if (object != null)
{
final String s = object.toString().trim();
final Matcher matcher = LINE_BREAK_PATTERN.matcher(s);
return matcher.replaceAll(" \\\\ ");
}
return null;
}
public static String indent(final int times, final String text)
{
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < times; i++)
{
sb.append(" ");
}
sb.append(text);
return sb.toString();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy