aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/internal/documents/Notes.java
blob: d7de8a635c73a3af97591203dc69ba4f22dcdb04 (plain)
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
package org.zwobble.mammoth.internal.documents;

import com.google.common.collect.Maps;
import com.google.common.collect.Multimaps;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.zwobble.mammoth.internal.util.MammothLists.list;
import static org.zwobble.mammoth.internal.util.MammothMaps.lookup;

public class Notes {
    public final static Notes EMPTY = new Notes(list());

    private final Map<NoteType, Map<String, Note>> notes;

    public Notes(List<Note> notes) {
        this.notes = Maps.transformValues(
            Multimaps.index(notes, Note::getNoteType).asMap(),
            notesOfType -> Maps.uniqueIndex(notesOfType, Note::getId));
    }

    public Optional<Note> findNote(NoteType noteType, String noteId) {
        return lookup(notes, noteType)
            .flatMap(notesOfType -> lookup(notesOfType, noteId));
    }
}