Skip to content

Instantly share code, notes, and snippets.

View oehme's full-sized avatar

Stefan Oehme oehme

View GitHub Profile
QPerson p = QPerson.person;
List<Person> persons = db.from(p)
.where(
(p.firstName.loe("C").or(p.lastName.goe("T")))
.and(p.age.gt(18))
.and(p.gender.eq(Gender.FEMALE))
)
.orderBy(p.firstName.asc())
.list(p);
@CreateWith(typeof(GuiceSpecCreator))
@InjectWith(typeof(EnglishModule))
describe Greeter "Interface Injection"{
@Inject
Greeter injected
fact "the subject is injected by Guice"{
subject.greet should be "Hello"
}
@Immutable
class Address {
String street;
String city;
String zip;
String postOfficeBox
}
class AddressTest {
val address = Address::build[
class ImmutableProcessor extends AbstractClassProcessor {
override doRegisterGlobals(ClassDeclaration cls, RegisterGlobalsContext context) {
context.registerClass(cls.builderClassName)
}
override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {
if(cls.extendedClass != object) cls.addError("Inheritance does not play well with immutability")
cls.final = true
class ExtractInterfaceProcessor extends AbstractClassProcessor {
override doRegisterGlobals(ClassDeclaration cls, RegisterGlobalsContext context) {
context.registerInterface(cls.qualifiedInterfaceName)
}
override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {
findInterface(cls.qualifiedInterfaceName) => [ iface |
cls.declaredMethods.filter [
visibility == Visibility::PUBLIC
@Active(typeof(BenchmarkProcessor))
annotation Benchmark {
}
class BenchmarkProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration cls, TransformationContext context) {
new BenchmarkClassGenerator(context, cls).generate
}
}
@Benchmark
class FibonacciBenchmarkXtend24 {
List<Integer> nValues = #[5, 10, 20]
def timeDumbFibonacci() {
for (i : 1 .. iterations) {
new Fibonaccis().dumbFibonacci(n)
}
}
public class FibonacciBenchmarkJava extends SimpleBenchmark {
@Param
private int n;
public static List<Integer> nValues = ImmutableList.of(5, 10, 20 );
public void timeMemoizedFibonacci(int iterations) {
for (int i = 0; i < iterations; i++) {
new Fibonaccis().memoizedFibonacci(n);
@oehme
oehme / gist:5180927
Last active December 15, 2015 01:38
Automatic Memoization through annotation processing
@Active(typeof(MemoizeProcessor))
annotation Memoize {
}
class MemoizeProcessor implements TransformationParticipant<MutableMethodDeclaration> {
override doTransform(List<? extends MutableMethodDeclaration> methods, extension TransformationContext context) {
methods.forEach [
switch (parameters.size) {
case 0: new ParamterlessMethodMemoizer(it, context, methods.indexOf(it)).generate
case 1: new SingleParameterMethodMemoizer(it, context, methods.indexOf(it)).generate
@oehme
oehme / gist:5180925
Last active December 15, 2015 01:38
Memoized fibonacci method - Generated Java code
@Memoize
public BigInteger fibonacci(final int n) {
try {
return fibonacci_cache.get(n);
} catch(Throwable e) {
throw Exceptions.sneakyThrow(e.getCause());
}
}
private BigInteger fibonacci_init(final int n) {