Last active
April 18, 2018 22:25
-
-
Save soverby/e61ab91b23574b1790c3351d656a0ec9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Example of a fully imperative approach to writing Java code... | |
public class FullyImperative { | |
// @Inject | |
private final ItemRepository itemRepository; | |
// @Inject | |
private final InventoryLocationRepostitory inventoryLocationRepostitory; | |
// Find the first available InventoryLocation for a given item id. | |
public InventoryLocation itemByFirstAvailable(String itemId) | |
throws NotFoundException { | |
// Null check | |
if(null == itemId) { | |
throw new InvalidParameterException("itemId cannot be null"); | |
} | |
// Retrieve the item | |
Item item = itemRepository.findByItemId(itemId); | |
// Throw an exception if the item doesn't exist | |
if(null == item) { | |
throw new NotFoundException("Item " + itemId + " not found"); | |
} | |
// Get a List of all of the inventory locations for this item | |
List<InventoryLocation> inventoryLocations = inventoryLocationRepostitory.findByItemId(item); | |
// If we couldn't find any return null | |
if(null == inventoryLocations || inventoryLocations.isEmpty()) { | |
return null; | |
} | |
// Default inventory location is none | |
InventoryLocation inStockLocation = null; | |
// Loop through locations for this item | |
for(InventoryLocation location : inventoryLocations) { | |
// Finally some business logic! Return the first in stock location | |
if(location.getInStock() > 0) { | |
inStockLocation = location; | |
break; | |
} | |
} | |
return inStockLocation; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment