Skip to content

Commit 3005dcf

Browse files
committed
RecipientInformation: Updates to toString method
Updated toString method to return fields and their values in alphabetical order based on the field name. Also added Javadoc to explain this behaviour and the use of a TreeMap.
1 parent a978de6 commit 3005dcf

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

src/main/java/com/trustly/api/data/request/requestdata/RecipientInformation.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424

2525
package com.trustly.api.data.request.requestdata;
2626

27+
import java.util.Map;
28+
import java.util.TreeMap;
29+
import java.util.stream.Collectors;
30+
2731
import com.google.gson.annotations.SerializedName;
2832

2933
public class RecipientInformation {
@@ -64,27 +68,37 @@ public RecipientInformation setDateOfBirth(final String dateOfBirth) {
6468
return this;
6569
}
6670

71+
/**
72+
* Method for returning a String representation of the
73+
* RecipientInformation object. Used for serialization.
74+
* <p>
75+
* Uses a TreeMap for automatically sorting the object's
76+
* field values in an alphabetical order, which is required
77+
* for generating a valid signature from the serialized string.
78+
*/
6779
@Override
6880
public String toString() {
69-
final StringBuilder stringBuilder = new StringBuilder();
81+
final Map<String, String> attributes = new TreeMap<>();
7082

71-
stringBuilder.append("Partytype").append(partyType);
72-
stringBuilder.append("Firstname").append(firstName);
73-
stringBuilder.append("Lastname").append(lastName);
74-
stringBuilder.append("CountryCode").append(countryCode);
83+
attributes.put("Partytype", partyType);
84+
attributes.put("Firstname", firstName);
85+
attributes.put("Lastname", lastName);
86+
attributes.put("CountryCode", countryCode);
7587

7688
if (customerId != null) {
77-
stringBuilder.append("CustomerID").append(customerId);
89+
attributes.put("CustomerID", customerId);
7890
}
7991

8092
if (address != null) {
81-
stringBuilder.append("Address").append(address);
93+
attributes.put("Address", address);
8294
}
8395

8496
if (dateOfBirth != null) {
85-
stringBuilder.append("DateOfBirth").append(dateOfBirth);
97+
attributes.put("DateOfBirth", dateOfBirth);
8698
}
8799

88-
return stringBuilder.toString();
100+
return attributes.entrySet().stream()
101+
.map(entry -> entry.getKey() + entry.getValue())
102+
.collect(Collectors.joining());
89103
}
90104
}

0 commit comments

Comments
 (0)