View Javadoc
1   package com.guinetik.hexafun.examples.tasks;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.Map;
6   import java.util.Optional;
7   import java.util.concurrent.ConcurrentHashMap;
8   
9   /**
10   * In-memory implementation of TaskRepository.
11   *
12   * <p>This is an adapter that implements the output port.
13   * Useful for testing and demos. In production, you'd have
14   * a JPA, JDBC, or other persistent implementation.
15   */
16  public class InMemoryTaskRepository implements TaskRepository {
17  
18      private final Map<String, Task> tasks = new ConcurrentHashMap<>();
19  
20      @Override
21      public Task save(Task task) {
22          tasks.put(task.id(), task);
23          return task;
24      }
25  
26      @Override
27      public Optional<Task> findById(String id) {
28          return Optional.ofNullable(tasks.get(id));
29      }
30  
31      @Override
32      public List<Task> findAll() {
33          return new ArrayList<>(tasks.values());
34      }
35  
36      @Override
37      public boolean delete(String id) {
38          return tasks.remove(id) != null;
39      }
40  
41      /**
42       * Clear all tasks (useful for testing).
43       */
44      public void clear() {
45          tasks.clear();
46      }
47  
48      /**
49       * Get the count of tasks.
50       */
51      public int count() {
52          return tasks.size();
53      }
54  }