Added remove methods and unit tests.

This commit is contained in:
Christian P. MOMON 2021-01-18 19:36:21 +01:00
parent 8da21c24c8
commit a4985cb2b8
2 changed files with 353 additions and 1 deletions

View file

@ -1767,6 +1767,59 @@ public class StringList extends ArrayList<String> implements CharSequence, Appen
return result;
}
/**
* Removes the first.
*
* @return the string list
*/
public StringList removeFirst()
{
StringList result;
if (!isEmpty())
{
this.remove(0);
}
result = this;
//
return result;
}
/**
* Removes the first.
*
* @param count
* the count
* @return the string list
*/
public StringList removeFirst(final int count)
{
StringList result;
int counting;
if (count < 0)
{
counting = 0;
}
else if (count <= size())
{
counting = count;
}
else
{
counting = size();
}
removeRange(0, counting);
result = this;
//
return result;
}
/**
* Removes the last element of the list.
*
@ -1776,7 +1829,7 @@ public class StringList extends ArrayList<String> implements CharSequence, Appen
{
StringList result;
if (this.size() > 0)
if (!isEmpty())
{
this.remove(this.size() - 1);
}
@ -1787,6 +1840,47 @@ public class StringList extends ArrayList<String> implements CharSequence, Appen
return result;
}
/**
* Removes the last.
*
* @param count
* the count
* @return the string list
*/
public StringList removeLast(final int count)
{
StringList result;
int counting;
if (count < 0)
{
counting = 0;
}
else
{
counting = count;
}
boolean ended = false;
while (!ended)
{
if ((counting == 0) || (isEmpty()))
{
ended = true;
}
else
{
this.remove(this.size() - 1);
counting -= 1;
}
}
result = this;
//
return result;
}
/**
* Extends the list copying the last element a number of time.
*

View file

@ -775,6 +775,264 @@ public class StringListTest
this.logger.debug("===== test done.");
}
/**
* Test remove first 01.
*/
@Test
public void testRemoveFirst01()
{
StringList source = new StringList();
source.removeFirst();
Assert.assertTrue(source.isEmpty());
}
/**
* Test remove first 02.
*/
@Test
public void testRemoveFirst02()
{
StringList source = new StringList();
source.append("un");
source.removeFirst();
Assert.assertTrue(source.isEmpty());
}
/**
* Test remove first 03.
*/
@Test
public void testRemoveFirst03()
{
StringList source = new StringList();
source.append("un", "deux");
source.removeFirst();
Assert.assertEquals(1, source.size());
Assert.assertEquals("deux", source.getLast());
}
/**
* Test remove first with count 01.
*/
@Test
public void testRemoveFirstWithCount01()
{
StringList source = new StringList();
source.removeFirst(-10);
Assert.assertTrue(source.isEmpty());
source.removeFirst(-1);
Assert.assertTrue(source.isEmpty());
}
@Test
public void testRemoveFirstWithCount02()
{
StringList source = new StringList();
source.append("un", "deux", "trois");
source.removeFirst(-10);
Assert.assertEquals(3, source.size());
source.removeFirst(-1);
Assert.assertEquals(3, source.size());
}
/**
* Test remove first with count 03.
*/
@Test
public void testRemoveFirstWithCount03()
{
StringList source = new StringList();
source.removeFirst(0);
Assert.assertTrue(source.isEmpty());
}
/**
* Test remove first with count 04.
*/
@Test
public void testRemoveFirstWithCount04()
{
StringList source = new StringList();
source.append("un", "deux", "trois");
source.removeFirst(0);
Assert.assertEquals(3, source.size());
}
/**
* Test remove first with count 05.
*/
@Test
public void testRemoveFirstWithCount05()
{
StringList source = new StringList();
source.removeFirst(1);
Assert.assertTrue(source.isEmpty());
source.removeFirst(10);
Assert.assertTrue(source.isEmpty());
}
/**
* Test remove first with count 06.
*/
@Test
public void testRemoveFirstWithCount06()
{
StringList source = new StringList();
source.append("un", "deux", "trois", "quatre", "cinq");
source.removeFirst(1);
Assert.assertEquals(4, source.size());
source.append("cinq", "six", "sept", "huit", "neuf", "dix");
source.removeFirst(10);
Assert.assertTrue(source.isEmpty());
}
/**
* Test remove last 01.
*/
@Test
public void testRemoveLast01()
{
StringList source = new StringList();
source.removeLast();
Assert.assertTrue(source.isEmpty());
}
/**
* Test remove last 02.
*/
@Test
public void testRemoveLast02()
{
StringList source = new StringList();
source.append("un");
source.removeLast();
Assert.assertTrue(source.isEmpty());
}
/**
* Test remove last 02.
*/
@Test
public void testRemoveLast03()
{
StringList source = new StringList();
source.append("un", "deux");
source.removeLast();
Assert.assertEquals(1, source.size());
Assert.assertEquals("un", source.getLast());
}
/**
* Test remove last with count 01.
*/
@Test
public void testRemoveLastWithCount01()
{
StringList source = new StringList();
source.removeLast(-10);
Assert.assertTrue(source.isEmpty());
source.removeLast(-1);
Assert.assertTrue(source.isEmpty());
}
/**
* Test remove last with count 02.
*/
@Test
public void testRemoveLastWithCount02()
{
StringList source = new StringList();
source.append("un", "deux", "trois");
source.removeLast(-10);
Assert.assertEquals(3, source.size());
source.removeLast(-1);
Assert.assertEquals(3, source.size());
}
/**
* Test remove last with count 03.
*/
@Test
public void testRemoveLastWithCount03()
{
StringList source = new StringList();
source.removeLast(0);
Assert.assertTrue(source.isEmpty());
}
/**
* Test remove last with count 04.
*/
@Test
public void testRemoveLastWithCount04()
{
StringList source = new StringList();
source.append("un", "deux", "trois");
source.removeLast(0);
Assert.assertEquals(3, source.size());
}
/**
* Test remove last with count 05.
*/
@Test
public void testRemoveLastWithCount05()
{
StringList source = new StringList();
source.removeLast(1);
Assert.assertTrue(source.isEmpty());
source.removeLast(10);
Assert.assertTrue(source.isEmpty());
}
/**
* Test remove last with count 06.
*/
@Test
public void testRemoveLastWithCount06()
{
StringList source = new StringList();
source.append("un", "deux", "trois", "quatre", "cinq");
source.removeLast(1);
Assert.assertEquals(4, source.size());
Assert.assertEquals("quatre", source.getLast());
source.append("cinq", "six", "sept", "huit", "neuf", "dix");
source.removeLast(10);
Assert.assertTrue(source.isEmpty());
}
/**
* Test shrink 01.
*/