You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Ivan Hristov (JIRA)" <ji...@apache.org> on 2011/04/20 10:36:06 UTC

[jira] [Updated] (LANG-694) ObjectUtils.defaultIfNotNull

     [ https://issues.apache.org/jira/browse/LANG-694?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ivan Hristov updated LANG-694:
------------------------------

    Description: 
Currently there is a defaultIfNull method in ObjectUtils, which is a great one, but it will be nice to have an additional method called defaultIfNotNull which returns null if the first parameter is null or the second parameter if the object is not null. This is quite helpful and avoids an if statement for cases when there is a wrapper object which first need to be checked if it's null and then information could be taken from it. Example:

public class MyNiceWrapper {
private Date wrappedDate;
public Date getWrappedDate() {
return wrappedDate;
}
...
}

MyNiceWrapper myNiceWrapperObject = someNiceMethodWhichReturnsMyNiceWrapper(...);

ObjectUtils.defaultIfNotNull(myNiceWrapperObject, anotherObject);



For the moment I have to perform a null comparison on my own,e.g., 

if (myNiceWrapperObject == null) {
return null;
}

return anotherObject;




  was:
Currently there is a defaultIfNull method in ObjectUtils, which is a great one, but it will be nice to have an additional method called defaultIfNotNull which returns null if the first parameter is null or the second parameter if the object is not null. This is quite helpful and avoids an if statement for cases when there is a wrapper object which first need to be checked if it's null and then information could be taken from it. Example:

public class MyNiceWrapper {
private Date wrappedDate;
public Date getWrappedDate() {
return wrappedDate;
}
...
}

MyNiceWrapper myNiceWrapperObject = someNiceMethodWhichReturnsMyNiceWrapper(...);

ObjectUtils.defaultIfNotNull(myNiceWrapperObject, myNiceWrapperObject.getWrappedDate);



For the moment I have to perform a null comparison on my own,e.g., 

if (myNiceWrapperObject == null) {
return null;
}

return myNiceWrapperObject.getWrappedDate();





> ObjectUtils.defaultIfNotNull
> ----------------------------
>
>                 Key: LANG-694
>                 URL: https://issues.apache.org/jira/browse/LANG-694
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.*
>         Environment: all
>            Reporter: Ivan Hristov
>            Priority: Minor
>   Original Estimate: 10m
>  Remaining Estimate: 10m
>
> Currently there is a defaultIfNull method in ObjectUtils, which is a great one, but it will be nice to have an additional method called defaultIfNotNull which returns null if the first parameter is null or the second parameter if the object is not null. This is quite helpful and avoids an if statement for cases when there is a wrapper object which first need to be checked if it's null and then information could be taken from it. Example:
> public class MyNiceWrapper {
> private Date wrappedDate;
> public Date getWrappedDate() {
> return wrappedDate;
> }
> ...
> }
> MyNiceWrapper myNiceWrapperObject = someNiceMethodWhichReturnsMyNiceWrapper(...);
> ObjectUtils.defaultIfNotNull(myNiceWrapperObject, anotherObject);
> For the moment I have to perform a null comparison on my own,e.g., 
> if (myNiceWrapperObject == null) {
> return null;
> }
> return anotherObject;

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira