All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squeakysand.commons.lang.StringUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
 *
 * 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 com.squeakysand.commons.lang;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Provides common String related functionality.
 */
public final class StringUtils {

    private static final Logger LOG = LoggerFactory.getLogger(StringUtils.class);

    /**
     * 

* crop. *

* * @param s * a {@link java.lang.String} object. * @param maxLength * a int. * @return a {@link java.lang.String} object. */ public static String crop(String s, int maxLength) { return crop(s, maxLength, null); } /** *

* crop. *

* * @param s * a {@link java.lang.String} object. * @param suffix * a {@link java.lang.String} object. * @param maxLength * a int. * @return a {@link java.lang.String} object. */ public static String crop(String s, int maxLength, String suffix) { StringBuilder sb = new StringBuilder(); if (isNotNullOrEmpty(s)) { sb.append(s.trim()); if (sb.length() > maxLength) { sb.append(sb.substring(0, sb.length() - suffix.length())); sb.append(suffix); } } return sb.toString(); } /** * DOCUMENT ME! * * @param letters * DOCUMENT ME! * @return DOCUMENT ME! */ public static SortedSet generateAnagrams(String letters) { return generateAnagrams("", letters); } public static boolean isEmpty(CharSequence charSequence) { boolean result = false; if (charSequence == null) { throw new NullParameterException("charSequence"); } else { String stringRepresentation = charSequence.toString(); result = stringRepresentation.trim().length() < 1; } return result; } public static boolean isNotNullOrEmpty(CharSequence charSequence) { boolean result = false; if (charSequence != null) { String stringRepresentation = charSequence.toString(); if (stringRepresentation.trim().length() > 0) { result = true; } } return result; } /** * Tests if the String argument is null, an empty (zero-length) String, or contains only whitespace. * * @param charSequence * the String to test. * @return true if the String is null, an empty (zero-length) String, or contains only * whitespace, false otherwise. */ public static boolean isNullOrEmpty(CharSequence charSequence) { boolean result = true; if (charSequence != null) { String stringRepresentation = charSequence.toString(); if (stringRepresentation.trim().length() > 0) { result = false; } } return result; } /** *

* pad. *

* * @param s * a {@link java.lang.String} object. * @param targetLength * a int. * @param pad * a char. * @return a {@link java.lang.String} object. */ public static String pad(String s, int targetLength, char pad) { throw new RuntimeException(); } public static List split(String source, String delimiter) { List result = new ArrayList(); StringTokenizer st = new StringTokenizer(source, delimiter); while (st.hasMoreTokens()) { result.add(st.nextToken()); } return result; } public static String trim(String source) { String result = ""; if (source != null) { result = source.trim(); } return result; } public static String trimPrefix(String source, char c, TrimStrategy strategy) { String result = source; if (source == null) { throw new NullParameterException("source"); } if (strategy == null) { throw new NullParameterException("strategy"); } int index = source.indexOf(c); if (index != -1) { switch (strategy) { case AT_FIRST: index = source.indexOf(c); break; case AFTER_FIRST: index = source.indexOf(c) + 1; break; case AT_LAST: index = source.lastIndexOf(c); break; case AFTER_LAST: index = source.lastIndexOf(c) + 1; break; } if (index != -1) { result = trimPrefix(source, index); } } return result; } public static String trimPrefix(String source, int prefixLength) { if (source == null) { throw new NullParameterException("source"); } if (prefixLength < 0) { throw new IllegalArgumentException(String.format("prefixLength must be >= 0, but was %s", prefixLength)); } if (prefixLength > source.length()) { throw new IllegalArgumentException(String.format("prefixLength must be <= source.length(), prefixLength was %s, source.length() was %s", prefixLength, source.length())); } return source.substring(prefixLength); } public static String trimPrefix(String source, String prefix) { if (source == null) { throw new NullParameterException("source"); } if (prefix == null) { throw new NullParameterException("prefix"); } int sourceLength = source.length(); int prefixLength = prefix.length(); if (prefixLength > sourceLength) { throw new IllegalArgumentException(String.format("prefix.length() must be <= source.length(), prefix.length() was %s, source.length() was %s", prefixLength, sourceLength)); } String result = null; if (source.startsWith(prefix)) { result = trimPrefix(source, prefix.length()); } else { result = source; } return result; } public static String trimSuffix(String source, char c, TrimStrategy strategy) { String result = source; if (source == null) { throw new NullParameterException("source"); } if (strategy == null) { throw new NullParameterException("strategy"); } int index = source.indexOf(c); if (index != -1) { switch (strategy) { case AT_FIRST: index = source.indexOf(c); break; case AFTER_FIRST: index = source.indexOf(c) + 1; break; case AT_LAST: index = source.lastIndexOf(c); break; case AFTER_LAST: index = source.lastIndexOf(c) + 1; break; } if (index != -1) { int sourceLength = source.length(); result = trimSuffix(source, sourceLength - index); } } return result; } public static String trimSuffix(String source, int suffixLength) { if (source == null) { throw new NullParameterException("source"); } int sourceLength = source.length(); if (suffixLength < 0) { throw new IllegalArgumentException(String.format("suffixLength must be >= 0, but was %s", suffixLength)); } if (suffixLength > sourceLength) { throw new IllegalArgumentException(String.format("suffixLength must be <= source.length(), suffixLength was %s, source.length() was %s", suffixLength, sourceLength)); } int endIndex = sourceLength - suffixLength; return source.substring(0, endIndex); } public static String trimSuffix(String source, String suffix) { if (source == null) { throw new NullParameterException("source"); } if (suffix == null) { throw new NullParameterException(("suffix")); } int sourceLength = source.length(); int suffixLength = suffix.length(); if (suffixLength > sourceLength) { throw new IllegalArgumentException(String.format("suffix.length() must be <= source.length(), suffix.length() was %s, source.length() was %s", suffixLength, sourceLength)); } String result = null; if (source.endsWith(suffix)) { result = trimSuffix(source, suffix.length()); } else { result = source; } return result; } private static SortedSet generateAnagrams(String prefix, String letters) { SortedSet anagrams = new TreeSet(); for (int i = 0; i < letters.length(); i++) { String newPrefix = prefix + letters.charAt(i); anagrams.add(newPrefix); String newLetters = ""; for (int j = 0; j < letters.length(); j++) { if (j != i) { newLetters += letters.charAt(j); } } Set newAnagrams = generateAnagrams(newPrefix, newLetters); anagrams.addAll(newAnagrams); } return anagrams; } /** * Utility class, so we enforce singleton pattern by hiding default constructor. */ private StringUtils() { } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy