-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathTransientTaskServiceImpl2.java
107 lines (86 loc) · 3.17 KB
/
TransientTaskServiceImpl2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.vogella.tasks.services.internal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import jakarta.inject.Inject;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.osgi.service.component.annotations.Component;
import com.vogella.tasks.events.TaskEventConstants;
import com.vogella.tasks.model.Task;
import com.vogella.tasks.model.TaskService;
@Component(property = { "service.ranking:Integer=8" })
public class TransientTaskServiceImpl2 implements TaskService {
@Inject
private IEventBroker broker;
private static AtomicInteger current = new AtomicInteger(1);
private List<Task> tasks;
public TransientTaskServiceImpl2() {
tasks = createTestData();
}
@Override
public void consume(Consumer<List<Task>> taskConsumer) {
// always pass a new copy of the data
taskConsumer.accept(tasks.stream().map(Task::copy).collect(Collectors.toList()));
}
// create or update an existing instance of object
@Override
public synchronized boolean update(Task newTask) {
// hold the Optional object as reference to determine, if the object is
// newly created or not
Optional<Task> taskOptional = findById(newTask.getId());
// get the actual object or create a new one
Task task = taskOptional.orElse(new Task(current.getAndIncrement()));
task.setSummary(newTask.getSummary());
task.setDescription(newTask.getDescription());
task.setDone(newTask.isDone());
task.setDueDate(newTask.getDueDate());
if (!taskOptional.isPresent()) {
tasks.add(task);
broker.post(TaskEventConstants.TOPIC_TASKS_NEW, Map.of(TaskEventConstants.TOPIC_TASKS_NEW,
TaskEventConstants.TOPIC_TASKS_NEW, Task.FIELD_ID, task.getId()));
} else {
broker.post(TaskEventConstants.TOPIC_TASKS_UPDATE, Map.of(TaskEventConstants.TOPIC_TASKS,
TaskEventConstants.TOPIC_TASKS_UPDATE, Task.FIELD_ID, task.getId()));
}
return true;
}
@Override
public Optional<Task> get(long id) {
return findById(id).map(Task::copy);
}
@Override
public boolean delete(long id) {
Optional<Task> deletedTask = findById(id);
deletedTask.ifPresent(t -> {
tasks.remove(t);
broker.post(TaskEventConstants.TOPIC_TASKS_DELETE, Map.of(TaskEventConstants.TOPIC_TASKS,
TaskEventConstants.TOPIC_TASKS_DELETE, Task.FIELD_ID, t.getId()));
});
return deletedTask.isPresent();
}
// Example data, change if you like
private List<Task> createTestData() {
List<Task> list = new ArrayList<>();
if (list.isEmpty()) {
// Create some initial content
list = List.of(create("Application model", "Flexible and extensible"),
create("Compatibility Layer", "Run Eclipse 3.x"));
}
return new ArrayList<>(list);
}
private Task create(String summary, String description) {
return new Task(current.getAndIncrement(), summary, description, false, LocalDate.now());
}
private Optional<Task> findById(long id) {
return tasks.stream().filter(t -> t.getId() == id).findAny();
}
@Override
public List<Task> getAll() {
return tasks.stream().map(Task::copy).collect(Collectors.toList());
}
}