Java tutorial
/* * Copyright (c) 2004-2015 YAMJ Members * https://github.com/organizations/YAMJ/teams * * This file is part of the Yet Another Media Jukebox (YAMJ). * * YAMJ is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * YAMJ is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with YAMJ. If not, see <http://www.gnu.org/licenses/>. * * Web: https://github.com/YAMJ/yamj-v3 * */ package org.yamj.common.tools; import java.text.BreakIterator; import java.util.*; import org.apache.commons.lang3.StringUtils; public class StringTools { private StringTools() { throw new UnsupportedOperationException("Utility class"); } /** * Split a list using a regex and return a list of trimmed strings * * @param stringToSplit * @param regexDelim * @return */ public static List<String> splitList(String stringToSplit, String regexDelim) { List<String> finalValues = new ArrayList<>(); for (String output : stringToSplit.split(regexDelim)) { finalValues.add(output.trim()); } return finalValues; } /** * Cast a generic list to a specfic class See: * http://stackoverflow.com/questions/367626/how-do-i-fix-the-expression-of-type-list-needs-unchecked-conversion * * @param <T> * @param objClass * @param c * @return */ public static <T> List<T> castList(Class<? extends T> objClass, Collection<?> c) { List<T> r = new ArrayList<>(c.size()); for (Object o : c) { r.add(objClass.cast(o)); } return r; } public static Collection<String> tokenize(String sourceString, String delimiter) { StringTokenizer st = new StringTokenizer(sourceString, delimiter); Collection<String> keywords = new HashSet<>(); while (st.hasMoreTokens()) { keywords.add(st.nextToken()); } return keywords; } /** * Check that the passed string is not longer than the required length and * trim it if necessary. * * @param sourceString * @param requiredLength * @return */ public static String trimToLength(String sourceString, int requiredLength) { return trimToLength(sourceString, requiredLength, Boolean.TRUE, "..."); } /** * Check that the passed string is not longer than the required length and * trim it if necessary * * @param sourceString The string to check * @param requiredLength The required length (Maximum) * @param trimToWord Trim the source string to the last space to avoid * partial words * @param endingSuffix The ending to append if the string is longer than the * required length * @return */ public static String trimToLength(String sourceString, int requiredLength, boolean trimToWord, String endingSuffix) { String changedString = sourceString.trim(); if (StringUtils.isNotBlank(changedString)) { if (changedString.length() <= requiredLength) { // No need to do anything return changedString; } else if (trimToWord) { BreakIterator bi = BreakIterator.getWordInstance(); bi.setText(changedString); int biLength = bi.preceding(requiredLength - endingSuffix.length()); return changedString.substring(0, biLength).trim() + endingSuffix; } else { // We know that the source string is longer that the required length, so trim it to size return changedString.substring(0, requiredLength - endingSuffix.length()).trim() + endingSuffix; } } return changedString; } }