Skip to content

Instantly share code, notes, and snippets.

@martinsson
Created February 5, 2011 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinsson/812388 to your computer and use it in GitHub Desktop.
Save martinsson/812388 to your computer and use it in GitHub Desktop.
Code puzzle : procedure duplication
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