Jump to content
Botre

[Functional Snippet] Multi-Consumers

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...