Lombok: @setter java.lang.String trim()

Created on 7 Jun 2018  路  6Comments  路  Source: projectlombok/lombok

like this :
Can you provide such a method

StringUtils.trimToNull(null) = null
StringUtils.trimToNull("") = null
StringUtils.trimToNull(" ") = null
StringUtils.trimToNull("abc") = "abc"
StringUtils.trimToNull(" abc ") = "abc"

org.apache.commons.lang3.StringUtils#trimToNull

public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}

public void setUsername(String username) {
this.username = StringUtils.trimToNull(username);
}

Most helpful comment

Believe or not this is a common case, would be great have the option of trim the string in the @Data or @Setter.

All 6 comments

That's where more specific cases should be handled by yourself.

Myself I would prefer to use or just allow an empty string instead or even an optional of String. Why not even use the Option class from Vavr.

I hope you see my point. You have so much opinions in the java community it would be difficult or impossible to please everyone.

Believe or not this is a common case, would be great have the option of trim the string in the @Data or @Setter.

If this is a "common case" is more an individual point of perspective imo.
Personally I can't remember to have seen this before.

And this would only work for String arguments.
Therefore I would suggest more a specialized annotation like @StringSetter which could handle more string specific behaviour.
@StringSetter(trim=true, convertion=uppercase|lowercase|none, maxLength=positiveNumber|unset)

  • trim: trim a non null string (like you suggested)
  • convertion: nonNullValue.toUpperCase(), nonNullValue.toLowerCase()
  • maxLength: cut after the specified length or leave it as it is if not specified

The @StringSetter option sounds amazing. About my point of view, you not always have the option to edit or optimize your data sources, and when the DB admin define the data as fixed char instead or varchar, and other similar cases, you need to do the setters at hand with the boilerplate code, or use reflections and the performance impoverishment of that. Your option make my life a lot easier.

May be having property for Setter (and @Data) like lombok.setter.trimString, default false will support as is, activated true as needed

Hi, is this ticket lost?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lombokissues picture lombokissues  路  61Comments

rspilker picture rspilker  路  98Comments

Krasnyanskiy picture Krasnyanskiy  路  39Comments

xiaolongzuo picture xiaolongzuo  路  32Comments

gudenau picture gudenau  路  47Comments