Lesson_37 (collection list) - for review.#88
Lesson_37 (collection list) - for review.#88Binary-Cat-01 wants to merge 3 commits intoKFalcon2022:for-prfrom
Conversation
|
|
||
| public static void main(String[] args) { | ||
| CounterService counterService = new CounterService(); | ||
| counterService.displayContent(); |
There was a problem hiding this comment.
замечание то же, что и ранее: сервис не должен отвечать за способ вывода
|
|
||
| Counter[] allCounters = new Counter[counterService.getAllCounters().size() + 5]; | ||
|
|
||
| counterService.getAllCounters().toArray(allCounters); |
There was a problem hiding this comment.
предсоздавать массив в данном случае не обязательно
| System.out.println("Названия счетчиков в массиве:"); | ||
|
|
||
| for (Counter counter : counters) { | ||
| if (counter == null) { |
There was a problem hiding this comment.
тернарка смотрелась бы лаконичнее
|
|
||
| private final ArrayList<Counter> counters; | ||
|
|
||
| public CounterService(Counter... counters) { |
There was a problem hiding this comment.
Вполне можно и параметром принимать лист. Опционально
| } | ||
|
|
||
| public List<Counter> getAllCounters() { | ||
| return counters; |
There was a problem hiding this comment.
на будущее - зачастую хорошей практикой принимается отдавать копию коллекции - на случай, если необходимо ограничить бесконтрольное изменение такого хранилища. Тогда добавление и иные изменения коллекции ограничены апи управляющего класса (здесь - сервиса), а на чтение всегда будет отдаваться копия, изменение которой не повлияет на основную коллекцию
| } | ||
|
|
||
| public Counter getFirst() { | ||
| return counters.get(0); |
| return this.counters.addAll(candidates); | ||
| } | ||
|
|
||
| public boolean removeIfNotMatch(Collection<? extends Counter> sample) { |
There was a problem hiding this comment.
чтобы потыкать функциональность листов - норм, но в целом такая семантика вызывает вопросы
| List<Counter> uniques = new ArrayList<>(); | ||
|
|
||
| for (Counter counter : counters) { | ||
| if (!uniques.contains(counter)) { |
There was a problem hiding this comment.
у тебя же вроде не переопределен equals(). Будет криво работать
| return uniques; | ||
| } | ||
|
|
||
| public boolean removeAllZeroValue() { |
There was a problem hiding this comment.
непонятное название, смысл становится прозрачным только после погружения в тело метода
| return true; | ||
| } | ||
|
|
||
| private List<Counter> getZeroValues() { |
There was a problem hiding this comment.
советую располагать методы в соответствие с модификатором доступа - сначала публичные, потом протектед и т.д.
| public Counter increaseCounter(String name, int value) { | ||
| Counter counter = getCounterByName(name); | ||
|
|
||
| if (counter == null) { |
| public Counter decreaseCounter(String name, int value) { | ||
| Counter counter = getCounterByName(name); | ||
|
|
||
| if (counter == null) { |
| public Counter incrementCounter(String name) { | ||
| Counter counter = getCounterByName(name); | ||
|
|
||
| if (counter == null) { |
| public void displayContent() { | ||
| System.out.printf("Всего счетчиков: %s\n%s\n", counters.size(), "-".repeat(20)); | ||
|
|
||
| if (counters.isEmpty()) { |
| this.next = next; | ||
| } | ||
| } | ||
| } |
No description provided.