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 / 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
@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;
}
// 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 / 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
@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 / 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 / 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 / Lazy.java
Last active December 19, 2015 13:59
A thread-safe Java object reference that will only initialize its value when get() is called
/*
Copyright (c) 2014 Stijn Van Bael
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
@stijnvanbael
stijnvanbael / OngoingStubbingAdapter.java
Created July 10, 2013 11:50
Provides a way to obtain an OngoingStubbing for Mockito spies or void methods. This allows you to write highly readable code in your tests.
import org.mockito.Mockito;
import org.mockito.internal.stubbing.answers.ThrowsException;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.OngoingStubbing;
import org.mockito.stubbing.Stubber;
/**
* <p>
* Provides a way to obtain an OngoingStubbing for spies or void methods. This allows you to write highly readable code in your tests.
@stijnvanbael
stijnvanbael / TypeBuilder.java
Created July 10, 2013 11:52
TypeBuilder builds parameterized Java Class references.
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
* <p>TypeBuilder builds parameterized {@code Class<?>} references. Example:</p>
* <pre>
* {@code
* Class<List<String>> type = new TypeBuilder<List<String>>() { }.build();
* }
* </pre>