View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3.function;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.math.BigInteger;
23  import java.util.concurrent.atomic.AtomicReference;
24  import java.util.function.Function;
25  
26  import org.apache.commons.lang3.AbstractLangTest;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link TriFunction}.
31   */
32  public class TriFunctionTest extends AbstractLangTest {
33  
34      @Test
35      public void testAccept() throws Throwable {
36          final AtomicReference<Character> ref1 = new AtomicReference<>();
37          final AtomicReference<Short> ref2 = new AtomicReference<>();
38          final AtomicReference<String> ref3 = new AtomicReference<>();
39          final TriFunction<AtomicReference<Character>, AtomicReference<Short>, AtomicReference<String>, String> tri = (t, u, v) -> {
40              ref1.set(Character.valueOf('a'));
41              ref2.set(Short.valueOf((short) 1));
42              ref3.set("z");
43              return "ABC";
44          };
45          assertEquals("ABC", tri.apply(ref1, ref2, ref3));
46          assertEquals(Character.valueOf('a'), ref1.get());
47          assertEquals(Short.valueOf((short) 1), ref2.get());
48          assertEquals("z", ref3.get());
49      }
50  
51      @Test
52      public void testAndThen() throws Throwable {
53          final AtomicReference<Character> ref1 = new AtomicReference<>();
54          final AtomicReference<Short> ref2 = new AtomicReference<>();
55          final AtomicReference<String> ref3 = new AtomicReference<>();
56          final TriFunction<AtomicReference<Character>, AtomicReference<Short>, AtomicReference<String>, String> tri = (t, u, v) -> {
57              ref1.set(Character.valueOf('a'));
58              ref2.set(Short.valueOf((short) 1));
59              ref3.set("z");
60              return "9";
61          };
62          final Function<String, BigInteger> after = t -> {
63              ref1.set(Character.valueOf('b'));
64              ref2.set(Short.valueOf((short) 2));
65              ref3.set("zz");
66              return BigInteger.valueOf(Long.parseLong(t)).add(BigInteger.ONE);
67          };
68          assertEquals(BigInteger.TEN, tri.andThen(after).apply(ref1, ref2, ref3));
69          assertEquals(Character.valueOf('b'), ref1.get());
70          assertEquals(Short.valueOf((short) 2), ref2.get());
71          assertEquals("zz", ref3.get());
72      }
73  
74  }