How to change Optional field(Java 8 API) into Java 7 -
i have application taken github in java 8 api, namely optional keyword, used. environment run app setup jdk_7. hence, have 0 experience java 8 api, can give alternative code block of following sample code:
public final static optional<string> reversegeocodefromlatlong(final double latitude, final double longitude) { final stringbuilder bingmapsurl = new stringbuilder(); bingmapsurl .append(bing_maps_url_start) .append(latitude) .append(",") .append(longitude) .append(bing_maps_url_middle_json) .append(constants.bing_maps_api_key_value); logger.debug("bingmapsurl==>{}", bingmapsurl.tostring()); httpurlconnection httpurlconnection; inputstream inputstream = null; try { final url url = new url(bingmapsurl.tostring()); httpurlconnection = (httpurlconnection)url.openconnection(); if(httpurlconnection.http_ok == httpurlconnection.getresponsecode()){ inputstream = httpurlconnection.getinputstream(); return getstatefromjsonresponse(inputstream); } } catch (final throwable throwable) { logger.error(throwable.getmessage(), throwable); throwable.printstacktrace(); } finally{ if(null != inputstream) { try { inputstream.close(); } catch (final ioexception ioexception) { logger.error(ioexception.getmessage(), ioexception); ioexception.printstacktrace(); } } httpurlconnection = null; } return optional.absent(); }
the purpose of optional pretty force not forget null checks, replace return optional.absent()
return null
, make getstatefromjsonresponse
return string
instead of , optional<string>
. don't forget check null
s in code, won't forced make checks.
Comments
Post a Comment