Skip to content

Commit

Permalink
Изменён метод getLetters()
Browse files Browse the repository at this point in the history
  • Loading branch information
kalenchukov committed Aug 5, 2022
1 parent b559130 commit 5a4df26
Showing 1 changed file with 10 additions and 33 deletions.
43 changes: 10 additions & 33 deletions src/main/java/dev/kalenchukov/alphabet/AbstractAlphabet.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ protected AbstractAlphabet(@NotNull final List<@NotNull Character> letters)
Objects.requireNonNull(from);
Objects.requireNonNull(to);

return this.getLetters(this.letters, from, to);
if (from > to) {
throw new IllegalArgumentException();
}

if (from < 1 || to > this.letters.size()) {
throw new IndexOutOfBoundsException();
}

return Collections.unmodifiableList(this.letters.subList(from - 1, to));
}

/**
Expand All @@ -69,45 +77,14 @@ public Character getLetter(@NotNull final Integer position)

try
{
return this.getLetters(this.letters, position, position).get(0);
return this.getLetters(position, position).get(0);
}
catch (IllegalArgumentException | IndexOutOfBoundsException exception)
{
return null;
}
}

/**
* Возвращает срез букв алфавита.
*
* @param alphabet Алфавит.
* @param from Начальная позиция букв в алфавите.
* @param to Конечная позиция букв в алфавите.
* @return Коллекция из среза букв алфавита.
* @throws IllegalArgumentException Если начальная позиция {@code from} больше {@code to}.
* @throws IndexOutOfBoundsException Если позиция {@code from} или {@code to} выходят за пределы алфавита.
*/
@Unmodifiable
@NotNull
protected List<@NotNull Character> getLetters(@NotNull final List<@NotNull Character> alphabet,
@NotNull final Integer from,
@NotNull final Integer to)
{
Objects.requireNonNull(alphabet);
Objects.requireNonNull(from);
Objects.requireNonNull(to);

if (from > to) {
throw new IllegalArgumentException();
}

if (from < 1 || to > alphabet.size()) {
throw new IndexOutOfBoundsException();
}

return Collections.unmodifiableList(alphabet.subList(from - 1, to));
}

/**
* @see Alphabetical#hasLetter(Character)
*/
Expand Down

0 comments on commit 5a4df26

Please sign in to comment.