From a07cddd98e2fdad40d2226818b04be1787abc6cf Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 14 Feb 2016 14:27:47 +0000 Subject: Add notes to document --- .../org/zwobble/mammoth/documents/Document.java | 8 ++++++- .../java/org/zwobble/mammoth/documents/Note.java | 12 +++++++++- .../java/org/zwobble/mammoth/documents/Notes.java | 27 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/zwobble/mammoth/documents/Notes.java (limited to 'src/main/java/org/zwobble/mammoth/documents') diff --git a/src/main/java/org/zwobble/mammoth/documents/Document.java b/src/main/java/org/zwobble/mammoth/documents/Document.java index 4d91925..c3a14d3 100644 --- a/src/main/java/org/zwobble/mammoth/documents/Document.java +++ b/src/main/java/org/zwobble/mammoth/documents/Document.java @@ -4,12 +4,18 @@ import java.util.List; public class Document implements HasChildren { private final List children; + private final Notes notes; - public Document(List children) { + public Document(List children, Notes notes) { this.children = children; + this.notes = notes; } public List getChildren() { return children; } + + public Notes getNotes() { + return notes; + } } diff --git a/src/main/java/org/zwobble/mammoth/documents/Note.java b/src/main/java/org/zwobble/mammoth/documents/Note.java index 1a0d3c0..da6f68f 100644 --- a/src/main/java/org/zwobble/mammoth/documents/Note.java +++ b/src/main/java/org/zwobble/mammoth/documents/Note.java @@ -3,11 +3,21 @@ package org.zwobble.mammoth.documents; import java.util.List; public class Note { + private final NoteType noteType; private final String id; private final List body; - public Note(String id, List body) { + public Note(NoteType noteType, String id, List body) { + this.noteType = noteType; this.id = id; this.body = body; } + + public NoteType getNoteType() { + return noteType; + } + + public String getId() { + return id; + } } diff --git a/src/main/java/org/zwobble/mammoth/documents/Notes.java b/src/main/java/org/zwobble/mammoth/documents/Notes.java new file mode 100644 index 0000000..2959bb0 --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/documents/Notes.java @@ -0,0 +1,27 @@ +package org.zwobble.mammoth.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.util.MammothLists.list; + +public class Notes { + public final static Notes EMPTY = new Notes(list()); + + private final Map> notes; + + public Notes(List notes) { + this.notes = Maps.transformValues( + Multimaps.index(notes, Note::getNoteType).asMap(), + notesOfType -> Maps.uniqueIndex(notesOfType, Note::getId)); + } + + public Optional findNote(NoteType noteType, String noteId) { + return Optional.ofNullable(notes.get(noteType)) + .flatMap(notesOfType -> Optional.ofNullable(notesOfType.get(noteId))); + } +} -- cgit v1.2.3