Seamlessly Convert Java Functional Interfaces to Groovy Closures

This article explains the compatibility challenges between Java functional interfaces and Groovy closures, introduces a utility class that converts Supplier, Function, Predicate, and Consumer into Groovy closures, and provides a complete code example with demonstration and output.

FunTester
FunTester
FunTester
Seamlessly Convert Java Functional Interfaces to Groovy Closures

Background

Java provides a large set of functional interfaces in the java.util.function package (e.g., Supplier, Function, Predicate, Consumer). Each interface is distinguished by its return type and the number of parameters, which leads to many separate types.

Groovy, on the other hand, uses a single Closure type that can represent any of those functional signatures, making it more flexible for scripting.

Problem

When Java and Groovy code are mixed in the same project, developers often need to convert a Java functional interface instance into a Groovy Closure. Doing this conversion manually for each interface adds boilerplate and can be error‑prone.

Solution – JToG Utility Class

The JToG class offers static helper methods that wrap Java functional interfaces into Groovy closures. Each method returns a Closure that delegates to the underlying Java instance.

package com.funtester.utils

import java.util.function.Consumer
import java.util.function.Function
import java.util.function.Predicate
import java.util.function.Supplier

/**
 * Utility to bridge Java functional interfaces and Groovy closures.
 */
class JToG {
    /** Convert a Supplier to a Closure */
    static Closure toClosure(Supplier supplier) {
        return { supplier.get() }
    }

    /** Convert a Function to a Closure */
    static Closure toClosure(Function function) {
        return { def t -> function.apply(t) }
    }

    /** Convert a Predicate to a Closure */
    static Closure toClosure(Predicate predicate) {
        return { def t -> predicate.test(t) }
    }

    /** Convert a Consumer to a Closure */
    static Closure toClosure(Consumer consumer) {
        return { def t -> consumer.accept(t) }
    }
}

Usage Example

The following Groovy script demonstrates how to wrap a Java Function and invoke it as a closure. It also shows an equivalent pure‑Groovy closure for comparison.

@Log4j2
class Ts extends FunHttp {
    public static void main(String[] args) {
        // Java Function implementation
        Function<Integer, Integer> function = new Function<Integer, Integer>() {
            @Override
            Integer apply(Integer integer) {
                return integer * 10
            }
        }
        log.info(function.apply(1))               // Direct Java call

        // Convert to Groovy Closure using JToG
        def closure = JToG.toClosure(function)
        log.info(closure(2))                       // Invoked as a Closure

        // Pure Groovy closure (for reference)
        def func = { int x -> x * 10 }
        log.info(func(3))
    }
}

Console Output

18:53:04.649 main 10
18:53:04.673 main 20
18:53:04.676 main 30
Process finished with exit code 0

Key Points

The utility eliminates repetitive boilerplate when integrating Java functional interfaces into Groovy scripts.

All four common interfaces ( Supplier, Function, Predicate, Consumer) are supported.

The returned Closure can be used anywhere a Groovy closure is expected, preserving Groovy's concise syntax.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaFunctional InterfaceGroovyclosureInterop
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.