diff --git a/src/fr/devinsy/util/strings/StringListUtils.java b/src/fr/devinsy/util/strings/StringListUtils.java index 67b0a00..2c97490 100644 --- a/src/fr/devinsy/util/strings/StringListUtils.java +++ b/src/fr/devinsy/util/strings/StringListUtils.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2008-2010, 2013-2015 Christian Pierre MOMON + * Copyright (C) 2008-2010, 2013-2016 Christian Pierre MOMON * * This file is part of Devinsy-utils. * @@ -40,782 +40,782 @@ import java.util.Collection; */ public class StringListUtils { - public static final String DEFAULT_CHARSET_NAME = "UTF-8"; - - /** - * - * @param target - * @return - */ - public StringSet getComplement(final Collection alpha, final Collection bravo) - { - StringSet result; - - result = getDifference(bravo, alpha); - - // - return result; - } - - /** - * - * @param target - * @return - */ - public StringSet getDifference(final Collection alpha, final Collection bravo) - { - StringSet result; - - result = new StringSet(alpha); - result.removeAll(bravo); - - // - return result; - } - - /** - * - * @param target - * @return - */ - public StringSet getDisjunction(final Collection alpha, final Collection bravo) - { - StringSet result; - - result = new StringSet(); - - for (String string : alpha) - { - if (!bravo.contains(string)) - { - result.add(string); - } - } - - for (String string : bravo) - { - if (!alpha.contains(string)) - { - result.add(string); - } - } - - // - return result; - } - - /** - * - * @param target - * @return - */ - public StringSet getIntersectionOf(final Collection alpha, final Collection bravo) - { - StringSet result; - - result = new StringSet(); - - for (String string : alpha) - { - if (bravo.contains(string)) - { - result.add(string); - } - } - - // - return result; - } - - /** - * - * @param target - * @return - */ - public StringSet getUnionOf(final Collection alpha, final Collection bravo) - { - StringSet result; - - result = new StringSet(alpha); - - for (String string : bravo) - { - result.add(string); - } - - // - return result; - } - - /** - * Builds a string concatenating many times a source string. - * - * @param source - * The string to concatenate several time. - * - * @param multiplier - * The number of concatenate to produce. - * @return - */ - public static String concatenate(final String source, final int multiplier) - { - String result; - - result = repeat(source, multiplier).toString(); - - // - return result; - } - - /** - * - * @param file - * @throws IOException - */ - public static StringList load(final File source) throws IOException - { - StringList result; - - result = load(source, DEFAULT_CHARSET_NAME); - - // - return result; - } - - /** - * - * @param file - * @throws IOException - */ - public static StringList load(final File file, final String charsetName) throws IOException - { - StringList result; - - // - result = new StringList(); - - // - StringListUtils.read(result, new FileInputStream(file), charsetName); - - // - return result; - } - - /** - * - * @param file - * @return - * @throws IOException - */ - public static StringList load(final URL source) throws IOException - { - StringList result; - - result = load(source, DEFAULT_CHARSET_NAME); - - // - return result; - } - - /** - * - * @param file - * @throws IOException - */ - public static StringList load(final URL source, final String charsetName) throws IOException - { - StringList result; - - // - result = new StringList(); - - // - StringListUtils.read(result, source.openStream(), charsetName); - - // - return result; - } - - /** - * - * @param file - * @throws IOException - */ - public static void read(final StringList out, final InputStream is, final String charsetName) throws IOException - { - BufferedReader in = null; - try - { - in = new BufferedReader(new InputStreamReader(is, charsetName)); - - boolean ended = false; - - while (!ended) - { - String line = in.readLine(); - - if (line == null) - { - ended = true; - } - else - { - out.append(line); - } - } - } - finally - { - try - { - if (in != null) - { - in.close(); - } - } - catch (IOException exception) - { - exception.printStackTrace(); - } - } - } - - /** - * Builds a string list concatenating many times a source string. - * - * @param source - * The string to concatenate several time. - * - * @param multiplier - * The number of concatenate to produce. - * @return - */ - public static StringList repeat(final String source, final int multiplier) - { - StringList result; - - result = new StringList(multiplier); - for (int count = 0; count < multiplier; count++) - { - result.append(source); - } - - // - return result; - } - - /** - * - * @param file - * @throws FileNotFoundException - * @throws UnsupportedEncodingException - */ - public static void save(final File file, final StringList source) throws UnsupportedEncodingException, FileNotFoundException - { - PrintWriter out = null; - try - { - out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), DEFAULT_CHARSET_NAME)); - - if (source != null) - { - for (String string : source) - { - out.println(string); - } - } - } - finally - { - if (out != null) - { - out.close(); - } - } - } - - /** - * Sorts the string list. - * - * @param source - * The string list to sort. - */ - public static void sort(final StringList source) - { - if (source != null) - { - source.sort(); - } - } - - /** - * Concatenates the string from an array to a string. - * - * @param source - * The string array to convert. - * - * @return A string concatenation of the argument. - */ - public static String toString(final String[] source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = new StringList(source).toString(); - } - - // - return result; - } - - /** - * - * @param prefix - * @param separator - * @param postfix - * @return - */ - public static String toString(final String[] source, final String prefix, final String separator, final String postfix) - { - String result; - - if (source == null) - { - result = new StringList().toString(prefix, null, postfix); - } - else - { - result = new StringList(source).toString(prefix, separator, postfix); - } - - // - return result; - } - - /** - * - * @param prefix - * @param separator - * @param postfix - * @return - */ - public static String toString(final StringList source, final String prefix, final String separator, final String postfix) - { - String result; - - if (source == null) - { - result = new StringList().toString(prefix, null, postfix); - } - else - { - result = source.toString(prefix, separator, postfix); - } - - // - return result; - } - - /** - * Converts a {@code Collection} to an array of {@code String}. - * - * @param source - * The string list to convert. - * - * @return The result of the conversion. - */ - public static String[] toStringArray(final Collection source) - { - String[] result; - - if (source == null) - { - result = new String[0]; - } - else - { - result = new StringList(source).toStringArray(); - } - - // - return result; - } - - /** - * Converts a {@code StringList} to an array of {@code String}. - * - * @param source - * The string list to convert. - * - * @return The result of the conversion. - */ - public static String[] toStringArray(final StringList source) - { - String[] result; - - if (source == null) - { - result = new String[0]; - } - else - { - result = source.toStringArray(); - } - - // - return result; - } - - /** - * Concatenates the string from an array to a string. - * - * @param strings - * The string array to convert. - * - * @return If argument is null then returns an empty string, otherwise - * returns a string concatenation of the argument. - */ - public static String toStringNotNull(final String[] source) - { - String result; - - result = toString(source); - - if (result == null) - { - result = ""; - } - - // - return result; - } - - /** - * - * @param strings - * @return - */ - public static String toStringWithBracket(final Collection source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = new StringList(source).toStringWithBracket(); - } - - // - return result; - } - - /** - * - * @param strings - * @return - */ - public static String toStringWithBracket(final String[] source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = new StringList(source).toStringWithBracket(); - } - - // - return result; - } - - /** - * - * @param strings - * @return - */ - public static String toStringWithBracket(final StringList source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = source.toStringWithBracket(); - } - - // - return result; - } - - /** - * - * @param strings - * @return - */ - public static String toStringWithBracketNotNull(final Collection source) - { - String result; - - result = toStringWithBracket(source); - - if (result == null) - { - result = ""; - } - - // - return result; - } - - /** - * - * @param strings - * @return - */ - public static String toStringWithBracketNotNull(final String[] source) - { - String result; - - result = toStringWithBracket(source); - - if (result == null) - { - result = ""; - } - - // - return result; - } - - /** - * - * @param strings - * @return - */ - public static String toStringWithBracketNotNull(final StringList source) - { - String result; - - result = toStringWithBracket(source); - - if (result == null) - { - result = ""; - } - - // - return result; - } - - /** - * - * @param source - * @return - */ - public static String toStringWithBrackets(final Collection source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = new StringList(source).toStringWithBrackets(); - } - - // - return result; - } - - /** - * - * @param source - * @return - */ - public static String toStringWithBrackets(final String[] source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = new StringList(source).toStringWithBrackets(); - } - - // - return result; - } - - /** - * - * @param source - * @return - */ - public static String toStringWithBrackets(final StringList source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = source.toStringWithBrackets(); - } - - // - return result; - } - - /** - * - * @param source - * @return - */ - public static String toStringWithCommas(final Collection source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = new StringList(source).toStringWithCommas(); - } - - // - return result; - } - - /** - * - * @param source - * @return - */ - public static String toStringWithCommas(final String[] source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = new StringList(source).toStringWithCommas(); - } - - // - return result; - } - - /** - * - * @param source - * @return - */ - public static String toStringWithCommas(final StringList source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = source.toStringWithCommas(); - } - - // - return result; - } - - /** - * - * @param source - * @return - */ - public static String toStringWithFrenchCommas(final Collection source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = new StringList(source).toStringWithFrenchCommas(); - } - - // - return result; - } - - /** - * - * @param source - * @return - */ - public static String toStringWithFrenchCommas(final String[] source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = new StringList(source).toStringWithFrenchCommas(); - } - - // - return result; - } - - /** - * - * @param source - * @return - */ - public static String toStringWithFrenchCommas(final StringList source) - { - String result; - - if (source == null) - { - result = null; - } - else - { - result = source.toStringWithFrenchCommas(); - } - - // - return result; - } + public static final String DEFAULT_CHARSET_NAME = "UTF-8"; + + /** + * + * @param target + * @return + */ + public StringSet getComplement(final Collection alpha, final Collection bravo) + { + StringSet result; + + result = getDifference(bravo, alpha); + + // + return result; + } + + /** + * + * @param target + * @return + */ + public StringSet getDifference(final Collection alpha, final Collection bravo) + { + StringSet result; + + result = new StringSet(alpha); + result.removeAll(bravo); + + // + return result; + } + + /** + * + * @param target + * @return + */ + public StringSet getDisjunction(final Collection alpha, final Collection bravo) + { + StringSet result; + + result = new StringSet(); + + for (String string : alpha) + { + if (!bravo.contains(string)) + { + result.add(string); + } + } + + for (String string : bravo) + { + if (!alpha.contains(string)) + { + result.add(string); + } + } + + // + return result; + } + + /** + * + * @param target + * @return + */ + public StringSet getIntersectionOf(final Collection alpha, final Collection bravo) + { + StringSet result; + + result = new StringSet(); + + for (String string : alpha) + { + if (bravo.contains(string)) + { + result.add(string); + } + } + + // + return result; + } + + /** + * + * @param target + * @return + */ + public StringSet getUnionOf(final Collection alpha, final Collection bravo) + { + StringSet result; + + result = new StringSet(alpha); + + for (String string : bravo) + { + result.add(string); + } + + // + return result; + } + + /** + * Builds a string concatenating many times a source string. + * + * @param source + * The string to concatenate several time. + * + * @param multiplier + * The number of concatenate to produce. + * @return + */ + public static String concatenate(final String source, final int multiplier) + { + String result; + + result = repeat(source, multiplier).toString(); + + // + return result; + } + + /** + * + * @param file + * @throws IOException + */ + public static StringList load(final File source) throws IOException + { + StringList result; + + result = load(source, DEFAULT_CHARSET_NAME); + + // + return result; + } + + /** + * + * @param file + * @throws IOException + */ + public static StringList load(final File file, final String charsetName) throws IOException + { + StringList result; + + // + result = new StringList(); + + // + read(result, new FileInputStream(file), charsetName); + + // + return result; + } + + /** + * + * @param file + * @return + * @throws IOException + */ + public static StringList load(final URL source) throws IOException + { + StringList result; + + result = load(source, DEFAULT_CHARSET_NAME); + + // + return result; + } + + /** + * + * @param file + * @throws IOException + */ + public static StringList load(final URL source, final String charsetName) throws IOException + { + StringList result; + + // + result = new StringList(); + + // + read(result, source.openStream(), charsetName); + + // + return result; + } + + /** + * + * @param file + * @throws IOException + */ + public static void read(final StringList out, final InputStream is, final String charsetName) throws IOException + { + BufferedReader in = null; + try + { + in = new BufferedReader(new InputStreamReader(is, charsetName)); + + boolean ended = false; + + while (!ended) + { + String line = in.readLine(); + + if (line == null) + { + ended = true; + } + else + { + out.append(line); + } + } + } + finally + { + try + { + if (in != null) + { + in.close(); + } + } + catch (IOException exception) + { + exception.printStackTrace(); + } + } + } + + /** + * Builds a string list concatenating many times a source string. + * + * @param source + * The string to concatenate several time. + * + * @param multiplier + * The number of concatenate to produce. + * @return + */ + public static StringList repeat(final String source, final int multiplier) + { + StringList result; + + result = new StringList(multiplier); + for (int count = 0; count < multiplier; count++) + { + result.append(source); + } + + // + return result; + } + + /** + * + * @param file + * @throws FileNotFoundException + * @throws UnsupportedEncodingException + */ + public static void save(final File file, final StringList source) throws UnsupportedEncodingException, FileNotFoundException + { + PrintWriter out = null; + try + { + out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), DEFAULT_CHARSET_NAME)); + + if (source != null) + { + for (String string : source) + { + out.println(string); + } + } + } + finally + { + if (out != null) + { + out.close(); + } + } + } + + /** + * Sorts the string list. + * + * @param source + * The string list to sort. + */ + public static void sort(final StringList source) + { + if (source != null) + { + source.sort(); + } + } + + /** + * Concatenates the string from an array to a string. + * + * @param source + * The string array to convert. + * + * @return A string concatenation of the argument. + */ + public static String toString(final String[] source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = new StringList(source).toString(); + } + + // + return result; + } + + /** + * + * @param prefix + * @param separator + * @param postfix + * @return + */ + public static String toString(final String[] source, final String prefix, final String separator, final String postfix) + { + String result; + + if (source == null) + { + result = new StringList().toString(prefix, null, postfix); + } + else + { + result = new StringList(source).toString(prefix, separator, postfix); + } + + // + return result; + } + + /** + * + * @param prefix + * @param separator + * @param postfix + * @return + */ + public static String toString(final StringList source, final String prefix, final String separator, final String postfix) + { + String result; + + if (source == null) + { + result = new StringList().toString(prefix, null, postfix); + } + else + { + result = source.toString(prefix, separator, postfix); + } + + // + return result; + } + + /** + * Converts a {@code Collection} to an array of {@code String}. + * + * @param source + * The string list to convert. + * + * @return The result of the conversion. + */ + public static String[] toStringArray(final Collection source) + { + String[] result; + + if (source == null) + { + result = new String[0]; + } + else + { + result = new StringList(source).toStringArray(); + } + + // + return result; + } + + /** + * Converts a {@code StringList} to an array of {@code String}. + * + * @param source + * The string list to convert. + * + * @return The result of the conversion. + */ + public static String[] toStringArray(final StringList source) + { + String[] result; + + if (source == null) + { + result = new String[0]; + } + else + { + result = source.toStringArray(); + } + + // + return result; + } + + /** + * Concatenates the string from an array to a string. + * + * @param strings + * The string array to convert. + * + * @return If argument is null then returns an empty string, otherwise + * returns a string concatenation of the argument. + */ + public static String toStringNotNull(final String[] source) + { + String result; + + result = toString(source); + + if (result == null) + { + result = ""; + } + + // + return result; + } + + /** + * + * @param strings + * @return + */ + public static String toStringWithBracket(final Collection source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = new StringList(source).toStringWithBracket(); + } + + // + return result; + } + + /** + * + * @param strings + * @return + */ + public static String toStringWithBracket(final String[] source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = new StringList(source).toStringWithBracket(); + } + + // + return result; + } + + /** + * + * @param strings + * @return + */ + public static String toStringWithBracket(final StringList source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = source.toStringWithBracket(); + } + + // + return result; + } + + /** + * + * @param strings + * @return + */ + public static String toStringWithBracketNotNull(final Collection source) + { + String result; + + result = toStringWithBracket(source); + + if (result == null) + { + result = ""; + } + + // + return result; + } + + /** + * + * @param strings + * @return + */ + public static String toStringWithBracketNotNull(final String[] source) + { + String result; + + result = toStringWithBracket(source); + + if (result == null) + { + result = ""; + } + + // + return result; + } + + /** + * + * @param strings + * @return + */ + public static String toStringWithBracketNotNull(final StringList source) + { + String result; + + result = toStringWithBracket(source); + + if (result == null) + { + result = ""; + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static String toStringWithBrackets(final Collection source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = new StringList(source).toStringWithBrackets(); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static String toStringWithBrackets(final String[] source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = new StringList(source).toStringWithBrackets(); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static String toStringWithBrackets(final StringList source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = source.toStringWithBrackets(); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static String toStringWithCommas(final Collection source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = new StringList(source).toStringWithCommas(); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static String toStringWithCommas(final String[] source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = new StringList(source).toStringWithCommas(); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static String toStringWithCommas(final StringList source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = source.toStringWithCommas(); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static String toStringWithFrenchCommas(final Collection source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = new StringList(source).toStringWithFrenchCommas(); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static String toStringWithFrenchCommas(final String[] source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = new StringList(source).toStringWithFrenchCommas(); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static String toStringWithFrenchCommas(final StringList source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = source.toStringWithFrenchCommas(); + } + + // + return result; + } }