Refactored and added methods.

This commit is contained in:
Christian P. MOMON 2017-05-05 20:07:36 +02:00
parent ee45089189
commit db9220840f
4 changed files with 407 additions and 1725 deletions

View file

@ -18,7 +18,6 @@
*/ */
package fr.devinsy.util.strings; package fr.devinsy.util.strings;
import java.io.IOException;
import java.text.Collator; import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -83,6 +82,23 @@ public class StringList extends ArrayList<String> implements CharSequence
super(initialCapacity); super(initialCapacity);
} }
/**
* Constructs a list of string from an Object array.
*
* @param source
*/
public StringList(final Object... source)
{
super();
if (source != null)
{
ensureCapacity(source.length);
append(source);
}
}
/** /**
* Constructs a list of string from a string array. * Constructs a list of string from a string array.
* *
@ -106,44 +122,20 @@ public class StringList extends ArrayList<String> implements CharSequence
} }
/** /**
* Appends the string of the array argument to this boolean list. * Appends the string of the array argument to this string list.
* *
* @param strings * @param strings
* @return * @return
*/ */
public StringList append(final boolean... items) public StringList append(final boolean... values)
{ {
StringList result; StringList result;
if (items != null) if (values != null)
{ {
for (boolean item : items) for (boolean value : values)
{ {
this.append(item); this.append(value);
}
}
result = this;
//
return result;
}
/**
* Appends the string of the array argument to this byte list.
*
* @param strings
* @return
*/
public StringList append(final byte... items)
{
StringList result;
if (items != null)
{
for (byte item : items)
{
this.append(item);
} }
} }
@ -172,30 +164,6 @@ public class StringList extends ArrayList<String> implements CharSequence
return result; return result;
} }
/**
* Appends the string of the array argument to this char list.
*
* @param strings
* @return
*/
public StringList append(final char... items)
{
StringList result;
if (items != null)
{
for (char item : items)
{
this.append(item);
}
}
result = this;
//
return result;
}
/** /**
* Appends the string of the specified collection, in the order they are * Appends the string of the specified collection, in the order they are
* returned by the collection's iterator. * returned by the collection's iterator.
@ -272,6 +240,30 @@ public class StringList extends ArrayList<String> implements CharSequence
return result; return result;
} }
/**
* Appends the string of the array argument to this string list.
*
* @param values
* @return
*/
public StringList append(final Long... values)
{
StringList result;
if (values != null)
{
for (Long value : values)
{
this.append(value);
}
}
result = this;
//
return result;
}
/** /**
* Appends the string representation of the int argument to this string * Appends the string representation of the int argument to this string
* list. * list.
@ -295,20 +287,20 @@ public class StringList extends ArrayList<String> implements CharSequence
} }
/** /**
* Appends the string of the array argument to this short list. * Appends the string of the array argument to this string list.
* *
* @param strings * @param strings
* @return * @return
*/ */
public StringList append(final short... items) public StringList append(final Object... values)
{ {
StringList result; StringList result;
if (items != null) if (values != null)
{ {
for (short item : items) for (Object value : values)
{ {
this.append(item); this.append(value);
} }
} }
@ -372,7 +364,7 @@ public class StringList extends ArrayList<String> implements CharSequence
{ {
for (String string : strings) for (String string : strings)
{ {
this.add(string); this.append(string);
} }
} }
@ -399,60 +391,6 @@ public class StringList extends ArrayList<String> implements CharSequence
return result; return result;
} }
/**
* Appends the string of the array argument to this boolean list.
*
* @param items
* @return
*/
public StringList appendln(final boolean... items)
{
StringList result;
append(items).appendln();
result = this;
//
return result;
}
/**
* Appends the string of the array argument to this byte list.
*
* @param items
* @return
*/
public StringList appendln(final byte... items)
{
StringList result;
append(items).appendln();
result = this;
//
return result;
}
/**
* Appends the string of the array argument to this char list.
*
* @param strings
* @return
*/
public StringList appendln(final char... items)
{
StringList result;
append(items).appendln();
result = this;
//
return result;
}
/** /**
* Appends the string representation of the char argument to this string * Appends the string representation of the char argument to this string
* list, then append a break line too. * list, then append a break line too.
@ -538,6 +476,24 @@ public class StringList extends ArrayList<String> implements CharSequence
return result; return result;
} }
/**
* Appends the string of the array argument to this string list.
*
* @param values
* @return
*/
public StringList appendln(final Object... values)
{
StringList result;
append(values).appendln();
result = this;
//
return result;
}
/** /**
* Appendln. * Appendln.
* *
@ -555,24 +511,6 @@ public class StringList extends ArrayList<String> implements CharSequence
return result; return result;
} }
/**
* Appends the string of the array argument to this short list.
*
* @param strings
* @return
*/
public StringList appendln(final short... items)
{
StringList result;
append(items).appendln();
result = this;
//
return result;
}
/** /**
* Appendln. * Appendln.
* *
@ -1797,35 +1735,4 @@ public class StringList extends ArrayList<String> implements CharSequence
return result; return result;
} }
/**
* Writes the strings of this list into a {@code PrintWriter}.
*
* @param out
* The {@code PrintWriter} where to write.
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public void writeInto(final java.io.PrintWriter out) throws IOException
{
for (String string : this)
{
out.write(string);
}
}
/**
* Writes the strings of this list into a {@code Writer}.
*
* @param out
* The {@code Writer} where to write.
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public void writeInto(final java.io.Writer out) throws IOException
{
for (String string : this)
{
out.write(string);
}
}
} }

View file

@ -1004,6 +1004,266 @@ public class StringsUtils
return result; return result;
} }
public static StringList toStringList(final boolean... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (boolean value : source)
{
result.append(value);
}
}
//
return result;
}
public static StringList toStringList(final Boolean... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (Boolean value : source)
{
result.append(String.valueOf(value));
}
}
//
return result;
}
public static StringList toStringList(final byte... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (byte value : source)
{
result.append(value);
}
}
//
return result;
}
public static StringList toStringList(final Byte... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (Byte value : source)
{
result.add(String.valueOf(value));
}
}
//
return result;
}
public static StringList toStringList(final char... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (char value : source)
{
result.append(value);
}
}
//
return result;
}
public static StringList toStringList(final double... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (double value : source)
{
result.append(value);
}
}
//
return result;
}
public static StringList toStringList(final Double... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (Double value : source)
{
result.add(String.valueOf(value));
}
}
//
return result;
}
public static StringList toStringList(final float... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (float value : source)
{
result.append(value);
}
}
//
return result;
}
public static StringList toStringList(final Float... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (Float value : source)
{
result.add(String.valueOf(value));
}
}
//
return result;
}
public static StringList toStringList(final long... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (long value : source)
{
result.append(value);
}
}
//
return result;
}
public static StringList toStringList(final Long... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (Long value : source)
{
result.add(String.valueOf(value));
}
}
//
return result;
}
public static StringList toStringList(final short... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (short value : source)
{
result.append(value);
}
}
//
return result;
}
public static StringList toStringList(final Short... source)
{
StringList result;
result = new StringList();
if (source != null)
{
result.ensureCapacity(source.length);
for (Short value : source)
{
result.add(String.valueOf(value));
}
}
//
return result;
}
/** /**
* Concatenates the string from an array to a string. * Concatenates the string from an array to a string.
* *
@ -1520,4 +1780,42 @@ public class StringsUtils
source.addAll(values); source.addAll(values);
} }
} }
/**
* Writes the strings of this list into a {@code PrintWriter}.
*
* @param out
* The {@code PrintWriter} where to write.
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void writeTo(final StringList source, final java.io.PrintWriter out) throws IOException
{
if (source != null)
{
for (String string : source)
{
out.write(string);
}
}
}
/**
* Writes the strings of this list into a {@code Writer}.
*
* @param out
* The {@code Writer} where to write.
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void writeTo(final StringList source, final java.io.Writer out) throws IOException
{
if (source != null)
{
for (String string : source)
{
out.write(string);
}
}
}
} }

File diff suppressed because it is too large Load diff

View file

@ -54,11 +54,11 @@ public class StringsUtilsTest
@Test @Test
public void testContainsBlank01() public void testContainsBlank01()
{ {
Assert.assertFalse(StringstUtils.containsBlank((Collection<String>) null)); Assert.assertFalse(StringsUtils.containsBlank((Collection<String>) null));
Assert.assertFalse(StringstUtils.containsBlank(new StringList("aaa", "bbb", "ccc"))); Assert.assertFalse(StringsUtils.containsBlank(new StringList("aaa", "bbb", "ccc")));
Assert.assertTrue(StringstUtils.containsBlank(new StringList("aaa", null, "ccc"))); Assert.assertTrue(StringsUtils.containsBlank(new StringList("aaa", null, "ccc")));
Assert.assertTrue(StringstUtils.containsBlank(new StringList("aaa", "", "ccc"))); Assert.assertTrue(StringsUtils.containsBlank(new StringList("aaa", "", "ccc")));
Assert.assertTrue(StringstUtils.containsBlank(new StringList("aaa", " ", "ccc"))); Assert.assertTrue(StringsUtils.containsBlank(new StringList("aaa", " ", "ccc")));
} }
/** /**
@ -67,11 +67,11 @@ public class StringsUtilsTest
@Test @Test
public void testContainsBlank02() public void testContainsBlank02()
{ {
Assert.assertFalse(StringstUtils.containsBlank((String[]) null)); Assert.assertFalse(StringsUtils.containsBlank((String[]) null));
Assert.assertFalse(StringstUtils.containsBlank("aaa", "bbb", "ccc")); Assert.assertFalse(StringsUtils.containsBlank("aaa", "bbb", "ccc"));
Assert.assertTrue(StringstUtils.containsBlank("aaa", null, "ccc")); Assert.assertTrue(StringsUtils.containsBlank("aaa", null, "ccc"));
Assert.assertTrue(StringstUtils.containsBlank("aaa", "", "ccc")); Assert.assertTrue(StringsUtils.containsBlank("aaa", "", "ccc"));
Assert.assertTrue(StringstUtils.containsBlank("aaa", " ", "ccc")); Assert.assertTrue(StringsUtils.containsBlank("aaa", " ", "ccc"));
} }
/** /**
@ -80,18 +80,18 @@ public class StringsUtilsTest
@Test @Test
public void testContainsBlank03() public void testContainsBlank03()
{ {
Assert.assertFalse(StringstUtils.containsBlank((ArrayList<String>) null)); Assert.assertFalse(StringsUtils.containsBlank((ArrayList<String>) null));
ArrayList<String> source = new ArrayList<String>(); ArrayList<String> source = new ArrayList<String>();
source.add("aaa"); source.add("aaa");
source.add("bbb"); source.add("bbb");
source.add("ccc"); source.add("ccc");
Assert.assertFalse(StringstUtils.containsBlank(source)); Assert.assertFalse(StringsUtils.containsBlank(source));
source.set(1, null); source.set(1, null);
Assert.assertTrue(StringstUtils.containsBlank(source)); Assert.assertTrue(StringsUtils.containsBlank(source));
source.set(1, ""); source.set(1, "");
Assert.assertTrue(StringstUtils.containsBlank(source)); Assert.assertTrue(StringsUtils.containsBlank(source));
source.set(1, " "); source.set(1, " ");
Assert.assertTrue(StringstUtils.containsBlank(source)); Assert.assertTrue(StringsUtils.containsBlank(source));
} }
/** /**
@ -100,10 +100,10 @@ public class StringsUtilsTest
@Test @Test
public void testContainsEmpty01() public void testContainsEmpty01()
{ {
Assert.assertFalse(StringstUtils.containsEmpty((Collection<String>) null)); Assert.assertFalse(StringsUtils.containsEmpty((Collection<String>) null));
Assert.assertFalse(StringstUtils.containsEmpty(new StringList("aaa", "bbb", "ccc"))); Assert.assertFalse(StringsUtils.containsEmpty(new StringList("aaa", "bbb", "ccc")));
Assert.assertTrue(StringstUtils.containsEmpty(new StringList("aaa", null, "ccc"))); Assert.assertTrue(StringsUtils.containsEmpty(new StringList("aaa", null, "ccc")));
Assert.assertTrue(StringstUtils.containsEmpty(new StringList("aaa", "", "ccc"))); Assert.assertTrue(StringsUtils.containsEmpty(new StringList("aaa", "", "ccc")));
} }
/** /**
@ -112,10 +112,10 @@ public class StringsUtilsTest
@Test @Test
public void testContainsEmpty02() public void testContainsEmpty02()
{ {
Assert.assertFalse(StringstUtils.containsEmpty((String[]) null)); Assert.assertFalse(StringsUtils.containsEmpty((String[]) null));
Assert.assertFalse(StringstUtils.containsEmpty("aaa", "bbb", "ccc")); Assert.assertFalse(StringsUtils.containsEmpty("aaa", "bbb", "ccc"));
Assert.assertTrue(StringstUtils.containsEmpty("aaa", null, "ccc")); Assert.assertTrue(StringsUtils.containsEmpty("aaa", null, "ccc"));
Assert.assertTrue(StringstUtils.containsEmpty("aaa", "", "ccc")); Assert.assertTrue(StringsUtils.containsEmpty("aaa", "", "ccc"));
} }
/** /**
@ -124,16 +124,16 @@ public class StringsUtilsTest
@Test @Test
public void testContainsEmpty03() public void testContainsEmpty03()
{ {
Assert.assertFalse(StringstUtils.containsEmpty((ArrayList<String>) null)); Assert.assertFalse(StringsUtils.containsEmpty((ArrayList<String>) null));
ArrayList<String> source = new ArrayList<String>(); ArrayList<String> source = new ArrayList<String>();
source.add("aaa"); source.add("aaa");
source.add("bbb"); source.add("bbb");
source.add("ccc"); source.add("ccc");
Assert.assertFalse(StringstUtils.containsEmpty(source)); Assert.assertFalse(StringsUtils.containsEmpty(source));
source.set(1, null); source.set(1, null);
Assert.assertTrue(StringstUtils.containsEmpty(source)); Assert.assertTrue(StringsUtils.containsEmpty(source));
source.set(1, ""); source.set(1, "");
Assert.assertTrue(StringstUtils.containsEmpty(source)); Assert.assertTrue(StringsUtils.containsEmpty(source));
} }
/** /**
@ -142,9 +142,9 @@ public class StringsUtilsTest
@Test @Test
public void testContainsNull01() public void testContainsNull01()
{ {
Assert.assertFalse(StringstUtils.containsNull((Collection<String>) null)); Assert.assertFalse(StringsUtils.containsNull((Collection<String>) null));
Assert.assertFalse(StringstUtils.containsNull(new StringList("aaa", "bbb", "ccc"))); Assert.assertFalse(StringsUtils.containsNull(new StringList("aaa", "bbb", "ccc")));
Assert.assertTrue(StringstUtils.containsNull(new StringList("aaa", null, "ccc"))); Assert.assertTrue(StringsUtils.containsNull(new StringList("aaa", null, "ccc")));
} }
/** /**
@ -153,9 +153,9 @@ public class StringsUtilsTest
@Test @Test
public void testContainsNull02() public void testContainsNull02()
{ {
Assert.assertFalse(StringstUtils.containsNull((String[]) null)); Assert.assertFalse(StringsUtils.containsNull((String[]) null));
Assert.assertFalse(StringstUtils.containsNull("aaa", "bbb", "ccc")); Assert.assertFalse(StringsUtils.containsNull("aaa", "bbb", "ccc"));
Assert.assertTrue(StringstUtils.containsNull("aaa", null, "ccc")); Assert.assertTrue(StringsUtils.containsNull("aaa", null, "ccc"));
} }
/** /**
@ -164,14 +164,14 @@ public class StringsUtilsTest
@Test @Test
public void testContainsNull03() public void testContainsNull03()
{ {
Assert.assertFalse(StringstUtils.containsNull((ArrayList<String>) null)); Assert.assertFalse(StringsUtils.containsNull((ArrayList<String>) null));
ArrayList<String> source = new ArrayList<String>(); ArrayList<String> source = new ArrayList<String>();
source.add("aaa"); source.add("aaa");
source.add("bbb"); source.add("bbb");
source.add("ccc"); source.add("ccc");
Assert.assertFalse(StringstUtils.containsNull(source)); Assert.assertFalse(StringsUtils.containsNull(source));
source.set(1, null); source.set(1, null);
Assert.assertTrue(StringstUtils.containsNull(source)); Assert.assertTrue(StringsUtils.containsNull(source));
} }
/** /**
@ -181,13 +181,13 @@ public class StringsUtilsTest
public void testFindSmallest01() public void testFindSmallest01()
{ {
// TODO // TODO
Assert.assertFalse(StringstUtils.containsNull((ArrayList<String>) null)); Assert.assertFalse(StringsUtils.containsNull((ArrayList<String>) null));
ArrayList<String> source = new ArrayList<String>(); ArrayList<String> source = new ArrayList<String>();
source.add("aaa"); source.add("aaa");
source.add("bbb"); source.add("bbb");
source.add("ccc"); source.add("ccc");
Assert.assertFalse(StringstUtils.containsNull(source)); Assert.assertFalse(StringsUtils.containsNull(source));
source.set(1, null); source.set(1, null);
Assert.assertTrue(StringstUtils.containsNull(source)); Assert.assertTrue(StringsUtils.containsNull(source));
} }
} }