Skip to content

Instantly share code, notes, and snippets.

public class Product {
enum ProductType {
PRODUCT, SERVICE
}
String name;
ProductType type;
BigDecimal cost;
}
public class Coupon {
String id;
double discountRate;
List<Product.ProductType> appliesTo;
}
public interface ProductOrderRulesEngine {
boolean apply(ProductOrder order);
}
public enum ProductOrderRules implements ProductOrderRulesEngine {
/**
* If no shipping address was explicitly selected, then we'll ship to the billing address of the payment method.
* If the payment method doesn't have a billing address (gift card), then this rule will fail
*/
ShippingAddressRule {
public boolean apply(ProductOrder order) {
// check if the shipping address is null
if (order.shippingAddress == null && order.paymentMethod.getType() == PaymentMethod.PaymentMethodType.CREDIT_CARD) {
// if so, find the first credit card and use its billing address as the shipping address
/** Addresses **/
def billingAddress1 = new Address(street: "1 Main ST", unit: "7a", city: "Chicago", state: "IL", zip: 60652)
def billingAddress2 = new Address(street: "75 Dr Dre Rd.", city: "Compton", state: "CA", zip: 90220)
/** Payment Methods **/
def giftCard = new GiftCard(number: "0000000001", balance: 50.00)
def creditCard = new CreditCard(number: "4111111111111111", expiration: new Date(year: 2013, month: 1, date: 31), billingAddress: billingAddress1)
/** Account **/
def account = new Account(email: "daniel.p.woods@gmail.com", orderCount: 0)
@danveloper
danveloper / overlapcollapse.groovy
Created February 28, 2013 17:37
collapse overlapping ranges in list
List.metaClass.collapse = {->
def start,end
def reverse = delegate[0]?.getAt(0)-delegate[0]?.getAt(-1) > 0
def expanded = delegate.flatten().unique().sort()
def result = (reverse ? expanded.reverse() : expanded).inject([]) { l, n ->
start = start == null ? n : start
end = end == null ? n : end
if ((reverse?n+1:n-1)==end) {
end = n
@danveloper
danveloper / Destructurable.groovy
Created March 10, 2013 22:30
Destructurable data type using getAt() to associate a result with an object in a service method call
class Destructurable<E> {
E obj
Boolean result
def getAt(idx) {
if (idx == 0) return obj
if (idx == 1) return result
throw new RuntimeException("Only two parameters are returnable")
}
}
@danveloper
danveloper / EnhancedJMSAppender.java
Created March 20, 2013 21:14
EnhancedJMSAppender -- Real Time Logging Project
public class EnhancedJMSAppender extends JMSAppender {
private String appName;
/**
* Method enriches the headers so that JMS consumers know who you are
*/
@Override
public void append(LoggingEvent event) {
if(!checkEntryConditions()) {
return;
@danveloper
danveloper / log4j.properties
Created March 20, 2013 21:16
log4j.properties -- Real Time Logging Project
log4j.rootLogger=INFO, stdout, jms
## Be sure that ActiveMQ messages are not logged to 'jms' appender
log4j.logger.org.apache.activemq=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c - %m%n
## Configure 'jms' appender. You'll also need jndi.properties file in order to make it work
@danveloper
danveloper / build.gradle
Last active December 15, 2015 05:19
Enhanced JMS Appender Maven Artifact -- Real Time Logging Project
// If you prefer Gradle
compile 'com.danveloper.log4j:enhanced-log4j-appender:0.1-SNAPSHOT'