My first inline class for Java

I’ve once made a simple helper class for a set of bytes stored as an integer bitfield. So the idea is that each of the 32bits is used to represent if the ordinal number is in the set or not. This only works for the bytes 0 to 31.
Now this is an “inline class” – something we will get with Project Valhalla.
Here’s the branch of my project:
https://github.com/claudemartin/smallset/tree/valhalla

For this to work you need a JDK with a preview of Project Valhalla. Check java.net for that: http://jdk.java.net/valhalla/

Just download that JDK, clone my branch, build using Ant, and see what you can do with it. It’s actually a good example of an inline class, because it is just an integer (primitive) value but has methods (like a referenced type would). It doesn’t need object identity, but should still behave like an object. For example the sets are comparable. The type actually implements the Comparable interface.

Note that this might not work with newer JDKs. I used JDK 14 + valhalla from java.net and the feature might look quite different in newer releases of Java.

Streamed SQL

I just saw that I never posted this here on my blog. JDBC is still a good way to just connect to some SQL database where JPA would be overkill. So I wrote a library that allows you to use the Java Stream API to process that data.

The Code is on GitHub:

https://github.com/claudemartin/streamed-sql

Example Code:

Connection conn = dbpool.getConnection();
var strsql = StreamedSQL.create(conn, true); 
try (Stream<Foo> stream = strsql.stream("SELECT * FROM FOO", Foo::new)) {
  stream.filter(f -> f.getName().startsWith("L")).sorted().forEachOrdered(System.out::println);
}

Simple Animation Loop in Java

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.

Continue reading “Simple Animation Loop in Java”

Multiset 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.

Functional Java Tuples

Just a prototype so far:

https://github.com/claudemartin/FunctionalJavaTuples

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 Fn for Quad ... Decade
  • Unit-Tests (instead of FunctionalDemo.java)
  • Types Curried and Uncurried for such forms of the methods

Contact me on github if you are interested in this project.