Skip to content

Unofficial build of jOOλ v0.9.15 latest official sources, plus extra features

Latest
Compare
Choose a tag to compare
@magicprinc magicprinc released this 18 Oct 18:39
· 2 commits to main since this release

This is an unofficial build of jOOλ. It includes all changes from the official repository. Plus some changes I have made in course of migration of an old legacy project to Java 8 and finally decided to make some of them public, because they could be interesting for broader auditory.

Most of the changes (except fixed errors) are simply "copy into" changes. So I can easily maintain this fork.

A short list of additions:

  1. [optional] Gradle 7.5.1 build with Error-Prone
    https://errorprone.info/ - is a great statistical analyzer from Google. It is used at compile time to find problems in code. 

  2. Added Predicates (Predicate0- Predicate16) similar to Functions (Function0- Function16). The API is almost identical.

  3. Fixed Javadoc errors and Error Prone errors. And also some Javadoc and Error Prone warnings. So one can build javadoc.jar without disabling Java DocLint.
    Most of the Javadoc errors falling in three main categories: 
    a) unescaped < and >
    b) "* @see {@link " (e.g. * @see {@link Sneaky#biFunction(CheckedBiFunction)} ) - it must be either * @see  Sneaky#biFunction(CheckedBiFunction) or {@link Sneaky#biFunction(CheckedBiFunction)}. I have decided to go with "{@link" variant.
    c) Wrong order of code and pre tags. It must be pre than code.

  4. Sub-project/branch "jOOL" had one type End-of-lines and "jOOL-java-8" the other. So it wasn't easy to understand: are they the same? Now both branches are identical except 'module-info.java' and build scripts.

  5. Loops.java - tiny funny functional "external" loops. Quick and fast replacement for some nasty repetitive "while" and "for". Can be used occasionally as "poor man's JMH" (There is a version with warm-up and measurement).
    There is also a "multi-/nested for" version, with so many nested for loops as you like. Can be used as "forEach", Iterator or Seq version.
     

  6. [!!!] "Optional" wrapped/alternative/default-value or "Safe" version of Unchecked and Sneaky. Instead of throwing Exception, it optionally logs it and returns some value. 
    org.jooq.lambda.Wrap (a new sibling of Unchecked and Sneaky).
    org.jooq.lambda.Either (a Tuple2 descendant) is used as a Tuple equivalent of Java's Optional.

There are usually three methods for each functional primitive :
e) Function<T,Either<R>> function (CheckedFunction<T,R> unsafeFunction)
h) Function<T,R> function (CheckedFunction<T,R> unsafeFunction, BiFunction<T,Throwable,R> handler)
d) Function<T,R> functionDef (CheckedFunction<T,R> unsafeFunction, R def)

In case of failure (a thrown Throwable):
e) returns "Optional" Either
h) allows user to handle it
d) returns default value
(There is no Supplier version, because version h - can be used in this case either).

As you can also see, it uses more powerful handlers: they receive not only Throwable, but also all original parameters - so they can do something meaningful.

Wrap also has some very useful methods like:
• static boolean run (CheckedRunnable runSafely)
• static <T> Either<T> call (CheckedCallable<T> callSafely)
• static void throwIfError (Throwable t) throws Error, etc..

  1. Safe Runnable and Callable
    They can be used to easily implement similar "safe" behavior.
    One need to override
    V execute () throws Throwable
    or
    void execute () throws Throwable
    and use them as usual Runnable or Callable, but all unhandled Exceptions will be caught and logged.

Interestingly enough, that I didn't introduce dependency to any logger framework, although Slf4j or Log4j will be discovered and used. The official way opened for frameworks such a Spring is used plus static MethodHandle, which allows achieving "direct call" speed of calling Slf4j's and Log4j's Logger.warn. If both slf4j and log4j are absent – System.err will be used.

  1. Unit tests with 100% code coverage of newly added code.

  2. Small Demo project with some impressive examples
    https://github.com/magicprinc/jOOL/blob/main/jOOL/src/test/java/org/jooq/lambda/Demo202210Test.java

  3. [jOOQ#391] Add Predicate<Tuple[N]<T...>> Tuple.predicate(Predicate[N]<T...>)
    Because I already have made Predicate0-16 it was easy to implement.