0

I'm looking for a design pattern or even advice on some code that I saw the other day. The general structure is this (pseudo code):

public String getUrl(){
Person person= new Person();
StringBuilder builder = new StringBuilder();

if(person.getName() != null){
builder.append(",_name=");
builder.append(person.getName());
}

if(person.getLastName() != null){
builder.append(",_lastName=");
builder.append(person.getName());
}

if(person.getPostCode() != null){
builder.append(",_postCode=");
builder.append(person.getPostCode());
}

// So on and so forth
return builder.toString();

}

Now the problem is that I don't have control over Person class (I'm just given it via an API call). I was thinking to use reflection and a map like so:

Map<String, String> methodNameToUrlParameter; //Pre Build this map with method name and the actual parameter key
Map<String, String> urlParameterToValue;
Method[] methods = person.getMethods();

    for(Method method: methods ){
        String result = (String) method.invoke(person, null);

          if(result != null){
             String urlParam = methodNameToUrlParameter.get(method.getName());
             urlToValue.put(urlParam, result );
          }

}

Then I can go on my merry way. But this doesn't seem too great to me and I don't really know all too much about reflection, any ideas? Remember, I have no control over the Person class and it just has getters since it's immutable.

Thanks.

Edit:

What I am asking is there a better way to represent the flow of logic here with out using a too many if statements that do null checks. Perhaps a design pattern that I do not know about.

2nd Edit: There's maybe like 20 if-null checks, which made things ugly. Is there a way todo it without none?

Sun
  • 2,658
  • 6
  • 28
  • 33
  • 1
    FYI, you haven't asked any question yet. You just said - *any ideas?*. Well, about what? – Rohit Jain Nov 27 '13 at 20:22
  • And your problem is... – Luiggi Mendoza Nov 27 '13 at 20:23
  • "with out using a too many if " - You only have one `if` statement. Are you asking if you can do it with zero `if` statements? – Peter Lawrey Nov 27 '13 at 20:27
  • 1
    [The codereview stackexchange site](codereview.stackexchange.com) sounds more like the place to go. But this is how I would probably do it. If you're sure about what methods are inside it, then reflection is ok. If `person` is a bean, you can use the [Introspector](http://docs.oracle.com/javase/7/docs/api/java/beans/Introspector.html) class too, which gives more information to make sure you're calling a getter method, and for what property you are calling the method for. – Andrew Gies Nov 27 '13 at 20:27

2 Answers2

1

Use either Apache Commons ToStringBuilder or Guava's MoreObjects.ToStringHelper. Or get inspired by them.

facundofarias
  • 2,973
  • 28
  • 27
lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • Thanks! This looks like a good approach, I'm reading through the source as we speak. – Sun Nov 27 '13 at 21:24
1

For a minor change with better readability, you could pull the redundant code into its own method:

void AddField(StringBuilder builder, String value, String fieldName) {
    if (value != null) {
        builder.append(",_");
        builder.append(fieldName);
        builder.append("=");
        builder.append(value);
    }
}

which would simplify your code sample to the following:

public String getUrl(){
    Person person= new Person();
    StringBuilder builder = new StringBuilder();

    AddField(builder, person.getName(), "name");
    AddField(builder, person.getLastName(), "lastName");
    AddField(builder, person.getPostCode(), "postCode");

    // So on and so forth
    return builder.toString();

 }
user1787270
  • 348
  • 3
  • 10