View Javadoc

1   /*
2      Copyright 2011 Atlassian
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package io.atlassian.fugue;
17  
18  import org.junit.Test;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.function.Function;
23  
24  import static java.util.Arrays.asList;
25  import static org.hamcrest.MatcherAssert.assertThat;
26  import static org.hamcrest.Matchers.contains;
27  
28  public class IterableVarianceTest {
29  
30    @Test public void flatMap() {
31      final Iterable<String> result = Iterables.flatMap(asList("123", "ABC"), new Function<CharSequence, List<String>>() {
32        public List<String> apply(final CharSequence from) {
33          return copyOf(new IterablesTest.CharSplitter(from));
34        }
35      });
36      assertThat(result, contains("1", "2", "3", "A", "B", "C"));
37    }
38  
39    // replaces ImmutableList.copyOf
40    private List<String> copyOf(final IterablesTest.CharSplitter strings) {
41      final List<String> copy = new ArrayList<>();
42      for (String s : strings) {
43        copy.add(s);
44      }
45      return copy;
46    }
47  }