001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.commons.function;
018
019import static org.apache.juneau.commons.utils.AssertionUtils.*;
020
021/**
022 * A functional interface representing an operation that accepts four arguments and returns no result.
023 *
024 * <p>
025 * This interface extends the standard Java {@link java.util.function.Consumer} pattern to support
026 * four-argument consumers. It's useful when you need to pass operations with four parameters to methods
027 * that expect functional interfaces, such as in iteration, builders, or callback patterns.
028 *
029 * <h5 class='section'>Features:</h5>
030 * <ul class='spaced-list'>
031 *    <li>Functional interface - can be used with lambda expressions and method references
032 *    <li>Composition support - provides {@link #andThen(Consumer4)} for operation chaining
033 *    <li>Side-effect operations - designed for operations that perform side effects rather than return values
034 * </ul>
035 *
036 * <h5 class='section'>Use Cases:</h5>
037 * <ul class='spaced-list'>
038 *    <li>Four-argument operations in iteration patterns
039 *    <li>Builder methods that accept four-parameter operations
040 *    <li>Callback patterns with four parameters
041 *    <li>Event handlers that process four values
042 * </ul>
043 *
044 * <h5 class='section'>Usage:</h5>
045 * <p class='bjava'>
046 *    <jc>// Lambda expression</jc>
047 *    Consumer4&lt;String,Integer,Boolean,Double&gt; <jv>printer</jv> = (<jv>name</jv>, <jv>age</jv>, <jv>active</jv>, <jv>score</jv>) -&gt;
048 *       System.<jsf>out</jsf>.println(<jv>name</jv> + <js>": age="</js> + <jv>age</jv> + <js>", active="</js> + <jv>active</jv> + <js>", score="</js> + <jv>score</jv>);
049 *    <jv>printer</jv>.apply(<js>"Alice"</js>, 30, <jk>true</jk>, 95.5);  <jc>// Prints "Alice: age=30, active=true, score=95.5"</jc>
050 *
051 *    <jc>// Operation composition</jc>
052 *    Consumer4&lt;String,Integer,Boolean,Double&gt; <jv>validate</jv> = (<jv>s</jv>, <jv>i</jv>, <jv>b</jv>, <jv>d</jv>) -&gt; {
053 *       <jk>if</jk> (<jv>i</jv> &lt; 0 || <jv>d</jv> &lt; 0) <jk>throw new</jk> IllegalArgumentException();
054 *    };
055 *    Consumer4&lt;String,Integer,Boolean,Double&gt; <jv>process</jv> = (<jv>s</jv>, <jv>i</jv>, <jv>b</jv>, <jv>d</jv>) -&gt; <jsm>process</jsm>(<jv>s</jv>, <jv>i</jv>, <jv>b</jv>, <jv>d</jv>);
056 *    Consumer4&lt;String,Integer,Boolean,Double&gt; <jv>validateAndProcess</jv> = <jv>validate</jv>.andThen(<jv>process</jv>);
057 *    <jv>validateAndProcess</jv>.apply(<js>"data"</js>, 42, <jk>true</jk>, 10.5);  <jc>// Validates then processes</jc>
058 * </p>
059 *
060 * <h5 class='section'>See Also:</h5><ul>
061 *    <li class='jc'>{@link Consumer2} - Two-argument consumer
062 *    <li class='jc'>{@link Consumer3} - Three-argument consumer
063 *    <li class='jc'>{@link Consumer5} - Five-argument consumer
064 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauCommonsBasics">juneau-commons Basics</a>
065 * </ul>
066 *
067 * @param <A> The type of the first argument to the operation.
068 * @param <B> The type of the second argument to the operation.
069 * @param <C> The type of the third argument to the operation.
070 * @param <D> The type of the fourth argument to the operation.
071 */
072@FunctionalInterface
073public interface Consumer4<A,B,C,D> {
074
075   /**
076    * Returns a composed {@link Consumer4} that performs, in sequence, this operation followed by the {@code after} operation.
077    *
078    * <p>
079    * This method enables operation composition, allowing you to chain multiple operations together.
080    * If performing either operation throws an exception, it is relayed to the caller of the composed operation.
081    * If performing this operation throws an exception, the {@code after} operation will not be performed.
082    *
083    * <h5 class='section'>Example:</h5>
084    * <p class='bjava'>
085    *    Consumer4&lt;String,Integer,Boolean,Double&gt; <jv>log</jv> = (<jv>s</jv>, <jv>i</jv>, <jv>b</jv>, <jv>d</jv>) -&gt; <jsm>logger</jsm>.info(<jv>s</jv> + <js>": "</js> + <jv>i</jv> + <js>", "</js> + <jv>b</jv> + <js>", "</js> + <jv>d</jv>);
086    *    Consumer4&lt;String,Integer,Boolean,Double&gt; <jv>store</jv> = (<jv>s</jv>, <jv>i</jv>, <jv>b</jv>, <jv>d</jv>) -&gt; <jv>cache</jv>.put(<jv>s</jv>, <jv>i</jv>, <jv>b</jv>, <jv>d</jv>);
087    *    Consumer4&lt;String,Integer,Boolean,Double&gt; <jv>logAndStore</jv> = <jv>log</jv>.andThen(<jv>store</jv>);
088    *    <jv>logAndStore</jv>.apply(<js>"data"</js>, 100, <jk>true</jk>, 50.0);  <jc>// Logs and stores</jc>
089    * </p>
090    *
091    * @param after The operation to perform after this operation. Must not be <jk>null</jk>.
092    * @return A composed {@link Consumer4} that performs in sequence this operation followed by the {@code after} operation.
093    * @throws NullPointerException if {@code after} is <jk>null</jk>.
094    */
095   default Consumer4<A,B,C,D> andThen(Consumer4<? super A,? super B,? super C,? super D> after) {  // NOSONAR - false positive on generics
096      assertArgNotNull("after", after);
097      return (A a, B b, C c, D d) -> {
098         apply(a, b, c, d);
099         after.apply(a, b, c, d);
100      };
101   }
102
103   /**
104    * Performs this operation on the given arguments.
105    *
106    * <h5 class='section'>Example:</h5>
107    * <p class='bjava'>
108    *    Consumer4&lt;String,Integer,Boolean,Double&gt; <jv>handler</jv> = (<jv>key</jv>, <jv>value</jv>, <jv>flag</jv>, <jv>weight</jv>) -&gt; {
109    *       <jv>map</jv>.put(<jv>key</jv>, <jv>value</jv>);
110    *       <jk>if</jk> (<jv>flag</jv>) <jv>cache</jv>.invalidate(<jv>key</jv>);
111    *       <jv>stats</jv>.record(<jv>weight</jv>);
112    *    };
113    *    <jv>handler</jv>.apply(<js>"user"</js>, 12345, <jk>true</jk>, 1.5);
114    * </p>
115    *
116    * @param a The first operation argument.
117    * @param b The second operation argument.
118    * @param c The third operation argument.
119    * @param d The fourth operation argument.
120    */
121   void apply(A a, B b, C c, D d);
122}