Merge pull request #357 from DevFactory/release/Consecutive-Appends-Should-Reuse-fix-1

pmd:ConsecutiveAppendsShouldReuse - Consecutive Appends Should Reuse
This commit is contained in:
Ilkka Seppälä 2016-01-30 14:37:18 +02:00
commit 62b2f8e9a4
3 changed files with 22 additions and 26 deletions

View File

@ -42,29 +42,25 @@ public class Hero {
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("This is a "); sb.append("This is a ")
sb.append(profession); .append(profession)
sb.append(" named "); .append(" named ")
sb.append(name); .append(name);
if (hairColor != null || hairType != null) { if (hairColor != null || hairType != null) {
sb.append(" with "); sb.append(" with ");
if (hairColor != null) { if (hairColor != null) {
sb.append(hairColor); sb.append(hairColor).append(" ");
sb.append(" ");
} }
if (hairType != null) { if (hairType != null) {
sb.append(hairType); sb.append(hairType).append(" ");
sb.append(" ");
} }
sb.append(hairType != HairType.BALD ? "hair" : "head"); sb.append(hairType != HairType.BALD ? "hair" : "head");
} }
if (armor != null) { if (armor != null) {
sb.append(" wearing "); sb.append(" wearing ").append(armor);
sb.append(armor);
} }
if (weapon != null) { if (weapon != null) {
sb.append(" and wielding a "); sb.append(" and wielding a ").append(weapon);
sb.append(weapon);
} }
sb.append("."); sb.append(".");
return sb.toString(); return sb.toString();

View File

@ -63,12 +63,12 @@ public class CustomerTest {
@Test @Test
public void testToString() { public void testToString() {
final StringBuffer buffer = new StringBuffer(); final StringBuffer buffer = new StringBuffer();
buffer.append("Customer{id="); buffer.append("Customer{id=")
buffer.append("" + customer.getId()); .append("" + customer.getId())
buffer.append(", firstName='"); .append(", firstName='")
buffer.append(customer.getFirstName()); .append(customer.getFirstName())
buffer.append("\', lastName='"); .append("\', lastName='")
buffer.append(customer.getLastName() + "\'}"); .append(customer.getLastName() + "\'}");
assertEquals(buffer.toString(), customer.toString()); assertEquals(buffer.toString(), customer.toString());
} }
} }

View File

@ -69,14 +69,14 @@ public class Character {
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("This is a "); sb.append("This is a ")
sb.append(fighterClass != null ? fighterClass : wizardClass); .append(fighterClass != null ? fighterClass : wizardClass)
sb.append(" named "); .append(" named ")
sb.append(name); .append(name)
sb.append(" armed with a "); .append(" armed with a ")
sb.append(weapon != null ? weapon : spell != null ? spell : "with nothing"); .append(weapon != null ? weapon : spell != null ? spell : "with nothing")
sb.append(abilities != null ? (" and wielding " + abilities + " abilities") : ""); .append(abilities != null ? (" and wielding " + abilities + " abilities") : "")
sb.append("."); .append(".");
return sb.toString(); return sb.toString();
} }
} }