I have the following class Subcontractor and a Utils Class named ResultUtils.
One of the Utils methods calculates the latest business year on a subcontractor. This method has to return an Optional Double, because it's possible that a subcontractor was newly added during the current year. So it has no statistics on the last two years. If a subcontractor was added one year before, it has statistics for the last business year, but not for the penultimate business year.
My problem is to chain the getLatestResult method correctly. In my example the ifPresentOrElse is wrong of course, because it cannot return any values.
How should I do that?
public class Subcontractor
{
BusinessYear lastBusinessYear;
BusinessYear penultimateBusinessYear;
...
}
public class ResultUtils
{
public static Optional< Double > getLatestResult( Subcontractor )
{
// how to do it right here?
getResult( Subcontractor.getLastBusinessYear() )
.ifPresentOrElse( r -> return r,
() -> return getResult( Subcontractor.getPenultimateBusinessYear() ) );
}
public static Optional< Double > getResult( BusinessYear businessYear )
{
if( businessYear == null ) return Optional.empty();
// make some calculations
return Optional.of( calculcatedResult );
}
}