If you want to show some animation, maybe for a simple java game, then you need to render a certain amount of frames per second. For a smooth animation you want 30 to 60 frames per second. But sometimes the needed calculations need a bit longer so the delay until the drawing of the next frame needs to be shorter. For this an animation loop is needed. I have implemented a very simple animation loop for Java 8+ that can be paused and started.
Functional Programming made easy!
Continue reading Functional Programming in Java 8
In Java you can only reference final fields inside a closure. Here’s my solution*. Continue reading Recursive Closures in Java
Here’s an implementation of a multiset in Java 8. It uses a
Map<T, Integer> to count how many times an object was inserted (multiplicity). It is not thread-safe, but there’s
synchronizedMultiset(...) and it always knows the current size in
O(1). With the method
asMap() you can get a view of the multiset as a
Map, that will allow modification of the underlying multiset.
The code is on GitHub: github.com/claudemartin/multiset
This started as a simple implementation and is not rather complex. The iterator allows to remove elements during iteration. I tried to write my own
Spliterator, but it wasn’t faster than the default implementaion. Many methods are optimized and I added some set operations for multisets (union, intersect, minus). With
merge(...) you can merge two multisets with any operation.
Yet another side project. But this isn’t usable as it’s not finished and not really tested. The idea is to provide wrapper classes for character sequences so that strings aren’t just copied and some memory can be saved.
The project is hosted here: https://github.com/claudemartin/stringwrappers/
Methods to use an int as a bit set of small values. Uses 32 bits for a bit set and offers high performance for algorithms. Designed for Java 8 and compatible with common interfaces and classes.
Project Home: https://github.com/claudemartin/smallset
Just a prototype so far:
The generic types of the arguments are always A, B, C, etc. The result is of generic type R.
It’s my goal to have three forms:
- Method/Lambda Form: Takes n arguments
- Curried Form: Methods are chained
A -> B -> C -> ... -> R
- Uncurried Form: Takes a tuple
(A,B,C,...) -> R
The idea is to add all the basic functions of Haskell to a project that then uses javatuples.org to have a way of using tuples for functional programming in Java 8.
- curry / uncurry
- zip / unzip
- composition (“.”-operator in Haskell)
- pipes (“|>” in F#)
- partial application
fn.arity()returns the arity of the function.
- Convert Suppliers and Consumers to Functions.
There’s still a lot to do…
List of TODOs:
- Implementations of
Quad ... Decade
- Unit-Tests (instead of FunctionalDemo.java)
Uncurriedfor such forms of the methods
Contact me on github if you are interested in this project.
There are many articles and blog posts about this topic but rarely are they complete. I’ll cover finalizers, Finalizer Guardian idiom, Dispose Pattern, PhantomReference and Cleanable.
Continue reading How to replace Object.finalize()
Run code only once!
Project hosted on github: https://github.com/claudemartin/LockOnce
It’s just one single utility class to make sure code is run only once. And then there’s another one for easy “Lazy Initialization”.
I once wrote this for Java 7 but now it needs Java 8. It makes much more sense to use this with Lambdas. It’s a bit like the lazy keyword of Scala.
// declaration of lazy field: final Supplier<Foo> lazy = Lazy.of(() -> ...); // Access to the data: Foo foo = lazy.get();
You can also register a “destructor”. This solves the problem that such fields (they are usually
static final) are never GCd and finalizers are utterly useless. In the same way you can have resources (they just have to implement
AutoClose) closed on shutdown.
There already is
sun.misc.Cleaner. And Java 9 should even have
java.util.Cleaner or something like that. I simply didn’t know. So it’s better to wait for Java 9 or use
If you are still interested:
I have a blog post about Finalizers and PhantomReferences: