-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_task.java
More file actions
98 lines (91 loc) · 3.48 KB
/
test_task.java
File metadata and controls
98 lines (91 loc) · 3.48 KB
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
import lombok.Builder;
import lombok.Data;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
* For implement this task focus on clear code, and make this solution as simple readable as possible
* Don't worry about performance, concurrency, etc
* You can use in Memory collection for sore data
* <p>
* Please, don't change class name, and signature for methods save, search, findById
* Implementations should be in a single class
* This class could be auto tested
* -------------------------------------------------------------------------------------------------
* @author Ruslan Chekina
* @version 0.1
* @since 2024-08-06
*/
public class DocumentManager {
private final AtomicLong idCounter = new AtomicLong();
private final List<Document> documents = new ArrayList<>();
/**
* Implementation of this method should upsert the document to your storage
* And generate unique id if it does not exist, don't change [created] field
*
* @param document - document content and author data
* @return saved document
*/
public Document save(Document document) {
if (document.getId() = null) {
document.setId(String.valueOf(idCounter.incrementAndGet()));
document.setCreated(Instant.now())
} else {
documents.removeIf(doc -> doc.getId().equals(document.getId()));
}
documents.add(document);
return document;
}
/**
* Implementation this method should find documents which match with request
*
* @param request - search request, each field could be null
* @return list matched documents
*/
public List<Document> search(SearchRequest request) {
return documents.stream()
.filter(document ->
(request.getTitlePrefixes() == null || request.getTitlePrefixes().stream().anyMatch(prefix -> document.getTitle().startsWith(prefix))) &&
(request.getContainsContents() == null || request.getContainsContents().stream().anyMatch(content -> document.getContent().contains(content))) &&
(request.getAuthorIds() == null || request.getAuthorIds().contains(document.getAuthor().getId())) &&
(request.getCreatedFrom() == null || !document.getCreated().isBefore(request.getCreatedFrom())) &&
(request.getCreatedTo() == null || !document.getCreated().isAfter(request.getCreatedTo()))
).collect(Collectors.toList());
}
/**
* Implementation this method should find document by id
*
* @param id - document id
* @return optional document
*/
public Optional<Document> findById(String id) {
return documents.stream()
.filter(document -> document.getId().equals(id))
.findFirst();
}
@Data
@Builder
public static class SearchRequest {
private List<String> titlePrefixes;
private List<String> containsContents;
private List<String> authorIds;
private Instant createdFrom;
private Instant createdTo;
}
@Data
@Builder
public static class Document {
private String id;
private String title;
private String content;
private Author author;
private Instant created;
}
@Data
@Builder
public static class Author {
private String id;
private String name;
}
}