Skip to content

Instantly share code, notes, and snippets.

View stijnvanbael's full-sized avatar

Stijn Van Bael stijnvanbael

  • Enprove
  • Belgium
View GitHub Profile
@stijnvanbael
stijnvanbael / BindingBuilder.class
Created July 10, 2013 11:47
Convenience builder for creating bindings with Java Beansbinding.
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.Converter;
public class BindingBuilder<S, SV, T, TV> implements PartialBindingBuilder<S, SV> {
private final S source;
private final String sourceProperty;
private AutoBinding<S, SV, T, TV> binding;
@stijnvanbael
stijnvanbael / Extender.java
Last active August 25, 2021 05:20
Extender extends Java objects at runtime. Any object implementing an interface can be extended this way with new methods. Or it can be used to change the behaviour of existing methods. Also useful for duck typing.
/*
Copyright (c) 2017 Stijn Van Bael.
All rights reserved.
Redistribution and use in source and binary forms are permitted
provided that the above copyright notice and this paragraph are
duplicated in all such forms and that any documentation,
advertising materials, and other materials related to such
distribution and use acknowledge that the software was developed
by the <organization>. The name of the
@stijnvanbael
stijnvanbael / MethodSelector.java
Last active December 19, 2015 13:58
Provides a safe, compiler-checked way to obtain a Java Method reference from an interface.
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
/**
* Provides a safe, compiler-checked way to obtain a {@code Method} reference from an interface. eg:
* <pre>
* {@code
@stijnvanbael
stijnvanbael / GuidedMethodChainExample.java
Last active December 17, 2015 15:59
Example on how to guide the API user with a method chain and a class implementing multiple interfaces
public class GuidedMethodChainExample {
public static void main(String[] args) throws SQLException {
// The method chain should read like natural language
ResultSet rs = SQLQuery.selectFrom("employee")
.where("first_name").isEqualTo("John")
.and("last_name").isEqualTo("Doe")
.execute();
}
// These interfaces limit the scope in the method chain
// Choose relevant names for types that are unlikely to exist in another library
public final class SealedServicesFactory {
// No need to repeat “my” or “service” in the method name, as the type defines the
// scope already.
public SealedServices create() {
return new DefaultSealedServices(new FooService(), new BarService());
}
}
@stijnvanbael
stijnvanbael / DefaultSealedServices.java
Last active December 17, 2015 15:59
Sealed class hieararchy "implementation"
public final class DefaultSealedServices extends SealedServices {
private FooService fooService;
private BarService barService;
// We don’t want the client to instantiate this class
DefaultSealedServices(FooService fooService, BarService barService) {
this.fooService = fooService;
this.barService = barService;
}
@stijnvanbael
stijnvanbael / SealedServices.java
Last active December 17, 2015 15:59
Sealed class hierarchy "interface"
public abstract class SealedServices {
Services() {
// Default scoped constructor prevents extension by client
}
public abstract FooService getFooService();
public abstract BarService getBarService();
abstract void validate(); // method we don’t want the client to call