Skip to content

Instantly share code, notes, and snippets.

@danveloper
danveloper / convert.sh
Last active December 5, 2018 18:51
Java to Groovy converter in bash!!
#!/bin/sh
find . -iname "*.java" -exec sed s/\;$// {} \;
@danveloper
danveloper / Promises.java
Created April 16, 2013 19:40
Java 8 CompletableFuture (Promise) and Lambda example
public class Promises {
private static class PageMetrics {
Integer visits;
Long avgMsOnPage;
@Override
public String toString() {
return String.format("{ avgMsOnPage=%d, visits=%d }", avgMsOnPage, visits);
}
@danveloper
danveloper / MemoizedLambdaFib.java
Last active December 16, 2015 08:19
Memoized Fibonacci calculation using lambdas in Java 8
public class MemoizedLambdaFib {
interface MemoizedFunction<T, R> {
enum Cache {
_;
Map<Object, Object> vals = new HashMap<>();
}
R calc(T t);
@danveloper
danveloper / TrampolineLambda.java
Created April 18, 2013 18:27
Trampoline in Java 8 with Lambdas and interface defaults
public class TrampolineLambda {
interface TrampolineFunction<T, R> {
R apply(T...obj);
public default Object trampoline(T...objs) {
Object result = apply(objs);
if (!(result instanceof TrampolineFunction)) {
return result;
} else {
return this;
@danveloper
danveloper / ImprovedNamingDescriptorCustomizer.java
Created May 6, 2013 05:53
EclipseLink class descriptor customizer that mimics Hibernate's ImprovedNamingStrategy for table and field naming
public class ImprovedNamingDescriptorCustomizer implements DescriptorCustomizer {
@Override
public void customize(ClassDescriptor classDescriptor) throws Exception {
String className = classDescriptor.getJavaClassName();
String shortName = Helper.getShortClassName(className);
String improvedName = toImprovedName(shortName);
classDescriptor.setTableName(improvedName);
updateMappings(classDescriptor, improvedName);
@danveloper
danveloper / ApplicationConfiguration.groovy
Last active December 17, 2015 14:19
Groovy Configuration DSL for environment properties in Spring
package com.danveloper.reactor.config
reactor {
social {
twitter {
consumerKey = "ABCD"
consumerSecret = "EFGH"
accessToken = "IJKL"
accessTokenSecret = "MNOP"
}
@danveloper
danveloper / AppConfig.groovy
Created May 22, 2013 16:31
Accessing Grails Configuration with Spring Property Placeholders
package com.objectpartners.application.config
config {
social {
twitter {
consumerKey = "ABCD"
consumerSecret = "EFGH"
accessToken = "IJKL"
accessTokenSecret = "MNOP"
}
@danveloper
danveloper / DataModel.groovy
Created May 29, 2013 17:54
Introduction - "Redefining the Service Layer with Groovy Categories"
class Payee {
String name
String phoneNumber
Address billingAddress
}
public interface PaymentType {
Payee getPayee();
}
@danveloper
danveloper / Service.groovy
Created May 29, 2013 18:08
Expansion - "Redefining the Service Layer with Groovy Categories"
class LocalPaymentService implements PaymentService {
// Dependency injected from container
CreditCardPaymentGateway creditCardPaymentGateway
GiftCardPaymentGateway giftCardPaymentGateway
CheckPaymentGateway checkPaymentGateway
EmployeePaymentGateway employeePaymentGateway
public String process(PaymentType paymentType) throws IllegalArgumentException {
if (paymentType instanceof CreditCardPaymentType) {
@danveloper
danveloper / Service.groovy
Created May 29, 2013 18:12
Fragility - "Redefining the Service Layer with Groovy Categories"
class LocalPaymentService implements PaymentService {
// Dependency injected from container
CreditCardPaymentGateway creditCardPaymentGateway
GiftCardPaymentGateway giftCardPaymentGateway
CheckPaymentGateway checkPaymentGateway
EmployeePaymentGateway employeePaymentGateway
public String process(PaymentType paymentType) throws IllegalArgumentException {
if (paymentType instanceof CreditCardPaymentType) {