• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ljacqu / wordeval / 14540892325

18 Apr 2025 07:50PM UTC coverage: 51.4% (-12.1%) from 63.456%
14540892325

push

github

ljacqu
Merge remote-tracking branch 'origin/master' into dependencies

239 of 546 branches covered (43.77%)

93 of 383 new or added lines in 27 files covered. (24.28%)

5 existing lines in 4 files now uncovered.

679 of 1321 relevant lines covered (51.4%)

3.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.86
/src/main/java/ch/jalu/wordeval/evaluators/impl/Palindromes.java
1
package ch.jalu.wordeval.evaluators.impl;
2

3
import ch.jalu.wordeval.dictionary.Word;
4
import ch.jalu.wordeval.evaluators.WordEvaluator;
5
import ch.jalu.wordeval.evaluators.result.WordWithKey;
6
import com.google.common.collect.ArrayListMultimap;
7
import com.google.common.collect.ListMultimap;
8
import lombok.Getter;
9

10
import java.util.ArrayList;
11
import java.util.Comparator;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Set;
15

16
/**
17
 * Finds palindromes or palindrome-parts inside a word, e.g. "awkwa" in
18
 * "awkward".
19
 */
20
public class Palindromes implements WordEvaluator {
2✔
21

22
  @Getter
6✔
23
  private final List<WordWithKey> results = new ArrayList<>();
24

25
  @Override
26
  public void evaluate(Word wordObject) {
27
    String word = wordObject.getWithoutAccentsWordCharsOnly();
3✔
28
    for (int i = 1; i < word.length() - 1; ++i) {
10✔
29
      String palindrome = findPalindrome(word, i);
4✔
30
      if (palindrome != null) {
2✔
31
        results.add(new WordWithKey(wordObject, palindrome));
9✔
32
      }
33
    }
34
  }
1✔
35

36
  /**
37
   * Returns the palindrome part in `word` with center `index` if there is one.
38
   * @param word The word to process
39
   * @param index The index to test at
40
   * @return The palindrome part, or null if none found
41
   */
42
  private static String findPalindrome(String word, int index) {
43
    // Asymmetrical palindromes like "awkwa"
44
    String palindrome = findPalindrome(word, index, 1);
5✔
45
    if (palindrome != null) {
2✔
46
      return palindrome;
2✔
47
    }
48
    // Symmetrical palindromes like "abba"
49
    palindrome = findPalindrome(word, index, 0);
5✔
50
    if (palindrome != null) {
2✔
51
      return palindrome;
2✔
52
    }
53
    return null;
2✔
54
  }
55

56
  /**
57
   * Checks for palindrome part in a word with `index` as middle. E.g. with word
58
   * as "awkward" it returns "awkwa" for index = 2 and offset = 1, null for all
59
   * other offsets.
60
   * @param word The word to process
61
   * @param index The index to test at
62
   * @param offset 0 or 1: for "abba"-like or "awkwa"-like palindromes
63
   * @return The palindrome part, or null if none found
64
   */
65
  private static String findPalindrome(String word, int index, int offset) {
66
    int i = index - 1, j = index + offset;
8✔
67
    for (; i >= 0 && j < word.length(); --i, ++j) {
9!
68
      if (word.charAt(i) != word.charAt(j)) {
7✔
69
        break;
1✔
70
      }
71
    }
72
    ++i;
1✔
73
    --j;
1✔
74

75
    // i < j - 1 ensures that a simple pair like "oo" in "cool" does not get
76
    // added: in this case, i = 1, j = 2; so i < j - 1 == false
77
    if (i < j - 1) {
5✔
78
      return word.substring(i, j + 1);
7✔
79
    }
80
    return null;
2✔
81
  }
82

83
  @Override
84
  public ListMultimap<Object, Object> getTopResults(int topScores, int maxLimit) {
NEW
85
    List<WordWithKey> sortedResult = results.stream()
×
NEW
86
        .sorted(Comparator.comparing((WordWithKey wwk) -> wwk.getKey().length()).reversed())
×
NEW
87
        .toList();
×
88

NEW
89
    Set<String> uniqueValues = new HashSet<>();
×
NEW
90
    ListMultimap<Object, Object> filteredResults = ArrayListMultimap.create();
×
NEW
91
    for (WordWithKey WordWithKey : sortedResult) {
×
NEW
92
      if (uniqueValues.add(WordWithKey.getKey()) && uniqueValues.size() > topScores) {
×
NEW
93
        break;
×
94
      }
NEW
95
      filteredResults.put(WordWithKey.getKey(), WordWithKey.getWord().getRaw());
×
NEW
96
      if (filteredResults.size() >= maxLimit) {
×
NEW
97
        break;
×
98
      }
NEW
99
    }
×
100

NEW
101
    return filteredResults;
×
102
  }
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc