A Late Introduction to Jools

This post is a few years old now, so some details (or my opinions) might be out of date.
I would still love to hear your feedback in the comments below. Enjoy!

A while back I wrote a small Java tools library called Jools. I never really presented it to “the world” (except for hosting it on Github. So I figured, now is my chance. Here are the features Jools provide:

Python-like Range Objects

This one is pretty straight forward

for (final int i : new Range(5)) {
    players.add(new SmartPlayer(names.get(i)));

Generating Permutations

PermutationGenerator<String> pg = new PermutationGenerator<String>("a", "b", "c"); 
for (List<String> permutation : pg) {
    System.out.println("Permutation: " + permutation); 

This prints:

Permutation: [a, b, c]
Permutation: [a, c, b]
Permutation: [b, a, c]
Permutation: [b, c, a]
Permutation: [c, a, b]
Permutation: [c, b, a]

The cool thing about this class is that it also allows to get a permutation based on its index (calculated efficiently in O(n) time, when n is the number of items (not permutation)):

System.out.println("Permutation #3: " + pg.get(3));

This yields:

Permutation #3: [b, c, a]

Iterable Wrapper For Looping with Indices

Similar to Python’s enumerate function:

final List<String> list = Arrays.asList("A", "B", "C", "D", "E", "F", "G");
for (final IndexedElement<String> element : new Indexer<String>(list)) {
    System.out.println(element.getElement() + " : " + element.getIndex()); 

That’s it. Not a big library, as I said, but it’s been helpful to me, and I hope it can be helpful to you as well!

Discuss this post at the comment section below.
Follow me on Twitter and Facebook

Similar Posts