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

ljacqu / wordeval / 14562991048

20 Apr 2025 08:19PM UTC coverage: 55.116% (+1.7%) from 53.367%
14562991048

push

github

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

269 of 568 branches covered (47.36%)

212 of 313 new or added lines in 36 files covered. (67.73%)

6 existing lines in 5 files now uncovered.

738 of 1339 relevant lines covered (55.12%)

3.27 hits per line

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

55.17
/src/main/java/ch/jalu/wordeval/evaluators/impl/AlphabeticalOrder.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.export.EvaluatorExportUtil;
6
import ch.jalu.wordeval.evaluators.result.WordWithScore;
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
 * Filter that saves words whose letters are alphabetical from beginning to end,
18
 * forwards or backwards. For example, in German "einst", each following letter
19
 * comes later in the alphabet.
20
 */
21
public class AlphabeticalOrder implements WordEvaluator {
2✔
22

23
  private static final int FORWARDS = -1;
24
  private static final int BACKWARDS = 1;
25

26
  @Getter
6✔
27
  private final List<WordWithScore> results = new ArrayList<>();
28

29
  @Override
30
  public void evaluate(Word word) {
31
    // TODO #15: Make locale-aware instead
32
    String text = word.getWithoutAccentsWordCharsOnly();
3✔
33
    if (areLettersOrdered(text, FORWARDS) || areLettersOrdered(text, BACKWARDS)) {
8✔
34
      results.add(new WordWithScore(word, text.length()));
11✔
35
    }
36
  }
1✔
37

38
  private static boolean areLettersOrdered(String word, int searchDirection) {
39
    // TODO: Replace Strings with chars
40
    String previousChar = String.valueOf(word.charAt(0));
5✔
41
    for (int i = 1; i < word.length(); ++i) {
8✔
42
      String currentChar = String.valueOf(word.charAt(i));
5✔
43
      int comparison = strcmp(previousChar, currentChar);
4✔
44
      if (comparison == 0 || comparison == searchDirection) {
5✔
45
        previousChar = currentChar;
3✔
46
      } else {
47
        // The comparison is not what we were looking for, so stop
48
        return false;
2✔
49
      }
50
    }
51
    return true;
2✔
52
  }
53

54
  private static int strcmp(String a, String b) {
55
    int comparison = a.compareToIgnoreCase(b);
4✔
56
    return Integer.compare(comparison, 0);
4✔
57
  }
58

59
  @Override
60
  public ListMultimap<Object, Object> getTopResults(int topScores, int maxLimit) {
61
    List<WordWithScore> sortedResult = results.stream()
×
NEW
62
        .sorted(Comparator.comparing(WordWithScore::score).reversed())
×
63
        .toList();
×
64

65
    Set<Double> uniqueValues = new HashSet<>();
×
66
    ListMultimap<Object, Object> filteredResults = EvaluatorExportUtil.newListMultimap();
×
67
    for (WordWithScore wordWithScore : sortedResult) {
×
NEW
68
      if (uniqueValues.add(wordWithScore.score()) && uniqueValues.size() > topScores) {
×
69
        break;
×
70
      }
NEW
71
      filteredResults.put((int) wordWithScore.score(), wordWithScore.word().getRaw());
×
72
      if (filteredResults.size() >= maxLimit) {
×
73
        break;
×
74
      }
75
    }
×
76

77
    return filteredResults;
×
78
  }
79
}
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