Jump to content

[Functional Snippet] Multi-Consumers


Recommended Posts

Posted

Inspired by: https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html

 

The functional package does provide a Consumer (1 arg) and a BiConsumer (2args) but you're on your own if you want more args. I stopped at 5 because I have never really needed more than that.

 

Example

    public static void main(String[] args) {
        Consumer.One<String> printInput = input -> System.out.println(input);
        Consumer.Two<Integer, Double> printSum = (a, b) -> System.out.println(a + b);
        //...
        Consumer.Five<Integer, Integer, Integer, Integer, Integer> printMultiplication = (a, b, c, d, e) ->System.out.println(a * b * c * d *e);
    }

Consumers

/**
 * Represents an operation that accepts one or multiple arguments and returns no result.
 *
 * Created by Botre on 24/05/2016.
 */
public final class Consumer {

    private Consumer() {

    }

    private interface IConsumer {
   
    }

    @FunctionalInterface
    public interface One<A> extends IConsumer {

        void consume(final A a);

    }

    @FunctionalInterface
    public interface Two<A, B> extends IConsumer {

        void consume(final A a, final B b);

    }

    @FunctionalInterface
    public interface Three<A, B, C> extends IConsumer {

        void consume(final A a, final B b, final C c);

    }

    @FunctionalInterface
    public interface Four<A, B, C, D> extends IConsumer {

        void consume(final A a, final B b, final C c, final D d);

    }

    @FunctionalInterface
    public interface Five<A, B, C, D, E> extends IConsumer {

        void consume(final A a, final B b, final C c, final D d, final E e);

    }

}
  • Like 3
  • 5 weeks later...
Posted

o true

why do u finalize params? final on locals is useless, maybe for clarity but in this case its just an interface method

 

In my opinion, it's safer to finalize everything by default as opposed to leaving everything virtual by default.

 

I prefer to selectively unlock doors instead of selectively locking them, I'm an eternal pessimist who's afraid of getting robbed.

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...