-
Notifications
You must be signed in to change notification settings - Fork 103
Lesson_37 (collection list) - for review. #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Binary-Cat-01
wants to merge
3
commits into
KFalcon2022:for-pr
Choose a base branch
from
Binary-Cat-01:lesson_37_collection_list_for_review
base: for-pr
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| # Project exclude paths | ||
| /out/ | ||
| /out/ | ||
| /resource/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/com/walking/lesson37_collection_list/task1/model/Counter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.walking.lesson37_collection_list.task1.model; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Counter implements Comparable<Counter> { | ||
| private final String name; | ||
| private final String unitOfMeasure; | ||
|
|
||
| private int value; | ||
|
|
||
| public Counter(String name, String unitOfMeasure, int value) { | ||
| this.name = name; | ||
| this.unitOfMeasure = unitOfMeasure; | ||
| this.value = value; | ||
| } | ||
|
|
||
| public Counter(String name, String unitOfMeasure) { | ||
| this(name, unitOfMeasure, 0); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getUnitOfMeasure() { | ||
| return unitOfMeasure; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| public void setValue(int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(Counter counter) { | ||
| return name.compareTo(counter.getName()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| Counter counter = (Counter) o; | ||
| return Objects.equals(name, counter.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return name.hashCode(); | ||
| } | ||
| } |
167 changes: 167 additions & 0 deletions
167
src/com/walking/lesson37_collection_list/task1/service/CounterService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package com.walking.lesson37_collection_list.task1.service; | ||
|
|
||
| import com.walking.lesson37_collection_list.task1.model.Counter; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class CounterService { | ||
| /*// замена ArrayList на Vector не повлияла на кодовую базу, т.к. они оба реализуют интерфейс List, | ||
| // что делает их взаимозаменяемыми | ||
| private final Vector<Counter> counters; | ||
|
|
||
| public CounterService(Counter... counters) { | ||
| this.counters = new Vector<>(Arrays.asList(counters)); | ||
| } */ | ||
|
|
||
| private final ArrayList<Counter> counters; | ||
|
|
||
| public CounterService(List<Counter> counters) { | ||
| this.counters = new ArrayList<>(counters); | ||
| } | ||
|
|
||
| public List<Counter> getAllCounters() { | ||
| return List.copyOf(counters); | ||
| } | ||
|
|
||
| public Counter getFirst() { | ||
| return counters.get(0); | ||
| } | ||
|
|
||
| public void addFirst(Counter counter) { | ||
| counters.add(0, counter); | ||
| } | ||
|
|
||
| public Counter addCounter(Counter counter) { | ||
| counters.add(counter); | ||
| return counters.get(counters.size() - 1); | ||
| } | ||
|
|
||
| public boolean addCountersIfAbsent(Counter... counters) { | ||
| List<Counter> candidates = new ArrayList<>(Arrays.asList(counters)); | ||
| candidates.removeAll(this.counters); | ||
| return this.counters.addAll(candidates); | ||
| } | ||
|
|
||
| public boolean removeIfNotMatch(Collection<? extends Counter> sample) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. чтобы потыкать функциональность листов - норм, но в целом такая семантика вызывает вопросы |
||
| return counters.retainAll(sample); | ||
| } | ||
|
|
||
| public boolean removeDuplicates() { | ||
| List<Counter> uniques = getUniques(); | ||
|
|
||
| if (uniques.size() == counters.size()) { | ||
| return false; | ||
| } | ||
|
|
||
| counters.clear(); | ||
| counters.addAll(uniques); | ||
| return true; | ||
| } | ||
|
|
||
| public boolean removeIdleCounters() { | ||
| List<Counter> idles = getIdles(); | ||
|
|
||
| if (idles.size() == 0) { | ||
| return false; | ||
| } | ||
|
|
||
| counters.removeAll(idles); | ||
| return true; | ||
| } | ||
|
|
||
| public void sortByName() { | ||
| counters.sort(Comparator.naturalOrder()); | ||
| } | ||
|
|
||
| public void sortBy(Comparator<Counter> comparator) { | ||
| counters.sort(comparator); | ||
| } | ||
|
|
||
| public Counter getCounterByName(String name) { | ||
| for (Counter counter : counters) { | ||
| if (counter.getName().equals(name)) { | ||
| return counter; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public Counter increaseCounter(String name, int value) { | ||
| Counter counter = getCounterByName(name); | ||
| return counter == null ? null : increaseCounter(counter, value); | ||
| } | ||
|
|
||
| public Counter increaseCounter(Counter counter, int value) { | ||
| counter.setValue(counter.getValue() + value); | ||
| return counter; | ||
| } | ||
|
|
||
| public Counter decreaseCounter(String name, int value) { | ||
| Counter counter = getCounterByName(name); | ||
| return counter == null ? null : decreaseCounter(counter, value); | ||
| } | ||
|
|
||
| public Counter decreaseCounter(Counter counter, int value) { | ||
| counter.setValue(counter.getValue() - value); | ||
| return counter; | ||
| } | ||
|
|
||
| public Counter incrementCounter(String name) { | ||
| Counter counter = getCounterByName(name); | ||
| return counter == null ? null : incrementCounter(counter); | ||
| } | ||
|
|
||
| public Counter incrementCounter(Counter counter) { | ||
| increaseCounter(counter, 1); | ||
| return counter; | ||
| } | ||
|
|
||
| public Counter decrementCounter(String name) { | ||
| Counter counter = getCounterByName(name); | ||
| return counter == null ? null : decrementCounter(counter); | ||
| } | ||
|
|
||
| public Counter decrementCounter(Counter counter) { | ||
| decreaseCounter(counter, 1); | ||
| return counter; | ||
| } | ||
|
|
||
| public Counter reset(String name) { | ||
| Counter counter = getCounterByName(name); | ||
| return counter == null ? null : reset(counter); | ||
| } | ||
|
|
||
| public Counter reset(Counter counter) { | ||
| counter.setValue(0); | ||
| return counter; | ||
| } | ||
|
|
||
| private List<Counter> getUniques() { | ||
| List<Counter> uniques = new ArrayList<>(); | ||
|
|
||
| for (Counter counter : counters) { | ||
| if (!uniques.contains(counter)) { | ||
| uniques.add(counter); | ||
| } | ||
| } | ||
|
|
||
| return uniques; | ||
| } | ||
|
|
||
| private List<Counter> getIdles() { | ||
| List<Counter> idles = new ArrayList<>(); | ||
|
|
||
| for (Counter counter : counters) { | ||
| if (isIdle(counter)) { | ||
| idles.add(counter); | ||
| } | ||
| } | ||
|
|
||
| return idles; | ||
| } | ||
|
|
||
| private boolean isIdle(Counter counter) { | ||
| return counter.getValue() == 0; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
с 21 джавы можно проще