Code puzzle : procedure duplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package code.puzzles; | |
public class WebserviceHelper { | |
private final Webservice webservice = new Webservice(); | |
public Products getProducts(final Integer category, final Integer quantity) throws WebserviceException{ | |
Products products; | |
try { | |
products = webservice.getProducts(category, quantity); | |
} catch(RuntimeException e) { | |
throw new WebserviceException("failed invoking the webservice with category "+category+" and quantity "+quantity, e); | |
} | |
ifNullThrowError(products); | |
return products; | |
} | |
public Rebates getRebates(final Products products) throws WebserviceException{ | |
Rebates rebates; | |
try { | |
rebates = webservice.getRebates(products); | |
} catch(RuntimeException e) { | |
throw new WebserviceException("failed invoking the webservice with products "+products); | |
} | |
ifNullThrowError(rebates); | |
return rebates; | |
} | |
private void ifNullThrowError(Object object) throws WebserviceException { | |
if (object==null) | |
throw new WebserviceException("The webservice returned null"); | |
} | |
/** below are classes that are just present for the code snippet above to compile | |
* and enable IDE refactorings | |
*/ | |
static class Webservice { | |
public Products getProducts(Integer category, Integer quantity) { | |
return new Products(); | |
} | |
public Rebates getRebates(Products products) { | |
return new Rebates(); | |
} | |
} | |
static class Products { | |
} | |
static class Rebates { | |
} | |
public static class WebserviceException extends Exception { | |
public WebserviceException(String string, RuntimeException e) { | |
} | |
public WebserviceException(String string) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment