diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-03-25 16:43:24 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-03-25 16:43:24 +0000 |
| commit | 0deccc08a51e68eb97ad6ef5a95bfd868a939169 (patch) | |
| tree | f9c80cdf1f450a1d9937e65b2d07c554b748c4f2 /src/main | |
| parent | 5b2235620d33776d0a1dcae8f9e68693d9390be3 (diff) | |
Remove use of Google collections in Notes
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/internal/documents/Notes.java | 11 | ||||
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java | 30 |
2 files changed, 31 insertions, 10 deletions
diff --git a/src/main/java/org/zwobble/mammoth/internal/documents/Notes.java b/src/main/java/org/zwobble/mammoth/internal/documents/Notes.java index d7de8a6..fdfa814 100644 --- a/src/main/java/org/zwobble/mammoth/internal/documents/Notes.java +++ b/src/main/java/org/zwobble/mammoth/internal/documents/Notes.java @@ -1,14 +1,11 @@ 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; +import static org.zwobble.mammoth.internal.util.MammothMaps.*; public class Notes { public final static Notes EMPTY = new Notes(list()); @@ -16,9 +13,9 @@ public class Notes { 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)); + this.notes = eagerMapValues( + toMultiMapWithKey(notes, Note::getNoteType), + notesOfType -> toMapWithKey(notesOfType, Note::getId)); } public Optional<Note> findNote(NoteType noteType, String noteId) { diff --git a/src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java b/src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java index 19fbfc4..873f90c 100644 --- a/src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java +++ b/src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java @@ -2,9 +2,7 @@ package org.zwobble.mammoth.internal.util; import com.google.common.collect.ImmutableMap; -import java.util.AbstractMap; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.function.Function; public class MammothMaps { @@ -32,6 +30,10 @@ public class MammothMaps { return Optional.ofNullable(map.get(key)); } + public static <T, K> Map<K, T> toMapWithKey(Iterable<T> iterable, Function<T, K> function) { + return toMap(iterable, element -> entry(function.apply(element), element)); + } + public static <T, K, V> Map<K, V> toMap(Iterable<T> iterable, Function<T, Map.Entry<K, V>> function) { ImmutableMap.Builder<K, V> builder = ImmutableMap.builder(); for (T element : iterable) { @@ -39,4 +41,26 @@ public class MammothMaps { } return builder.build(); } + + public static <T, K> Map<K, List<T>> toMultiMapWithKey(Iterable<T> iterable, Function<T, K> function) { + return toMultiMap(iterable, element -> entry(function.apply(element), element)); + } + + public static <T, K, V> Map<K, List<V>> toMultiMap(Iterable<T> iterable, Function<T, Map.Entry<K, V>> function) { + Map<K, List<V>> map = new HashMap<>(); + for (T element : iterable) { + Map.Entry<K, V> pair = function.apply(element); + map.computeIfAbsent(pair.getKey(), key -> new ArrayList<V>()) + .add(pair.getValue()); + } + return map; + } + + public static <K, V1, V2> Map<K, V2> eagerMapValues(Map<K, V1> map, Function<V1, V2> function) { + Map<K, V2> result = new HashMap<>(); + for (Map.Entry<K, V1> element : map.entrySet()) { + result.put(element.getKey(), function.apply(element.getValue())); + } + return result; + } } |
