Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
[427909]: Expose specialized StringConcatenation append methods
Browse files Browse the repository at this point in the history
Current append(Object) and related methods need perform
instanceof checks to identify the type of object being passed
in -- leading to simple string literals to need two checks
and a call to String.toString().

Provide overloaded versions of append() which take String,
StringConcatenation or StringConcatenationClient, which allows
the compiler to use them directly when the callsite can be statically
determined to pass one of these types.

Signed-off-by: Robert Varga <nite@hq.sk>
  • Loading branch information
rovarga committed Nov 22, 2016
1 parent 2da3759 commit da32a7d
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 5 deletions.
Expand Up @@ -11,6 +11,7 @@
import static org.junit.Assert.assertSame;

import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtend2.lib.StringConcatenationClient;
import org.junit.Test;

public class StringConcatenationTest {
Expand Down Expand Up @@ -44,11 +45,38 @@ public void testEmptyDelimiter() {
}

@Test
public void testAppendNull() {
public void testAppendObjectNull() {
final StringConcatenation c = new StringConcatenation();
c.append(null);
c.append(null, "");
c.append(null, " ");
c.append((Object)null);
c.append((Object)null, "");
c.append((Object)null, " ");
assertEquals("", c.toString());
}

@Test
public void testAppendStringNull() {
final StringConcatenation c = new StringConcatenation();
c.append((String)null);
c.append((String)null, "");
c.append((String)null, " ");
assertEquals("", c.toString());
}

@Test
public void testAppendStringConcatNull() {
final StringConcatenation c = new StringConcatenation();
c.append((StringConcatenation)null);
c.append((StringConcatenation)null, "");
c.append((StringConcatenation)null, " ");
assertEquals("", c.toString());
}

@Test
public void testAppendStringConcatClientNull() {
final StringConcatenation c = new StringConcatenation();
c.append((StringConcatenationClient)null);
c.append((StringConcatenationClient)null, "");
c.append((StringConcatenationClient)null, " ");
assertEquals("", c.toString());
}

Expand All @@ -70,7 +98,7 @@ public void testAppendWithNullIndent() {
@Test(expected=NullPointerException.class)
public void testAppendNullWithNullIndent() {
final StringConcatenation c = new StringConcatenation();
c.append(null, null);
c.append((Object)null, null);
}

@Test
Expand All @@ -82,6 +110,15 @@ public void testStringConcat() {
assertEquals("abc", c.toString());
}

@Test
public void testMaskedStringConcat() {
final StringConcatenation c = new StringConcatenation();
c.append((Object)"a");
c.append((Object)"b");
c.append((Object)"c");
assertEquals("abc", c.toString());
}

@Test
public void testMixedConcat() {
final StringConcatenation c = new StringConcatenation();
Expand Down Expand Up @@ -123,6 +160,15 @@ public void testIndentConcat() {
assertEquals("a\n b\n c\n d", c.toString());
}

@Test
public void testMaskedIndentConcat() {
final StringConcatenation c = new StringConcatenation();
c.append((Object)"a\n", " ");
c.append((Object)"b\r", " ");
c.append((Object)"c\nd", " ");
assertEquals("a\n b\n c\n d", c.toString());
}

@Test
public void testObjectIndentConcat() {
final StringConcatenation c = new StringConcatenation();
Expand Down Expand Up @@ -202,6 +248,28 @@ public void testAppendEmptyConcatSeparator() {
assertEquals("", c.toString());
}

@Test
public void testAppendMaskedConcat() {
final StringConcatenation toAppend = new StringConcatenation();
toAppend.append("a\n");
toAppend.append("b");

final StringConcatenation c = new StringConcatenation();
c.append((Object)toAppend);
assertEquals("a\nb", c.toString());
}

@Test
public void testAppendMaskedIndentConcat() {
final StringConcatenation toAppend = new StringConcatenation();
toAppend.append("a\n");
toAppend.append("b");

final StringConcatenation c = new StringConcatenation();
c.append((Object)toAppend, " ");
assertEquals("a\n b", c.toString());
}

@Test
public void testAppendImmediateSimple() {
final StringConcatenation c = new StringConcatenation();
Expand Down
Expand Up @@ -109,6 +109,44 @@ public void append(Object object) {
}

/**
* Append the given string to this sequence. Does nothing if the string is <code>null</code>.
*
* @param str
* the to-be-appended string.
* @since 2.11
*/
public void append(String str) {
if (str != null)
append(str, segments.size());
}

/**
* Append the contents of a given StringConcatenation to this sequence. Does nothing
* if the concatenation is <code>null</code>.
*
* @param concat
* the to-be-appended StringConcatenation.
* @since 2.11
*/
public void append(StringConcatenation concat) {
if (concat != null)
appendSegments(segments.size(), concat.getSignificantContent(), concat.lineDelimiter);
}

/**
* Append the contents of a given StringConcatenationClient to this sequence. Does nothing
* if the argument is <code>null</code>.
*
* @param client
* the to-be-appended StringConcatenationClient.
* @since 2.11
*/
public void append(StringConcatenationClient client) {
if (client != null)
client.appendTo(new SimpleTarget(this, segments.size()));
}

/*
* Add the string representation of the given object to this sequence at the given index. Does nothing if the object
* is <code>null</code>.
*
Expand Down Expand Up @@ -158,6 +196,59 @@ public void append(Object object, String indentation) {
append(object, indentation, segments.size());
}

/**
* Add the given string to this sequence. The given indentation will be prepended to
* each line except the first one if the object has a multi-line string representation.
*
* @param str
* the appended string.
* @param indentation
* the indentation string that should be prepended. May not be <code>null</code>.
* @since 2.11
*/
public void append(String str, String indentation) {
if (indentation.isEmpty()) {
append(str);
} else if (str != null)
append(indentation, str, segments.size());
}

/**
* Append the contents of a given StringConcatenation to this sequence. Does nothing
* if the concatenation is <code>null</code>. The given indentation will be prepended to each line except
* the first one.
*
* @param concat
* the to-be-appended StringConcatenation.
* @param indentation
* the indentation string that should be prepended. May not be <code>null</code>.
* @since 2.11
*/
public void append(StringConcatenation concat, String indentation) {
if (indentation.isEmpty()) {
append(concat);
} else if (concat != null)
appendSegments(indentation, segments.size(), concat.getSignificantContent(), concat.lineDelimiter);
}

/**
* Append the contents of a given StringConcatenationClient to this sequence. Does nothing
* if that argument is <code>null</code>. The given indentation will be prepended to each line except
* the first one.
*
* @param client
* the to-be-appended StringConcatenationClient.
* @param indentation
* the indentation string that should be prepended. May not be <code>null</code>.
* @since 2.11
*/
public void append(StringConcatenationClient client, String indentation) {
if (indentation.isEmpty()) {
append(client);
} else if (client != null)
client.appendTo(new IndentedTarget(this, indentation, segments.size()));
}

/**
* Add the string representation of the given object to this sequence at the given index. The given indentation will
* be prepended to each line except the first one if the object has a multi-line string representation.
Expand Down

0 comments on commit da32a7d

Please sign in to comment.