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
public class Product { | |
enum ProductType { | |
PRODUCT, SERVICE | |
} | |
String name; | |
ProductType type; | |
BigDecimal cost; | |
} |
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
public class Coupon { | |
String id; | |
double discountRate; | |
List<Product.ProductType> appliesTo; | |
} |
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
public interface ProductOrderRulesEngine { | |
boolean apply(ProductOrder order); | |
} |
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
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 |
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
/** 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: "[email protected]", orderCount: 0) |
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
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 |
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
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") | |
} | |
} |
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
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; |
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
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 |
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
// If you prefer Gradle | |
compile 'com.danveloper.log4j:enhanced-log4j-appender:0.1-SNAPSHOT' |