using a single method abstract class as a functional interface


abstract class

public abstract class SchemaOutputResolver {
    public abstract Result createOutput(
        String namespaceUri, String suggestedFileName)
        throws IOException;
}

We, unfortunately, can’t use it in a lambda expression because it’s not an interface but an abstract class.

final JAXBContext context;
context.generateSchema((namespaceUri, suggestedFileName) -> {
    // not compile
});

We, of course, can use it as an anonymous class.

final JAXBContext context;
context.generateSchema(new SchemaOutputResolver() {
    @Override
    public Result createOutput(String namespaceUri,
                               String suggestedFileName)
        throws IOException {
        return null;
    }
})

functional interface

public class SchemaOutputResolvers {

    //@FunctionalInterface
    public static interface Functional {
        Result createOutput(String namespaceUri, String suggestedFileName)
            throws IOException;
    }

    private static final class FunctionalAdapter extends SchemaOutputResolver {

        public FunctionalAdapter(final Functional functional) {
            super();
            this.functional = functional;
        }

        @Override
        public Result createOutput(final String namespaceUri,
                                   final String suggestedFileName)
            throws IOException {
            return functional.createOutput(namespaceUri, suggestedFileName);
        }

        private final Functional functional;
    }

    public static SchemaOutputResolver of(final Functional functional) {
        return new FunctionalAdapter(functional);
    }

    public static SchemaOutputResolver simply(final Functional functional) {
        return new SchemaOutputResolver() {
            @Override public Result createOutput(String namespaceUri,
                                                 String suggestedFileName)
                throws IOException {
                    return functional.createOutput(
                        namespaceUri, suggestedFileName);
            }
        }
    }
    private SchemaOutputResolvers() {
        super();
    }
}

lambda expression

context.generateSchema(SchemaOutputResovers.of((namespaceUri, suggestedFileName) -> {
    // ...
});

A New Kid On The Block

public static SchemaOutputResolver of(
    final BiFunction<String, String, Result> function) {

    return new SchemaOutputResolver() {

        @Override
        public Result createOutput(final String namespaceUri,
                                   final String suggestedFileName)
            throws IOException {

            return function.apply(namespaceUri, suggestedFileName);
        }

    };
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s