Prepare changes to version 3. Refactored package and methods. Normalized

methods.
This commit is contained in:
Christian P. MOMON 2015-09-17 16:38:13 +02:00
parent d2d0990e05
commit 6984f9fbfc
10 changed files with 1606 additions and 206 deletions

View file

@ -1,5 +1,5 @@
/**
* Copyright (C) 2008-2014 Christian Pierre MOMON
* Copyright (C) 2008-2015 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
@ -32,14 +32,16 @@ import java.io.UnsupportedEncodingException;
import java.net.URL;
import fr.devinsy.util.strings.StringList;
import fr.devinsy.util.strings.StringListUtils;
/**
*
* @author cpm
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
public class FileTools
{
public static final String DEFAULT_CHARSET_NAME = "UTF-8";
/**
*
*
@ -151,7 +153,10 @@ public class FileTools
* Source.
*
* @return Extension value or null.
* @deprecated See
* <code>org.apache.commons.io.FilenameUtils.getExtension</code>
*/
@Deprecated
public static String getExtension(final String fileName)
{
String result;
@ -187,7 +192,6 @@ public class FileTools
{
String result;
final String DEFAULT_CHARSET_NAME = "UTF-8";
result = load(source, DEFAULT_CHARSET_NAME);
//
@ -219,7 +223,6 @@ public class FileTools
{
String result;
final String DEFAULT_CHARSET_NAME = "UTF-8";
result = load(source, DEFAULT_CHARSET_NAME);
//
@ -246,6 +249,36 @@ public class FileTools
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static StringList loadStringList(final File source) throws IOException
{
StringList result;
result = loadStringList(source, DEFAULT_CHARSET_NAME);
//
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static StringList loadStringList(final File file, final String charsetName) throws IOException
{
StringList result;
result = StringListUtils.load(file, charsetName);
//
return result;
}
/**
*
* @param file
@ -255,7 +288,6 @@ public class FileTools
{
StringBuffer result;
final String DEFAULT_CHARSET_NAME = "UTF-8";
result = loadToStringBuffer(source, DEFAULT_CHARSET_NAME);
//
@ -321,7 +353,6 @@ public class FileTools
{
StringList result;
final String DEFAULT_CHARSET_NAME = "UTF-8";
result = loadToStringList(source, DEFAULT_CHARSET_NAME);
//
@ -388,7 +419,6 @@ public class FileTools
{
StringList result;
final String DEFAULT_CHARSET_NAME = "UTF-8";
result = loadToStringList(source, DEFAULT_CHARSET_NAME);
//
@ -405,10 +435,7 @@ public class FileTools
StringList result;
//
result = new StringList();
//
read(result, source.openStream(), charsetName);
result = StringListUtils.load(source, charsetName);
//
return result;
@ -459,56 +486,16 @@ public class FileTools
}
}
/**
*
* @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();
}
}
}
/**
*
* @param source
* @param extension
* @return
*
* @deprecated See
* <code>org.apache.commons.io.FilenameUtils.removeExtension</code>
*/
@Deprecated
public static String removeExtension(final String source)
{
String result;
@ -547,7 +534,7 @@ public class FileTools
PrintWriter out = null;
try
{
out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), DEFAULT_CHARSET_NAME));
out.println(source);
}
@ -568,20 +555,7 @@ public class FileTools
*/
public static void save(final File file, final StringBuffer source) throws UnsupportedEncodingException, FileNotFoundException
{
PrintWriter out = null;
try
{
out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
out.println(source.toString());
}
finally
{
if (out != null)
{
out.close();
}
}
save(file, source.toString());
}
/**
@ -592,20 +566,7 @@ public class FileTools
*/
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), "UTF-8"));
out.println(source.toString());
}
finally
{
if (out != null)
{
out.close();
}
}
StringListUtils.save(file, source);
}
/**

View file

@ -0,0 +1,46 @@
package fr.devinsy.util.strings;
import java.io.BufferedReader;
import java.io.IOException;
/**
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*
*/
public class BufferedStringListReader implements StringListReader
{
private BufferedReader in;
/**
*
* @param in
*/
public BufferedStringListReader(final BufferedReader in)
{
this.in = in;
}
/**
*
*/
@Override
public void close()
{
}
/**
*
*/
@Override
public String readLine() throws IOException
{
String result;
result = this.in.readLine();
//
return result;
}
}

View file

@ -0,0 +1,128 @@
package fr.devinsy.util.strings;
import java.util.Comparator;
/**
*
*/
public class StringLengthComparator implements Comparator<String>
{
private static StringLengthComparator instance;
/**
*
*/
@Override
public int compare(final String alpha, final String bravo)
{
int result;
//
Integer alphaValue;
if (alpha == null)
{
//
alphaValue = null;
}
else
{
//
alphaValue = alpha.length();
}
//
Integer bravoValue;
if (bravo == null)
{
//
bravoValue = null;
}
else
{
//
bravoValue = bravo.length();
}
//
result = compare(alphaValue, bravoValue);
//
return result;
}
/**
* This method compares two nullable string values.
*
* The comparison manages the local language alphabet order.
*
* <pre>
* compare(null, null) = 0
* compare(null, bravo) < 0
* compare(alpha, null) > 0
* compare(alpha, bravo) = alpha.compareTo(bravo)
* </pre>
*
* @param alpha
* one of the value.
*
* @param bravo
* the other value.
*
* @return zero or a positive value or a negative value.
*
*/
public static int compare(final Integer alpha, final Integer bravo)
{
int result;
//
if ((alpha == null) && (bravo == null))
{
//
result = 0;
}
else if (alpha == null)
{
//
result = -1;
}
else if (bravo == null)
{
//
result = +1;
}
else
{
//
result = alpha.compareTo(bravo);
}
//
return result;
}
/**
*
* @return
*/
public static StringLengthComparator instance()
{
StringLengthComparator result;
if (instance == null)
{
instance = new StringLengthComparator();
}
result = instance;
//
return result;
}
}

View file

@ -23,11 +23,16 @@ import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import org.apache.commons.lang3.StringUtils;
/**
* 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 StringList extends ArrayList<String> implements CharSequence
{
@ -161,28 +166,6 @@ public class StringList extends ArrayList<String> implements CharSequence
return (result);
}
/**
* Appends the string representation of the Double argument to this string
* list.
*
* @param value
* @return
*/
public StringList append(final Double value)
{
StringList result;
if (value != null)
{
this.append(String.valueOf(value));
}
result = this;
//
return (result);
}
/**
* Appends the string representation of the int argument to this string
* list.
@ -217,6 +200,45 @@ public class StringList extends ArrayList<String> implements CharSequence
return (result);
}
/**
* Appends the string representation of the int argument to this string
* list.
*
* @param value
* @return
*/
public StringList append(final Object value)
{
StringList result;
if (value != null)
{
this.append(value.toString());
}
result = this;
//
return (result);
}
/**
* Appends the string representation of the long argument to this string
* list.
*
* @param value
* @return
*/
public StringList append(final short value)
{
StringList result;
result = this.append(String.valueOf(value));
//
return (result);
}
/**
* Appends the string argument to this string list.
*
@ -358,6 +380,36 @@ public class StringList extends ArrayList<String> implements CharSequence
return (result);
}
/**
*
* @param value
* @return
*/
public StringList appendln(final Object value)
{
StringList result;
result = this.append(value).appendln();
//
return (result);
}
/**
*
* @param value
* @return
*/
public StringList appendln(final short value)
{
StringList result;
result = this.append(value).appendln();
//
return (result);
}
/**
*
* @param string
@ -406,6 +458,22 @@ public class StringList extends ArrayList<String> implements CharSequence
return result;
}
/**
*
* @param position
* @return
*/
public char charAt(final StringListCharPosition position)
{
char result;
//
result = get(position.getStringIndex()).charAt(position.getLocalCharIndex());
//
return result;
}
/**
* Deep copy and shallow copy have no sense about a list of immutable
* objects.
@ -428,6 +496,150 @@ public class StringList extends ArrayList<String> implements CharSequence
return result;
}
/**
*
* @param source
* @return
*/
public boolean containsAny(final Collection<String> target)
{
boolean result;
if (target == null)
{
result = false;
}
else
{
boolean ended = false;
Iterator<String> iterator = target.iterator();
result = false;
while (!ended)
{
if (iterator.hasNext())
{
String current = iterator.next();
if (this.contains(current))
{
ended = true;
result = true;
}
}
else
{
ended = true;
result = false;
}
}
}
//
return result;
}
/**
*
* @return
*/
public boolean containsBlank()
{
boolean result;
boolean ended = false;
Iterator<String> iterator = iterator();
result = false;
while (!ended)
{
if (iterator.hasNext())
{
String line = iterator.next();
if (StringUtils.isBlank(line))
{
ended = true;
result = true;
}
}
else
{
ended = false;
result = false;
}
}
//
return result;
}
/**
*
* @return
*/
public boolean containsEmpty()
{
boolean result;
boolean ended = false;
Iterator<String> iterator = iterator();
result = false;
while (!ended)
{
if (iterator.hasNext())
{
String line = iterator.next();
if (StringUtils.isEmpty(line))
{
ended = true;
result = true;
}
}
else
{
ended = false;
result = false;
}
}
//
return result;
}
/**
*
* @return
*/
public boolean containsNull()
{
boolean result;
boolean ended = false;
Iterator<String> iterator = iterator();
result = false;
while (!ended)
{
if (iterator.hasNext())
{
String line = iterator.next();
if (line == null)
{
ended = true;
result = true;
}
}
else
{
ended = false;
result = false;
}
}
//
return result;
}
/**
*
* @param id
@ -485,6 +697,192 @@ public class StringList extends ArrayList<String> implements CharSequence
return (result);
}
/**
*
* @return
*/
public String getLongestBytesLine()
{
String result;
if (isEmpty())
{
result = null;
}
else
{
int max = 0;
result = "";
for (String line : this)
{
if (line.getBytes().length > max)
{
max = line.length();
result = line;
}
}
}
//
return result;
}
/**
*
* @return
*/
public String getLongestLine()
{
String result;
if (isEmpty())
{
result = null;
}
else
{
int max = 0;
result = "";
for (String line : this)
{
if (line.length() > max)
{
max = line.length();
result = line;
}
}
}
//
return result;
}
/**
*
* @return
*/
public StringList getNotBlank()
{
StringList result;
result = new StringList();
for (String line : this)
{
if (StringUtils.isNotBlank(line))
{
result.add(line);
}
}
//
return (result);
}
/**
*
* @return
*/
public StringList getNotEmpty()
{
StringList result;
result = new StringList();
for (String line : this)
{
if (StringUtils.isNotEmpty(line))
{
result.add(line);
}
}
//
return (result);
}
/**
*
* @return
*/
public StringList getNotNull()
{
StringList result;
result = new StringList();
for (String line : this)
{
if (line != null)
{
result.add(line);
}
}
//
return (result);
}
/**
*
* @return
*/
public String getShortestBytesLine()
{
String result;
if (isEmpty())
{
result = null;
}
else
{
int min = Integer.MAX_VALUE;
result = null;
for (String line : this)
{
if (line.getBytes().length < min)
{
min = line.length();
result = line;
}
}
}
//
return result;
}
/**
*
* @return
*/
public String getShortestLine()
{
String result;
if (isEmpty())
{
result = null;
}
else
{
int min = Integer.MAX_VALUE;
result = null;
for (String line : this)
{
if (line.length() < min)
{
min = line.length();
result = line;
}
}
}
//
return result;
}
/**
*
* @param index
@ -526,6 +924,148 @@ public class StringList extends ArrayList<String> implements CharSequence
/**
*
* @param startIndex
* @return the index of the next line not null, -1 otherwise.
*/
public int indexOfNextLineNotNull(final int startIndex)
{
int result;
boolean ended = false;
int currentIndex = startIndex;
result = -1;
while (!ended)
{
if (currentIndex >= this.size())
{
ended = true;
result = -1;
}
else
{
if (this.get(currentIndex) == null)
{
ended = true;
result = currentIndex;
}
else
{
currentIndex += 1;
}
}
}
//
return result;
}
/**
*
* @param position
* @return
*/
public boolean isOutOfBounds(final StringListCharPosition position)
{
boolean result;
if (position == null)
{
result = true;
}
else
{
if (isOutOfList(position))
{
result = true;
}
else if (isOutOfLine(position))
{
result = true;
}
else
{
result = false;
}
}
//
return result;
}
/**
*
* @param position
* @return
*/
public boolean isOutOfLine(final StringListCharPosition position)
{
boolean result;
if (position == null)
{
result = true;
}
else
{
if (position.getLocalCharIndex() >= this.get(position.getStringIndex()).length())
{
result = true;
}
else
{
result = false;
}
}
//
return result;
}
/**
*
* @param position
* @return
*/
public boolean isOutOfList(final StringListCharPosition position)
{
boolean result;
if (position == null)
{
result = true;
}
else
{
if (position.getStringIndex() >= this.size())
{
result = true;
}
else
{
result = false;
}
}
//
return result;
}
/**
*
*/
public Iterator<Character> iteratorOfChar()
{
Iterator<Character> result;
result = new StringListCharIterator(this);
//
return result;
}
/**
* Returns the length of the string list concatenation. Null strings are
* ignored.
*/
@Override
public int length()
@ -534,8 +1074,10 @@ public class StringList extends ArrayList<String> implements CharSequence
for (String string : this)
{
result += string.length();
if (string != null)
{
result += string.length();
}
}
//
@ -619,6 +1161,24 @@ public class StringList extends ArrayList<String> implements CharSequence
return (result);
}
/**
* Sorts this list.
*
* @return This List.
*/
public StringList reverse()
{
StringList result;
Collections.reverse(this);
//
result = this;
//
return result;
}
/**
* Sorts this list.
*
@ -637,6 +1197,42 @@ public class StringList extends ArrayList<String> implements CharSequence
return result;
}
/**
* Sorts this list.
*
* @return This List.
*/
public StringList sort(final Comparator<String> comparator)
{
StringList result;
Collections.sort(this, comparator);
//
result = this;
//
return result;
}
/**
* Sorts this list.
*
* @return This List.
*/
public StringList sortByLength()
{
StringList result;
Collections.sort(this, StringLengthComparator.instance());
//
result = this;
//
return result;
}
/**
* {@inheritDoc}
*/
@ -844,6 +1440,43 @@ public class StringList extends ArrayList<String> implements CharSequence
return (result);
}
/**
*
* @param strings
* @return
*/
public String toStringWithBracket()
{
String result;
result = toString("[", ",", "]");
//
return result;
}
/**
*
* @param source
* @return
*/
public String toStringWithBrackets()
{
String result;
StringList buffer = new StringList();
for (String string : this)
{
buffer.append("[").append(string).append("]");
}
result = buffer.toString();
//
return result;
}
/**
*
* @return

View file

@ -0,0 +1,133 @@
/**
* Copyright (C) 2014-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;
import java.util.Iterator;
/**
*
*/
public class StringListCharIterator implements Iterator<Character>
{
private StringList source;
private StringListCharPosition currentPosition;
private StringListCharPosition nextPosition;
/**
*
*/
public StringListCharIterator(final StringList source)
{
super();
this.source = source;
this.nextPosition = new StringListCharPosition();
}
/**
*
*/
@Override
public boolean hasNext()
{
boolean result;
if (this.source == null)
{
result = false;
}
else
{
if (this.source.isOutOfBounds(this.nextPosition))
{
result = false;
}
else
{
result = true;
}
}
//
return result;
}
/**
*
*/
@Override
public Character next()
{
Character result;
if (this.source == null)
{
result = null;
}
else
{
if (hasNext())
{
result = this.source.charAt(this.nextPosition);
this.nextPosition.next();
if (this.source.isOutOfLine(this.nextPosition))
{
this.nextPosition.nextEndOfLine();
while ((!this.source.isOutOfList(this.nextPosition)) && (this.source.get(this.nextPosition.getStringIndex()) == null))
{
this.nextPosition.nextEndOfLine();
}
}
}
else
{
result = null;
}
}
//
return result;
}
/**
*
* @return
*/
public StringListCharPosition nextPosition()
{
StringListCharPosition result;
result = new StringListCharPosition(this.nextPosition);
//
return result;
}
/**
* Do nothing.
*/
@Override
public void remove()
{
// TODO or not TODO?
}
}

View file

@ -29,6 +29,19 @@ public class StringListCharPosition
private int stringIndex;
private int localCharIndex;
/**
*
* @param index
* @param stringIndex
* @param localIndex
*/
public StringListCharPosition()
{
this.charIndex = 0;
this.stringIndex = 0;
this.localCharIndex = 0;
}
/**
*
* @param index
@ -42,6 +55,17 @@ public class StringListCharPosition
this.localCharIndex = localIndex;
}
/**
*
* @param source
*/
public StringListCharPosition(final StringListCharPosition source)
{
this.charIndex = source.getCharIndex();
this.stringIndex = source.getStringIndex();
this.localCharIndex = source.getLocalCharIndex();
}
public int getCharIndex()
{
return this.charIndex;
@ -57,6 +81,18 @@ public class StringListCharPosition
return this.stringIndex;
}
public void next()
{
this.charIndex += 1;
this.localCharIndex += 1;
}
public void nextEndOfLine()
{
this.localCharIndex = 0;
this.stringIndex += 1;
}
public void setCharIndex(final int charIndex)
{
this.charIndex = charIndex;
@ -71,4 +107,5 @@ public class StringListCharPosition
{
this.stringIndex = stringIndex;
}
}

View file

@ -0,0 +1,23 @@
package fr.devinsy.util.strings;
import java.io.Closeable;
import java.io.IOException;
/**
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*
*/
public interface StringListReader extends Closeable
{
/**
*
*/
@Override
void close();
/**
*
*/
String readLine() throws IOException;
}

View file

@ -18,6 +18,18 @@
*/
package fr.devinsy.util.strings;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Collection;
/**
@ -28,24 +40,64 @@ import java.util.Collection;
*/
public class StringListUtils
{
public static final String DEFAULT_CHARSET_NAME = "UTF-8";
/**
*
* @param prefix
* @param separator
* @param postfix
* @param target
* @return
*/
public String toString(final StringList source, final String prefix, final String separator, final String postfix)
public StringSet getComplement(final Collection<String> alpha, final Collection<String> bravo)
{
String result;
StringSet result;
if (source == null)
result = getDifference(bravo, alpha);
//
return result;
}
/**
*
* @param target
* @return
*/
public StringSet getDifference(final Collection<String> alpha, final Collection<String> bravo)
{
StringSet result;
result = new StringSet(alpha);
result.removeAll(bravo);
//
return result;
}
/**
*
* @param target
* @return
*/
public StringSet getDisjunction(final Collection<String> alpha, final Collection<String> bravo)
{
StringSet result;
result = new StringSet();
for (String string : alpha)
{
result = prefix + postfix;
if (!bravo.contains(string))
{
result.add(string);
}
}
else
for (String string : bravo)
{
result = source.toString(prefix, separator, postfix);
if (!alpha.contains(string))
{
result.add(string);
}
}
//
@ -53,30 +105,235 @@ 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.
* @param target
* @return
*/
public static String multiply(final String source, final int number)
public StringSet getIntersectionOf(final Collection<String> alpha, final Collection<String> bravo)
{
String result;
StringSet result;
StringList strings = new StringList(number);
for (int count = 0; count < number; count++)
result = new StringSet();
for (String string : alpha)
{
strings.append(source);
if (bravo.contains(string))
{
result.add(string);
}
}
result = strings.toString();
//
return result;
}
/**
*
* @param target
* @return
*/
public StringSet getUnionOf(final Collection<String> alpha, final Collection<String> 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.
*
@ -116,6 +373,79 @@ public class StringListUtils
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<String>} 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<String> 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}.
*
@ -150,11 +480,11 @@ public class StringListUtils
* @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)
public static String toStringNotNull(final String[] source)
{
String result;
result = toString(strings);
result = toString(source);
if (result == null)
{
@ -170,23 +500,17 @@ public class StringListUtils
* @param strings
* @return
*/
public static String toStringWithBracket(final Collection<String> strings)
public static String toStringWithBracket(final Collection<String> source)
{
String result;
if (strings == null)
if (source == null)
{
result = null;
}
else
{
StringList buffer = new StringList();
buffer.append("[");
buffer.append(toStringWithCommas(strings));
buffer.append("]");
result = buffer.toString();
result = new StringList(source).toStringWithBracket();
}
//
@ -198,23 +522,17 @@ public class StringListUtils
* @param strings
* @return
*/
public static String toStringWithBracket(final String[] strings)
public static String toStringWithBracket(final String[] source)
{
String result;
if (strings == null)
if (source == null)
{
result = null;
}
else
{
StringList merge = new StringList();
merge.append("[");
merge.append(toStringWithCommas(strings));
merge.append("]");
result = merge.toString();
result = new StringList(source).toStringWithBracket();
}
//
@ -226,11 +544,33 @@ public class StringListUtils
* @param strings
* @return
*/
public static String toStringWithBracketNotNull(final Collection<String> strings)
public static String toStringWithBracket(final StringList source)
{
String result;
result = toStringWithBrackets(strings);
if (source == null)
{
result = null;
}
else
{
result = source.toStringWithBracket();
}
//
return result;
}
/**
*
* @param strings
* @return
*/
public static String toStringWithBracketNotNull(final Collection<String> source)
{
String result;
result = toStringWithBracket(source);
if (result == null)
{
@ -246,11 +586,31 @@ public class StringListUtils
* @param strings
* @return
*/
public static String toStringWithBracketNotNull(final String[] strings)
public static String toStringWithBracketNotNull(final String[] source)
{
String result;
result = toStringWithBrackets(strings);
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)
{
@ -276,14 +636,7 @@ public class StringListUtils
}
else
{
StringList buffer = new StringList();
for (String string : source)
{
buffer.append("[").append(string).append("]");
}
result = buffer.toString();
result = new StringList(source).toStringWithBrackets();
}
//
@ -305,14 +658,29 @@ public class StringListUtils
}
else
{
StringList merge = new StringList();
result = new StringList(source).toStringWithBrackets();
}
for (String string : source)
{
merge.append("[").append(string).append("]");
}
//
return result;
}
result = merge.toString();
/**
*
* @param source
* @return
*/
public static String toStringWithBrackets(final StringList source)
{
String result;
if (source == null)
{
result = null;
}
else
{
result = source.toStringWithBrackets();
}
//
@ -334,7 +702,7 @@ public class StringListUtils
}
else
{
result = new StringList(source).toStringSeparatedBy(",");
result = new StringList(source).toStringWithCommas();
}
//
@ -356,7 +724,29 @@ public class StringListUtils
}
else
{
result = new StringList(source).toStringSeparatedBy(",");
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();
}
//
@ -378,7 +768,7 @@ public class StringListUtils
}
else
{
result = new StringList(source).toStringSeparatedBy(", ");
result = new StringList(source).toStringWithFrenchCommas();
}
//
@ -400,7 +790,29 @@ public class StringListUtils
}
else
{
result = new StringList(source).toStringSeparatedBy(", ");
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();
}
//

View file

@ -18,6 +18,7 @@
*/
package fr.devinsy.util.strings;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
@ -38,6 +39,26 @@ public class StringSet extends HashSet<String>
super();
}
/**
* Constructs a list of string of the specified collection, in the order
* they are returned by the collection's iterator.
*
* @param source
*/
public StringSet(final Collection<String> source)
{
super(source.size());
if (source != null)
{
//
for (String string : source)
{
this.add(string);
}
}
}
/**
*
*/
@ -46,23 +67,6 @@ public class StringSet extends HashSet<String>
super(initialCapacity);
}
/**
*
*/
public StringSet(final List<String> source)
{
super();
if (source != null)
{
//
for (String string : source)
{
this.add(string);
}
}
}
/**
*
*/
@ -80,23 +84,6 @@ public class StringSet extends HashSet<String>
}
}
/**
*
*/
public StringSet(final StringList source)
{
super();
if (source != null)
{
//
for (String string : source)
{
this.add(string);
}
}
}
/**
*
*/

View file

@ -18,6 +18,8 @@
*/
package fr.devinsy.util;
import java.util.Iterator;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
@ -26,6 +28,8 @@ import org.junit.Before;
import org.junit.Test;
import fr.devinsy.util.strings.StringList;
import fr.devinsy.util.strings.StringListCharIterator;
import fr.devinsy.util.strings.StringListCharPosition;
/**
*
@ -210,6 +214,42 @@ public class StringListTest
/**
*
*/
@Test
public void testIteratorOfChar01()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abc");
source.append("def");
source.append("ghi");
source.append("jkl");
source.append("mno");
//
Iterator<Character> iterator = source.iteratorOfChar();
for (int index = 0; index < source.length(); index++)
{
StringListCharPosition position = ((StringListCharIterator) iterator).nextPosition();
System.out.println(index + ":" + source.charAt(index) + " " + position.getCharIndex() + " " + position.getStringIndex() + " " + position.getLocalCharIndex());
Assert.assertTrue(iterator.hasNext());
Character character = ((StringListCharIterator) iterator).next();
Assert.assertEquals(character, new Character(source.charAt(index)));
}
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testSubstring01()