Skip to content

Commit

Permalink
Fix authIni1
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Apr 24, 2023
1 parent 60a345f commit 0e0c04b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -998,25 +998,21 @@ static String authIniN(AuthorList authorList, int n) {
return "";
}

if (authorList.getNumberOfAuthors() >= n) {
// We have to spend fewer letters than the complete length
// Fall back to authorsAlpha to have the letters and "+" correctly handled
return authorsAlpha(authorList);
}

if ((authorList.getAuthor(authorList.getNumberOfAuthors() - 1).equals(Author.OTHERS))) {
final int numberOfAuthors = authorList.getNumberOfAuthors();
final boolean lastAuthorIsOthers = authorList.getAuthor(numberOfAuthors - 1).equals(Author.OTHERS);
if ((n > 1) && ((n < numberOfAuthors) || lastAuthorIsOthers)) {
final int limit = Math.min(n - 1, numberOfAuthors - 1);
// special handling if the last author is "Others"
// This gets the single char "+" only
AuthorList allButOthers = AuthorList.of(
authorList.getAuthors()
.stream()
.limit(authorList.getNumberOfAuthors() - 1)
.limit(limit)
.toList());
return authIniN(allButOthers, n - 1) + "+";
}

StringBuilder author = new StringBuilder();
final int numberOfAuthors = authorList.getNumberOfAuthors();

int charsAll = n / numberOfAuthors;
for (int i = 0; i < numberOfAuthors; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ static Stream<Arguments> authShort() {
Arguments.of("ABC+", "Aachen and Berlin and Chemnitz and Düsseldorf and Essen and others"));
}

@ParameterizedTest
@MethodSource
void authIni1(String expected, AuthorList list) {
assertEquals(expected, BracketedPattern.authIniN(list, 1));
}

static Stream<Arguments> authIni1() {
return Stream.of(
Arguments.of("N", "Isaac Newton"),
Arguments.of("N", "Isaac Newton and James Maxwell"),
Arguments.of("N", "Isaac Newton and James Maxwell and Albert Einstein"),
Arguments.of("N", "Isaac Newton and James Maxwell and Albert Einstein and N. Bohr"),
Arguments.of("A", "Aachen"),
Arguments.of("A", "Aachen and others"),
Arguments.of("A", "Aachen and Berlin"),
Arguments.of("A", "Aachen and Berlin and others"),
Arguments.of("A", "Aachen and Berlin and Chemnitz"),
Arguments.of("A", "Aachen and Berlin and Chemnitz and others"),
Arguments.of("A", "Aachen and Berlin and Chemnitz and Düsseldorf"),
Arguments.of("A", "Aachen and Berlin and Chemnitz and Düsseldorf and others"),
Arguments.of("A", "Aachen and Berlin and Chemnitz and Düsseldorf and Essen"),
Arguments.of("A", "Aachen and Berlin and Chemnitz and Düsseldorf and Essen and others"));
}

@ParameterizedTest
@MethodSource
void authIni4(String expected, AuthorList list) {
Expand Down

0 comments on commit 0e0c04b

Please sign in to comment.