Refactor packages for StringList. Remove Concatenator class. Split

StringList with StringListUtils.
This commit is contained in:
Christian P. MOMON 2015-05-23 00:29:04 +02:00
parent 86b02b2ca3
commit 7158216734
16 changed files with 300 additions and 621 deletions

View file

@ -31,6 +31,8 @@ import java.io.PrintWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URL; import java.net.URL;
import fr.devinsy.util.strings.StringList;
/** /**
* *
* @author cpm * @author cpm

View file

@ -1,399 +0,0 @@
/**
* Copyright (C) 2008-2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.io.IOException;
import java.util.ArrayList;
/**
* This class is a collection of String objects with specific methods. It makes
* possible to build a string without any copy. The goal is to optimize the
* building of strings where they are lot of concatenation action.
*
* @deprecated Replaced by StringList.
*/
@Deprecated
public class StringConcatenator extends ArrayList<String>
{
private static final long serialVersionUID = -1154185934830213732L;
public String LINE_SEPARATOR = "\n";
/**
*
*/
public StringConcatenator()
{
super();
}
/**
*
*/
public StringConcatenator append(final char character)
{
StringConcatenator result;
this.add(String.valueOf(character));
result = this;
//
return (result);
}
/**
*
*/
public StringConcatenator append(final String string)
{
StringConcatenator result;
if (string != null)
{
this.add(string);
}
result = this;
//
return (result);
}
/**
*
*/
public StringConcatenator append(final StringConcatenator string)
{
StringConcatenator result;
if (string != null)
{
for (int nString = 0; nString < string.size(); nString++)
{
this.add(string.getByIndex(nString));
}
}
result = this;
//
return (result);
}
/**
*
*/
public StringConcatenator appendln()
{
StringConcatenator result;
this.add(this.LINE_SEPARATOR);
result = this;
//
return (result);
}
/**
*
*/
public StringConcatenator appendln(final char character)
{
StringConcatenator result;
result = this.append(character).appendln();
//
return (result);
}
/**
*
*/
public StringConcatenator appendln(final String string)
{
StringConcatenator result;
result = this.append(string).appendln();
//
return (result);
}
/**
*
*/
public StringConcatenator appendln(final StringConcatenator string)
{
StringConcatenator result;
result = this.append(string).appendln();
//
return (result);
}
/**
*
*/
public String getByIndex(final int id)
{
String result;
result = this.get(id);
//
return (result);
}
/**
*
*/
public int lenght()
{
int result = 0;
for (int nString = 0; nString < this.size(); nString++)
{
result += this.getByIndex(nString).length();
}
//
return (result);
}
/**
*
*/
@Override
public String toString()
{
String result;
StringBuffer preResult = new StringBuffer(this.lenght());
for (int nString = 0; nString < this.size(); nString++)
{
preResult.append(this.getByIndex(nString));
}
result = new String(preResult);
//
return (result);
}
/**
*
*/
public void writeInto(final java.io.Writer out) throws IOException
{
for (int nString = 0; nString < this.size(); nString++)
{
out.write(this.getByIndex(nString));
}
}
/**
*
*/
static public String toString(final String[] strings)
{
String result;
if (strings == null)
{
result = null;
}
else
{
StringConcatenator string = new StringConcatenator();
for (int nString = 0; nString < strings.length; nString++)
{
string.append(strings[nString]);
if (nString < strings.length - 1)
{
string.append(' ');
}
}
result = string.toString();
}
//
return (result);
}
/**
*
*/
static public String toStringNotNull(final String[] strings)
{
String result;
result = toString(strings);
if (result == null)
{
result = "";
}
//
return (result);
}
/**
*
*/
static public String toStringWithBracket(final String[] strings)
{
String result;
if (strings == null)
{
result = null;
}
else
{
StringConcatenator merge = new StringConcatenator();
merge.append("[");
merge.append(toStringWithCommas(strings));
merge.append("]");
result = merge.toString();
}
//
return (result);
}
/**
*
*/
static public String toStringWithBracketNotNull(final String[] strings)
{
String result;
result = toStringWithBrackets(strings);
if (result == null)
{
result = "";
}
//
return (result);
}
/**
*
*/
static public String toStringWithBrackets(final String[] strings)
{
String result;
if (strings == null)
{
result = null;
}
else
{
StringConcatenator merge = new StringConcatenator();
for (String string : strings)
{
merge.append("[").append(string).append("]");
}
result = merge.toString();
}
//
return (result);
}
/**
*
*/
static public String toStringWithCommas(final String[] strings)
{
String result;
if (strings == null)
{
result = null;
}
else
{
StringConcatenator merge = new StringConcatenator();
for (String string : strings)
{
if (merge.size() != 0)
{
merge.append(",");
}
merge.append(string);
}
result = merge.toString();
}
//
return (result);
}
/**
*
*/
static public String toStringWithFrenchCommas(final String[] strings)
{
String result;
if (strings == null)
{
result = null;
}
else
{
StringConcatenator merge = new StringConcatenator();
for (String string : strings)
{
if (merge.size() != 0)
{
merge.append(", ");
}
merge.append(string);
}
result = merge.toString();
}
//
return (result);
}
}

View file

@ -24,6 +24,8 @@ import java.util.Set;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import fr.devinsy.util.strings.StringList;
/** /**
* *
* @author christian.momon@devinsy.fr * @author christian.momon@devinsy.fr

View file

@ -1,5 +1,5 @@
/** /**
* Copyright (C) 2005-2010, 2013 Christian Pierre MOMON * Copyright (C) 2005-2010, 2013, 2015 Christian Pierre MOMON
* *
* This file is part of Devinsy-utils. * This file is part of Devinsy-utils.
* *
@ -21,7 +21,7 @@ package fr.devinsy.util.cmdexec;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.util.StringConcatenator; import fr.devinsy.util.strings.StringListUtils;
/** /**
* We must use the isOver method on Gobblers because with short tasks the * We must use the isOver method on Gobblers because with short tasks the
@ -195,8 +195,8 @@ public class CmdExec
{ {
this.exitValue = 0; this.exitValue = 0;
logger.info("CmdExec(commande[]) = [" + StringConcatenator.toString(command) + "]"); logger.info("CmdExec(commande[]) = [" + StringListUtils.toString(command) + "]");
logger.info("CmdExec(commande[]) = [" + StringConcatenator.toStringWithBrackets(command) + "]"); logger.info("CmdExec(commande[]) = [" + StringListUtils.toStringWithBrackets(command) + "]");
try try
{ {
@ -316,9 +316,9 @@ public class CmdExec
} }
else else
{ {
logger.error("Command=\"" + StringConcatenator.toStringWithBrackets(command)); logger.error("Command=\"" + StringListUtils.toStringWithBrackets(command));
logger.error("Command=\"[" + StringConcatenator.toString(command) + "]\n out => [" + cmd.getOutStream() + "]\n " + "err => (" + cmd.getErrStream().length() + ")[" logger.error("Command=\"[" + StringListUtils.toString(command) + "]\n out => [" + cmd.getOutStream() + "]\n " + "err => (" + cmd.getErrStream().length() + ")[" + cmd.getErrStream()
+ cmd.getErrStream() + "]"); + "]");
throw new Exception(cmd.getErrStream()); throw new Exception(cmd.getErrStream());
} }
} }
@ -371,11 +371,11 @@ public class CmdExec
} }
else if (nullArg) else if (nullArg)
{ {
throw new Exception("Null parameter detected in position " + nArg + " for " + StringConcatenator.toStringWithBrackets(args) + "."); throw new Exception("Null parameter detected in position " + nArg + " for " + StringListUtils.toStringWithBrackets(args) + ".");
} }
else if ((args.length < min) || (args.length > max)) else if ((args.length < min) || (args.length > max))
{ {
throw new Exception("Bad number of parameters: " + args.length + " for " + StringConcatenator.toStringWithBrackets(args) + "."); throw new Exception("Bad number of parameters: " + args.length + " for " + StringListUtils.toStringWithBrackets(args) + ".");
} }
else else
{ {
@ -406,24 +406,24 @@ public class CmdExec
// //
boolean nullArg = false; boolean nullArg = false;
boolean ended = false; boolean ended = false;
int nArg = 0; int argumentCounter = 0;
while (!ended) while (!ended)
{ {
if (nArg >= args.length) if (argumentCounter >= args.length)
{ {
ended = true; ended = true;
nullArg = false; nullArg = false;
} }
else else
{ {
if (args[nArg] == null) if (args[argumentCounter] == null)
{ {
ended = true; ended = true;
nullArg = true; nullArg = true;
} }
else else
{ {
nArg += 1; argumentCounter += 1;
} }
} }
} }
@ -435,20 +435,20 @@ public class CmdExec
} }
else if (nullArg) else if (nullArg)
{ {
throw new Exception("Null parameter detected in position " + nArg + " for " + StringConcatenator.toStringWithBrackets(args) + "."); throw new Exception("Null parameter detected in position " + argumentCounter + " for " + StringListUtils.toStringWithBrackets(args) + ".");
} }
else if ((args.length < min) || (args.length > max)) else if ((args.length < min) || (args.length > max))
{ {
throw new Exception("Bad number of parameters: " + args.length + " for " + StringConcatenator.toStringWithBrackets(args) + "."); throw new Exception("Bad number of parameters: " + args.length + " for " + StringListUtils.toStringWithBrackets(args) + ".");
} }
else else
{ {
// //
String[] command = new String[args.length + 1]; String[] command = new String[args.length + 1];
command[0] = program; command[0] = program;
for (nArg = 0; nArg < args.length; nArg++) for (argumentCounter = 0; argumentCounter < args.length; argumentCounter++)
{ {
command[nArg + 1] = args[nArg]; command[argumentCounter + 1] = args[argumentCounter];
} }
result = CmdExec.run(command); result = CmdExec.run(command);

View file

@ -21,7 +21,7 @@ package fr.devinsy.util.rss;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import fr.devinsy.util.StringList; import fr.devinsy.util.strings.StringList;
/** /**
* *

View file

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/> * along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/ */
package fr.devinsy.util; package fr.devinsy.util.strings;
import java.io.IOException; import java.io.IOException;
import java.text.Collator; import java.text.Collator;
@ -843,173 +843,4 @@ public class StringList extends ArrayList<String> implements CharSequence
// //
return (result); return (result);
} }
/**
* This method converts a string array to a string.
*/
public static String toString(final String[] source)
{
String result;
if (source == null)
{
result = null;
}
else
{
result = new StringList(source).toString();
}
//
return (result);
}
/**
* This method converts a StringList to an array of String.
*/
public static String[] toStringArray(final StringList source)
{
String[] result;
if (source == null)
{
result = new String[0];
}
else
{
result = source.toStringArray();
}
//
return (result);
}
/**
*
*/
public static String toStringNotNull(final String[] strings)
{
String result;
result = toString(strings);
if (result == null)
{
result = "";
}
//
return (result);
}
/**
*
*/
public static String toStringWithBracket(final String[] strings)
{
String result;
if (strings == null)
{
result = null;
}
else
{
StringList merge = new StringList();
merge.append("[");
merge.append(toStringWithCommas(strings));
merge.append("]");
result = merge.toString();
}
//
return (result);
}
/**
*
*/
public static String toStringWithBracketNotNull(final String[] strings)
{
String result;
result = toStringWithBrackets(strings);
if (result == null)
{
result = "";
}
//
return (result);
}
/**
*
*/
public static String toStringWithBrackets(final String[] source)
{
String result;
if (source == null)
{
result = null;
}
else
{
StringList merge = new StringList();
for (String string : source)
{
merge.append("[").append(string).append("]");
}
result = merge.toString();
}
//
return (result);
}
/**
*
*/
public static String toStringWithCommas(final String[] source)
{
String result;
if (source == null)
{
result = null;
}
else
{
result = new StringList(source).toStringSeparatedBy(",");
}
//
return (result);
}
/**
*
*/
public static String toStringWithFrenchCommas(final String[] source)
{
String result;
if (source == null)
{
result = null;
}
else
{
result = new StringList(source).toStringSeparatedBy(", ");
}
//
return (result);
}
} }

View file

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/> * along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/ */
package fr.devinsy.util; package fr.devinsy.util.strings;
/** /**
* This class manages a char position in a StringList object. * This class manages a char position in a StringList object.

View file

@ -0,0 +1,261 @@
/**
* Copyright (C) 2008-2010, 2013-2015 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util.strings;
/**
* This class is a collection of String objects with specific methods. It makes
* possible to build a string without any copy. The goal is to optimize the
* building of strings where they are lot of concatenation action.
*/
public class StringListUtils
{
/**
* Builds a string list concatenating several time one string.
*
* @param source
* The string to concatenate several time.
* @param number
* The number of times to multiply.
* @return
*/
public static String multiply(final String source, final int number)
{
String result;
StringList strings = new StringList(number);
for (int count = 0; count < number; count++)
{
strings.append(source);
}
result = strings.toString();
//
return result;
}
/**
* 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;
}
/**
* 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[] strings)
{
String result;
result = toString(strings);
if (result == null)
{
result = "";
}
//
return result;
}
/**
*
* @param strings
* @return
*/
public static String toStringWithBracket(final String[] strings)
{
String result;
if (strings == null)
{
result = null;
}
else
{
StringList merge = new StringList();
merge.append("[");
merge.append(toStringWithCommas(strings));
merge.append("]");
result = merge.toString();
}
//
return result;
}
/**
*
* @param strings
* @return
*/
public static String toStringWithBracketNotNull(final String[] strings)
{
String result;
result = toStringWithBrackets(strings);
if (result == null)
{
result = "";
}
//
return result;
}
/**
*
* @param source
* @return
*/
public static String toStringWithBrackets(final String[] source)
{
String result;
if (source == null)
{
result = null;
}
else
{
StringList merge = new StringList();
for (String string : source)
{
merge.append("[").append(string).append("]");
}
result = merge.toString();
}
//
return result;
}
/**
*
* @param source
* @return
*/
public static String toStringWithCommas(final String[] source)
{
String result;
if (source == null)
{
result = null;
}
else
{
result = new StringList(source).toStringSeparatedBy(",");
}
//
return result;
}
/**
*
* @param source
* @return
*/
public static String toStringWithFrenchCommas(final String[] source)
{
String result;
if (source == null)
{
result = null;
}
else
{
result = new StringList(source).toStringSeparatedBy(", ");
}
//
return result;
}
}

View file

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/> * along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/ */
package fr.devinsy.util; package fr.devinsy.util.strings;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;

View file

@ -1,5 +1,5 @@
/** /**
* Copyright (C) 2014 Christian Pierre MOMON * Copyright (C) 2015 Christian Pierre MOMON
* *
* This file is part of Devinsy-utils. * This file is part of Devinsy-utils.
* *
@ -16,7 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/> * along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/ */
package fr.devinsy.util; package fr.devinsy.util.strings;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -28,7 +28,7 @@ import java.util.List;
*/ */
public class StringSet extends HashSet<String> public class StringSet extends HashSet<String>
{ {
private static final long serialVersionUID = 7421460140821150313L; private static final long serialVersionUID = 6674838743930005326L;
/** /**
* *
@ -213,24 +213,6 @@ public class StringSet extends HashSet<String>
return result; return result;
} }
/**
* Check null parameter before add.
*/
public StringSet pub(final String string)
{
StringSet result;
if (string != null)
{
this.add(string);
}
result = this;
//
return result;
}
/** /**
* *
*/ */
@ -325,7 +307,7 @@ public class StringSet extends HashSet<String>
} }
/** /**
* * Check null parameter before add.
*/ */
public StringSet put(final String string) public StringSet put(final String string)
{ {

View file

@ -24,8 +24,8 @@ import java.util.Vector;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.util.StringConcatenator;
import fr.devinsy.util.cmdexec.CmdExec; import fr.devinsy.util.cmdexec.CmdExec;
import fr.devinsy.util.strings.StringListUtils;
import fr.devinsy.util.unix.acl.Acl; import fr.devinsy.util.unix.acl.Acl;
import fr.devinsy.util.unix.acl.AclManager; import fr.devinsy.util.unix.acl.AclManager;
@ -515,7 +515,7 @@ public class Unix
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception("Error running setfacl command for " + StringConcatenator.toStringWithBrackets(args) + ":" + exception.getMessage() + ".", exception); throw new Exception("Error running setfacl command for " + StringListUtils.toStringWithBrackets(args) + ":" + exception.getMessage() + ".", exception);
} }
} }

View file

@ -25,8 +25,8 @@ import java.util.regex.Pattern;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.util.StringConcatenator;
import fr.devinsy.util.cmdexec.CmdExec; import fr.devinsy.util.cmdexec.CmdExec;
import fr.devinsy.util.strings.StringListUtils;
import fr.devinsy.util.unix.Unix; import fr.devinsy.util.unix.Unix;
/** /**
@ -416,7 +416,7 @@ public class AclManager
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception("Error running setfacl command for " + StringConcatenator.toStringWithBrackets(args) + ":" + exception.getMessage() + "."); throw new Exception("Error running setfacl command for " + StringListUtils.toStringWithBrackets(args) + ":" + exception.getMessage() + ".");
} }
} }
} }

View file

@ -24,7 +24,6 @@ import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.io.UnsupportedEncodingException;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventReader;
@ -36,7 +35,7 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.util.StringList; import fr.devinsy.util.strings.StringList;
import fr.devinsy.util.xml.XMLTag.TagType; import fr.devinsy.util.xml.XMLTag.TagType;
/** /**
@ -65,7 +64,6 @@ public class XMLReader
* @param file * @param file
* @throws XMLStreamException * @throws XMLStreamException
* @throws FileNotFoundException * @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/ */
public XMLReader(final File file) throws FileNotFoundException, XMLStreamException public XMLReader(final File file) throws FileNotFoundException, XMLStreamException
{ {
@ -79,7 +77,6 @@ public class XMLReader
* *
* @param target * @param target
* @throws XMLStreamException * @throws XMLStreamException
* @throws UnsupportedEncodingException
*/ */
public XMLReader(final InputStream source) throws XMLStreamException public XMLReader(final InputStream source) throws XMLStreamException
{ {
@ -93,7 +90,6 @@ public class XMLReader
* *
* @param target * @param target
* @throws XMLStreamException * @throws XMLStreamException
* @throws UnsupportedEncodingException
*/ */
public XMLReader(final Reader source) throws XMLStreamException public XMLReader(final Reader source) throws XMLStreamException
{ {

View file

@ -27,6 +27,8 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import fr.devinsy.util.strings.StringList;
/** /**
* *
* @author Christian P. Momon * @author Christian P. Momon

View file

@ -25,6 +25,8 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import fr.devinsy.util.strings.StringList;
/** /**
* *
* @author Christian P. Momon * @author Christian P. Momon

View file

@ -28,7 +28,7 @@ import org.apache.log4j.Level;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.junit.Before; import org.junit.Before;
import fr.devinsy.util.StringList; import fr.devinsy.util.strings.StringList;
/** /**
* *