-
Notifications
You must be signed in to change notification settings - Fork 0
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
Спринт 6. Изменения хранения истории просмотра задач на LinkedList и … #1
Changes from 5 commits
4c7bac5
9f6d64c
3894507
8695b7b
73be755
5669688
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# java-kanban | ||
Repository for homework project. | ||
Sprint 6. | ||
Необходимо: | ||
- Сделать историю посещений неограниченной по размеру. | ||
- Избавиться от повторных просмотров в истории. Если какую-либо задачу посещали несколько раз, то в истории должен остаться только её последний просмотр. Предыдущий должен быть удалён. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,28 +3,72 @@ | |
import model.Task; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class InMemoryHistoryManager implements HistoryManager { | ||
private static final int SIZE_HISTORY = 10; | ||
private final List<Task> history = new ArrayList<>(SIZE_HISTORY); | ||
|
||
private void removeHistoryFirst() { | ||
if (!history.isEmpty() && history.size() == SIZE_HISTORY) { | ||
history.removeFirst(); | ||
} | ||
} | ||
private Node head = null; | ||
private Node tail = null; | ||
private final Map<Integer, Node> listHistory = new HashMap<>(); | ||
|
||
@Override | ||
public List<Task> getHistory() { | ||
return new ArrayList<>(history); | ||
return getTasks(); | ||
} | ||
|
||
@Override | ||
public void addInHistory(Task task) { | ||
if (task != null) { | ||
removeHistoryFirst(); | ||
history.add(task); | ||
linkLast(task); | ||
} | ||
} | ||
|
||
private void linkLast(Task task) { | ||
Node newNode = new Node(task, null, this.tail); | ||
if (this.tail == null) { | ||
this.head = newNode; | ||
} else { | ||
tail.setNext(newNode); | ||
} | ||
this.tail = newNode; | ||
removeFromHistory(task.getId()); | ||
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. Обязательно |
||
listHistory.put(task.getId(), newNode); | ||
} | ||
|
||
@Override | ||
public void removeFromHistory(int id) { | ||
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. Обязательно |
||
if (listHistory.containsKey(id)) { | ||
removeNode(listHistory.get(id)); | ||
} | ||
} | ||
|
||
private List<Task> getTasks() { | ||
List<Task> listHistoryRes = new ArrayList<>(); | ||
Node node = head; | ||
while (node != null) { | ||
listHistoryRes.add(node.getItem()); | ||
node = node.getNext(); | ||
} | ||
return listHistoryRes; | ||
} | ||
|
||
private void removeNode(Node node) { | ||
if (node != null) { | ||
Node nodeNext = node.getNext(); | ||
Node nodePrev = node.getPrev(); | ||
if (nodePrev == null) { | ||
head = nodeNext; | ||
head.setPrev(null); | ||
} else { | ||
nodePrev.setNext(nodeNext); | ||
} | ||
if (nodeNext == null) { | ||
tail = nodePrev; | ||
tail.setNext(null); | ||
} else { | ||
nodeNext.setPrev(nodePrev); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package service; | ||
|
||
import model.Task; | ||
|
||
public class Node { | ||
private Task item; | ||
private Node next; | ||
private Node prev; | ||
|
||
public Node(Task item, Node next, Node prev) { | ||
this.item = item; | ||
this.next = next; | ||
this.prev = prev; | ||
} | ||
|
||
public Task getItem() { | ||
return item; | ||
} | ||
|
||
public void setItem(Task item) { | ||
this.item = item; | ||
} | ||
|
||
public Node getNext() { | ||
return next; | ||
} | ||
|
||
public void setNext(Node next) { | ||
this.next = next; | ||
} | ||
|
||
public Node getPrev() { | ||
return prev; | ||
} | ||
|
||
public void setPrev(Node prev) { | ||
this.prev = prev; | ||
} | ||
} |
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.
Можно лучше
Писать this не обязательно везде, если нет противоречий с названий переменных. Во вторых у вас например в 32 строчке без this указано, в других с this, лучше чтобы было одинаково и без this