From 5a4df267f353ce7dd6ff067b4d7a029315dcdc51 Mon Sep 17 00:00:00 2001 From: Aleksey Kalenchukov Date: Fri, 5 Aug 2022 09:42:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D1=91=D0=BD=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20getLetters()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alphabet/AbstractAlphabet.java | 43 +++++-------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/src/main/java/dev/kalenchukov/alphabet/AbstractAlphabet.java b/src/main/java/dev/kalenchukov/alphabet/AbstractAlphabet.java index 33ce466..3500c25 100644 --- a/src/main/java/dev/kalenchukov/alphabet/AbstractAlphabet.java +++ b/src/main/java/dev/kalenchukov/alphabet/AbstractAlphabet.java @@ -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)); } /** @@ -69,7 +77,7 @@ 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) { @@ -77,37 +85,6 @@ public Character getLetter(@NotNull final Integer position) } } - /** - * Возвращает срез букв алфавита. - * - * @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) */