2

I need to create a string which becomes an input to a SHA 256 function to generate its hash equivalent. The string is created by concatenating several variables as below:

String strRequest = "";

strRequest = request_passphrase.concat("access_code=").concat(access_code).concat("amount=").concat(amount).concat("command=").concat(mode).concat("currency=").concat(currency).concat("merchant_identifier=").concat(merchant_identifier).concat(request_passphrase);
if(strRequest!="" || !strRequest.isEmpty()) {
  signature = sha256(strRequest);
}

What should be the best way to create an if-else to drop concatenation for a variable which is null.

For ex. if access_code is Null or empty, then the string will be request_passphrase.concat("amount=").concat(amount). and so on.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
user2967948
  • 529
  • 2
  • 8
  • 23

2 Answers2

3

Depending on your context, the reasonable answer could be to organize your data differently:

request_passphrase.concat("access_code=").concat(access_code)

Instead of having a flat list of fields (or worse: method parameters), you could pass some sort of Map<String, String> - where the key denotes the "field name", like ("access_code"); and the map value represents, well, the corresponding value. And if that value is null, you know to not append it.

Then you can simply iterate that map object, pull key/value pairs and append them to that string. Maybe you need an additional list that tells you the order in which the map keys should be iterated.

As said: these are options. In order to make a clear decision, one should understand more of the context; the underlying "data model"; other requirements, etc. The one thing that is definitely sure: if/else chains are never a good idea.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I believe that your solution is very good (upvoted). I do understand the problem and do not know how to get into Optional variable's _name_. Depending on what the OP wants to do a `Map` or a `Map>` may be best fits for him/her. – Grzegorz Górkiewicz Feb 26 '17 at 19:03
  • On the other hand, the java fathers say: optionals should only be used as return type of methods ( see http://stackoverflow.com/questions/31922866/why-should-java-8s-optional-not-be-used-in-arguments for example ). – GhostCat Feb 26 '17 at 19:25
3

Wrap strings in Java 8 Optional:

Optional<String> accessCode = Optional.ofNullable("access");
Optional<String> addressCode = Optional.ofNullable(null);

System.out.println(accessCode.orElse("It was null."));
System.out.println(addressCode.orElse("It was null."));

In your particular case "It was null." should be replaced with an empty string.

Besides. Use an instance of StringBuilder to minimizeString constructor calls.
Typical usage:

StringBuilder outputBuilder = new StringBuilder("");
while (condition) {
    outputBuilder.append(" another chunk ");
}
outputBuilder.append("end.");
String outputString = outputBuilder.toString();

And besides, you can filter out "nullable" parameters with a Map<String, Optional<String>>.
This would be a very good idea.

Grzegorz Górkiewicz
  • 4,496
  • 4
  • 22
  • 38
  • Depending on the number of strings, concat() might be better than StringBuilder ... see http://stackoverflow.com/questions/47605/string-concatenation-concat-vs-operator – GhostCat Feb 26 '17 at 18:51
  • @GhostCat really that was the worst thing you could say about this answer? if there are only two strings then it's going to be fast either way. – Patrick Parker Feb 26 '17 at 19:43
  • @PatrickParker What makes you think so; you are not a moderator; so you can't see the comments that went forth and back between me and Greg already ;-) ... and hint: if I do complain about the quality of an answer, then people notice. Here, I even upvoted. I just wanted to point out that things are not that simple performance wise ;-) – GhostCat Feb 26 '17 at 19:45
  • @GhostCat what I mean to say is, I can see a lot of bad things with this answer. But that is not one of them. – Patrick Parker Feb 26 '17 at 19:50
  • 1
    You can always step in, edit, or add a specific comment. I have made the experience more than once that *constructive* critique is well appreciated. Esp. as most people understand that fixing problems might lead to upvotes. – GhostCat Feb 26 '17 at 19:54