Created
May 27, 2016 12:39
-
-
Save marcherdiego/7b1047103d045d7bdaf96c2d355ab294 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.google.ara.app.manager; | |
import java.util.List; | |
public class NameJoiner { | |
private NameJoiner() { | |
} | |
public static String join(List<String> names, int index, String... separators) { | |
if (names.size() == 0) { | |
return ""; | |
} | |
if (names.size() == 1) { | |
return names.get(0); | |
} | |
StringBuilder sb = new StringBuilder(); | |
List<String> tail = names.subList(1, names.size()); | |
String separator = names.size() == 2 | |
? separators[separators.length - 1] | |
: separators[index % (separators.length - 1)]; | |
return sb | |
.append(names.get(0)) | |
.append(separator) | |
.append(join(tail, index + 1, separators)) | |
.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.google.ara.app.manager; | |
import org.junit.Test; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class NameJoinerTest { | |
private final static String JOINER = ", "; | |
private final static String FINAL_JOINER = " and "; | |
private List<String> zeroModules = new ArrayList<>(); | |
private List<String> oneModule = Arrays.asList("13MP Camera"); | |
private List<String> twoModules = Arrays.asList("13MP Camera", "Harman Speaker"); | |
private List<String> manyModules = Arrays.asList("13MP Camera", "Harman Speaker", "Hi fi Speaker", "Micron Storage"); | |
@Test | |
public void testZeroModuleNames() { | |
assertThat(NameJoiner.join(zeroModules, 0, JOINER, FINAL_JOINER)).isEqualTo(""); | |
} | |
@Test | |
public void testOneModuleName() { | |
assertThat(NameJoiner.join(oneModule, 0, JOINER, FINAL_JOINER)).isEqualTo("13MP Camera"); | |
} | |
@Test | |
public void testTwoModuleNames() { | |
assertThat(NameJoiner.join(twoModules, 0, JOINER, FINAL_JOINER)).isEqualTo("13MP Camera and Harman Speaker"); | |
} | |
@Test | |
public void testManyModuleNames() { | |
assertThat(NameJoiner.join(manyModules, 0, JOINER, FINAL_JOINER)) | |
.isEqualTo("13MP Camera, Harman Speaker, Hi fi Speaker and Micron Storage"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment