Skip to content

Instantly share code, notes, and snippets.

@claudemartin
claudemartin / WeakAssQueue.java
Last active Oct 10, 2019
WeakAssQueue allows you to let the GC remove the tail (ass) of this queue automatically.
View WeakAssQueue.java
/*
* Licence:
*
* Feel free to use this however you want with the following limitations.
* Don't change this licence here and don't remove it either.
*
* You can add it to your own namespace and alter the code as you wish.
* Please share the changes if you think they might be useful to others.
*
@claudemartin
claudemartin / Main.java
Created Apr 24, 2018
Configurable bit position per enum [Example]
View Main.java
package ch.claude_martin.enumbitset;
import static java.util.Arrays.asList;
import java.util.stream.Collectors;
public class Main {
enum Bla implements EnumBitSetHelper<Bla> {
A(-1), B(42), C(108);
View .java
public static void main(String args[]) {
String str = "abc";
byte[] cstr = str.getBytes();
cstr[0] = 'b';
System.out.println(new String(cstr));
System.out.println(str); // still "abc" -> it has not changed
}
@claudemartin
claudemartin / YComb.java
Created Oct 13, 2017
Y combinator in Java 8
View YComb.java
package ch.claude_martin;
import java.util.function.Function;
import java.util.function.UnaryOperator;
public class YComb {
interface Fn2Op<T> extends Function<Fn2Op<T>, UnaryOperator<T>> {
@Override
UnaryOperator<T> apply(Fn2Op<T> x);
View Child.java
public final class Child {
private final Object token;
private final SomeClass parent;
Child(SomeClass parent, Object token) {
this.parent = parent;
this.token = token;
}
public void doStuff() {
View Nashorn.java
package ch.claude_martin.playground;
import java.io.IOException;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
View pipe.c
#include <stdio.h>
#define MSGSIZE 16
char *msg1 = “hello, world #1”;
char *msg2 =“ hello, world #2”;
char *msg3 =“ hello, world #3”;
main () {
char inbuf[MSGSIZE];
int p[2], j, pid;
@claudemartin
claudemartin / AnimationLoop.java
Last active Aug 29, 2015
A very simple animation loop that can be paused and stopped.
View AnimationLoop.java
/****************************
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Claude Martin
*
* 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
@claudemartin
claudemartin / SomeClass.java
Last active Aug 29, 2015 — forked from anonymous/SomeClass.java
game loop that can bepaused / stopped, now using a Lock.
View SomeClass.java
package com.example.foo;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class SomeClass {
public static void main(final String[] args) {
final Thread thread = new Thread(SomeClass::gameLoop);
You can’t perform that action at this time.