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  package org.apache.commons.functor.core.algorithm;
18  
19  import static org.junit.Assert.assertEquals;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import org.apache.commons.functor.BaseFunctorTest;
26  import org.apache.commons.functor.BinaryFunction;
27  import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
28  import org.junit.Test;
29  
30  /**
31   * Tests {@link FoldRight} algorithm.
32   */
33  public class TestFoldRight extends BaseFunctorTest {
34  
35      @Override
36      protected Object makeFunctor() throws Exception {
37          return new FoldRight<Object>(new StringConcatenator());
38      }
39      
40      @Test
41      public void testFoldRight() {
42          FoldRight<Object> foldRight = new FoldRight<Object>(new BinaryFunction<Object, Object, Object>() {
43              public Object evaluate(Object left, Object right) {
44                  StringBuilder buf = left instanceof StringBuilder ? (StringBuilder) left : new StringBuilder().append(left);
45                  return buf.append(right);
46              }
47          });
48          assertEquals("0123456789", foldRight.evaluate(IteratorToGeneratorAdapter.adapt(list.iterator())).toString());
49          assertEquals("0123456789x", foldRight.evaluate(IteratorToGeneratorAdapter.adapt(list.iterator()), "x").toString());
50          assertEquals("x", foldRight.evaluate(IteratorToGeneratorAdapter.adapt(new ArrayList<Object>().iterator()), "x").toString());
51      }
52  
53      // Attributes
54      // ------------------------------------------------------------------------
55      private List<Object> list = Arrays.<Object>asList(0,1,2,3,4,5,6,7,8,9);
56  
57      // Classes
58      // ------------------------------------------------------------------------
59      
60      static class StringConcatenator implements BinaryFunction<Object, Object, Object> {
61  
62          public Object evaluate(Object left, Object right) {
63              StringBuilder buf = left instanceof StringBuilder ? (StringBuilder) left : new StringBuilder().append(left);
64              return buf.append(right);
65          }
66          
67          @Override
68          public boolean equals(Object obj) {
69              if(this == obj) {
70                  return true;
71              }
72              return obj != null && obj instanceof StringConcatenator;
73          }
74          
75          @Override
76          public int hashCode() {
77              return "StringConcatenator".hashCode();
78          }
79      }
80      
81  }