Set StringList as CharSequence. Add StringList tests.

This commit is contained in:
Christian P. MOMON 2013-08-12 14:28:56 +02:00
parent 587de36df6
commit 620399fd97
5 changed files with 438 additions and 6 deletions

View file

@ -83,7 +83,7 @@ org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_annotation_de
org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_constant_header=true org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_constant_header=true
org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_declaration_header=true org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_declaration_header=true
org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header=true org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header=true
org.eclipse.jdt.core.formatter.indent_breaks_compare_to_cases=true org.eclipse.jdt.core.formatter.indent_breaks_compare_to_cases=false
org.eclipse.jdt.core.formatter.indent_empty_lines=false org.eclipse.jdt.core.formatter.indent_empty_lines=false
org.eclipse.jdt.core.formatter.indent_statements_compare_to_block=true org.eclipse.jdt.core.formatter.indent_statements_compare_to_block=true
org.eclipse.jdt.core.formatter.indent_statements_compare_to_body=true org.eclipse.jdt.core.formatter.indent_statements_compare_to_body=true

View file

@ -13,9 +13,11 @@ import java.util.ArrayList;
* possible to build a string without any copy. The goal is to optimize the * possible to build a string without any copy. The goal is to optimize the
* building of strings where they are lot of concatenation action. * building of strings where they are lot of concatenation action.
*/ */
public class StringList extends ArrayList<String> public class StringList extends ArrayList<String> implements CharSequence
{ {
private static final long serialVersionUID = -1154185934830213732L; private static final long serialVersionUID = -1154185934830213732L;
public static final String LINE_SEPARATOR = "\n"; public static final String LINE_SEPARATOR = "\n";
/** /**
@ -288,6 +290,24 @@ public class StringList extends ArrayList<String>
return (result); return (result);
} }
/**
*
*/
@Override
public char charAt(final int index)
{
char result;
//
StringListCharPosition position = indexOf(index);
//
result = get(position.getStringIndex()).charAt(position.getLocalCharIndex());
//
return result;
}
/** /**
* *
*/ */
@ -303,7 +323,47 @@ public class StringList extends ArrayList<String>
/** /**
* *
* @param index
* @return
*/ */
public StringListCharPosition indexOf(final int index)
{
StringListCharPosition result;
if ((index < 0) || (index >= length()))
{
throw new StringIndexOutOfBoundsException(index);
}
else
{
boolean ended = false;
int stringIndex = 0;
int currentLength = 0;
result = null;
while (!ended)
{
if (index < currentLength + get(stringIndex).length())
{
ended = true;
result = new StringListCharPosition(index, stringIndex, index - currentLength);
}
else
{
stringIndex += 1;
currentLength += get(stringIndex).length();
}
}
}
//
return result;
}
/**
*
*/
@Override
public int length() public int length()
{ {
int result = 0; int result = 0;
@ -353,6 +413,91 @@ public class StringList extends ArrayList<String>
return (result); return (result);
} }
/**
*
*/
@Override
public CharSequence subSequence(final int start, final int end)
{
StringList result;
result = substring(start, end);
//
return result;
}
/**
*
* @param start
* @param end
* @return
*/
public StringList substring(final int beginIndex, final int endIndex)
{
StringList result;
if (beginIndex < 0)
{
throw new StringIndexOutOfBoundsException(beginIndex);
}
else if (endIndex > length())
{
throw new StringIndexOutOfBoundsException(endIndex);
}
else if (beginIndex > endIndex)
{
throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
}
else if (beginIndex == endIndex)
{
result = new StringList();
}
else
{
//
result = new StringList();
//
StringListCharPosition startPosition = indexOf(beginIndex);
StringListCharPosition endPosition = indexOf(endIndex);
//
if (startPosition.getStringIndex() == endPosition.getStringIndex())
{
String source = get(startPosition.getStringIndex());
String target = source.substring(startPosition.getLocalCharIndex(), endPosition.getLocalCharIndex());
result.append(target);
}
else
{
//
{
String source = get(startPosition.getStringIndex());
String target = source.substring(startPosition.getLocalCharIndex());
result.append(target);
}
//
for (int stringIndex = startPosition.getStringIndex() + 1; stringIndex < endPosition.getStringIndex(); stringIndex++)
{
String target = get(stringIndex);
result.append(target);
}
//
{
String source = get(endPosition.getStringIndex());
String target = source.substring(0, endPosition.getLocalCharIndex());
result.append(target);
}
}
}
//
return result;
}
/** /**
* *
*/ */
@ -361,7 +506,7 @@ public class StringList extends ArrayList<String>
{ {
String result; String result;
StringBuffer buffer = new StringBuffer(this.length()); StringBuffer buffer = new StringBuffer(length());
for (String string : this) for (String string : this)
{ {

View file

@ -0,0 +1,55 @@
/**
* @author Christian Momon, 2013.
* This file is free software under the terms of the GNU Library General Public License
* as published by the Free Software Foundation version 2 or any later version.
*/
package fr.devinsy.util;
/**
* This class manages a char position in a StringList object.
*
* @author Christian P. Momon
*/
public class StringListCharPosition
{
private int charIndex;
private int stringIndex;
private int localCharIndex;
public StringListCharPosition(final int index, final int stringIndex, final int localIndex)
{
this.charIndex = index;
this.stringIndex = stringIndex;
this.localCharIndex = localIndex;
}
public int getCharIndex()
{
return charIndex;
}
public int getLocalCharIndex()
{
return localCharIndex;
}
public int getStringIndex()
{
return stringIndex;
}
public void setCharIndex(final int charIndex)
{
this.charIndex = charIndex;
}
public void setLocalCharIndex(final int localCharIndex)
{
this.localCharIndex = localCharIndex;
}
public void setStringIndex(final int stringIndex)
{
this.stringIndex = stringIndex;
}
}

View file

@ -0,0 +1,235 @@
package fr.devinsy.util;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author Christian P. Momon
*/
public class StringListTest
{
static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(StringListTest.class);
/**
*
*/
@Before
public void before()
{
BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.ERROR);
}
/**
*
*/
@Test
public void testCharAt01()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abcdefghijklm");
//
char target = source.charAt(0);
Assert.assertEquals(target, 'a');
//
target = source.charAt(4);
Assert.assertEquals(target, 'e');
//
target = source.charAt(11);
Assert.assertEquals(target, 'l');
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testCharAt02()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abc");
source.append("def");
source.append("ghi");
source.append("jkl");
source.append("mno");
//
char target = source.charAt(0);
Assert.assertEquals('a', target);
//
target = source.charAt(4);
Assert.assertEquals('e', target);
//
target = source.charAt(11);
Assert.assertEquals('l', target);
//
logger.debug("===== test done.");
}
/**
*
*/
@Test(expected = IndexOutOfBoundsException.class)
public void testCharAtException01()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abcdefghijklm");
//
char target = source.charAt(-2);
Assert.assertEquals('a', target);
//
logger.debug("===== test done.");
}
/**
*
*/
@Test(expected = IndexOutOfBoundsException.class)
public void testCharAtException02()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abcdefghijklm");
//
char target = source.charAt(100);
Assert.assertEquals('a', target);
//
logger.debug("===== test done.");
}
/**
*
*/
@Test(expected = IndexOutOfBoundsException.class)
public void testCharAtException03()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abcdefghijklm");
//
char target = source.charAt(source.length());
Assert.assertEquals('a', target);
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testSubstring01()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("hamburger");
//
StringList target = source.substring(4, 8);
Assert.assertEquals("urge", target.toString());
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testSubstring02()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("ham").append("bur").append("ger");
//
StringList target = source.substring(4, 8);
Assert.assertEquals("urge", target.toString());
//
logger.debug("===== test done.");
}
/**
*/
@Test
public void testSubstring03()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("smiles");
//
StringList target = source.substring(1, 5);
Assert.assertEquals("mile", target.toString());
//
logger.debug("===== test done.");
}
/**
*/
@Test
public void testSubstring04()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("sm").append("il").append("es");
//
StringList target = source.substring(1, 5);
Assert.assertEquals("mile", target.toString());
//
logger.debug("===== test done.");
}
}

View file

@ -5,9 +5,6 @@ import org.junit.Test;
public class Foo2Test public class Foo2Test
{ {
// static private final Logger logger =
// LoggerFactory.getLogger(PdfGenerationAmqpServiceInjectedTest.class);
/** /**
* *
*/ */