Skip to content

pig-latin: convert to use instance methods #414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import java.util.List;
import java.util.stream.Collectors;

public class PigLatin {
public class PigLatinTranslator {

public static final String AY = "ay";
public static final String THR = "thr";
Expand All @@ -15,7 +15,7 @@ public class PigLatin {
public static final String YT = "yt";
public static final String VOWELS_REGEX = "[aeiou]";

public static String translate(String sentence) {
public String translate(String sentence) {
List<String> translatedWords = Arrays.asList(sentence.split(" "))
.stream()
.map(x -> translateWord(x))
Expand All @@ -24,7 +24,7 @@ public static String translate(String sentence) {
return String.join(" ", translatedWords);
}

private static String translateWord(String word) {
private String translateWord(String word) {
if (wordStartsWithVowelLike(word)) {
return word + AY;
}
Expand All @@ -44,16 +44,16 @@ private static String translateWord(String word) {
return word.substring(1) + word.toCharArray()[0] + AY;
}

private static boolean wordStartsWithVowelLike(String word) {
private boolean wordStartsWithVowelLike(String word) {
return word.startsWith(YT) || word.startsWith(XR) || word.substring(0, 1).matches(VOWELS_REGEX);
}

private static boolean wordStartsWithPrefixes(String word, String... prefixes) {
return Arrays.asList(prefixes).stream()
.anyMatch(x -> word.startsWith(x));
private boolean wordStartsWithPrefixes(String word, String... prefixes) {
return Arrays.stream(prefixes)
.anyMatch(word::startsWith);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both these changes suggested by IntelliJ.

}

private static boolean wordStartsWithConsonantAndQu(String word) {
private boolean wordStartsWithConsonantAndQu(String word) {
return word.substring(1).startsWith(QU);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.junit.Test;
import org.junit.Ignore;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused because of the parametrization.

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

Expand All @@ -9,7 +8,7 @@
import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class PigLatinTest {
public class PigLatinTranslatorTest {

private String englishPhrase;
private String pigLatinTranslation;
Expand Down Expand Up @@ -59,14 +58,14 @@ public static Collection<Object[]> data() {
});
}

public PigLatinTest(String englishPhrase, String pigLatinTranslation) {
public PigLatinTranslatorTest(String englishPhrase, String pigLatinTranslation) {
this.englishPhrase = englishPhrase;
this.pigLatinTranslation = pigLatinTranslation;
}


@Test
public void test() {
assertEquals(pigLatinTranslation, PigLatin.translate(englishPhrase));
assertEquals(pigLatinTranslation, new PigLatinTranslator().translate(englishPhrase));
}

}