diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-03-26 09:33:55 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-03-26 09:33:55 +0000 |
| commit | 300d110d93e9aa8ed91d060cedae47231106c31c (patch) | |
| tree | 878c9c638dc45a33f4a3124b130b319465f985ee /src | |
| parent | abbcc3915fa783c648436ef52e15da923a246dd1 (diff) | |
Replace usages of Iterables.transform with appropriate map methods
Diffstat (limited to 'src')
6 files changed, 26 insertions, 31 deletions
diff --git a/src/main/java/org/zwobble/mammoth/internal/DocumentToHtml.java b/src/main/java/org/zwobble/mammoth/internal/DocumentToHtml.java index 11869e0..2074402 100644 --- a/src/main/java/org/zwobble/mammoth/internal/DocumentToHtml.java +++ b/src/main/java/org/zwobble/mammoth/internal/DocumentToHtml.java @@ -31,10 +31,10 @@ public class DocumentToHtml { } private static List<Note> findNotes(Document document, Iterable<NoteReference> noteReferences) { - return ImmutableList.copyOf(Iterables.transform( + return eagerMap( noteReferences, // TODO: handle missing notes - reference -> document.getNotes().findNote(reference.getNoteType(), reference.getNoteId()).get())); + reference -> document.getNotes().findNote(reference.getNoteType(), reference.getNoteId()).get()); } public static InternalResult<List<HtmlNode>> convertToHtml(DocumentElement element, DocumentToHtmlOptions options) { @@ -64,7 +64,7 @@ public class DocumentToHtml { return mainBody; } else { HtmlNode noteNode = Html.element("ol", - ImmutableList.copyOf(Iterables.transform(notes, this::convertToHtml))); + eagerMap(notes, this::convertToHtml)); return ImmutableList.copyOf(Iterables.concat(mainBody, list(noteNode))); } diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java b/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java index 0edc7a7..6f705bb 100644 --- a/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java +++ b/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java @@ -15,9 +15,9 @@ import java.util.Set; import java.util.function.Function; import static com.google.common.collect.Iterables.filter; -import static com.google.common.collect.Iterables.transform; import static org.zwobble.mammoth.internal.docx.ReadResult.EMPTY_SUCCESS; import static org.zwobble.mammoth.internal.docx.ReadResult.success; +import static org.zwobble.mammoth.internal.util.MammothIterables.lazyMap; import static org.zwobble.mammoth.internal.util.MammothLists.list; import static org.zwobble.mammoth.internal.util.MammothStrings.trimLeft; @@ -173,7 +173,7 @@ public class BodyXmlReader { public ReadResult readElements(Iterable<XmlNode> nodes) { return ReadResult.concat( - transform( + lazyMap( filter(nodes, XmlElement.class), this::readElement)); } @@ -286,7 +286,7 @@ public class BodyXmlReader { } private ReadResult readBlips(XmlElementList blips, Optional<String> altText) { - return ReadResult.concat(transform(blips, blip -> readBlip(blip, altText))); + return ReadResult.concat(lazyMap(blips, blip -> readBlip(blip, altText))); } private ReadResult readBlip(XmlElement blip, Optional<String> altText) { diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/NotesXmlReader.java b/src/main/java/org/zwobble/mammoth/internal/docx/NotesXmlReader.java index d2ba221..40219b0 100644 --- a/src/main/java/org/zwobble/mammoth/internal/docx/NotesXmlReader.java +++ b/src/main/java/org/zwobble/mammoth/internal/docx/NotesXmlReader.java @@ -8,7 +8,7 @@ import org.zwobble.mammoth.internal.xml.XmlElement; import java.util.List; import static com.google.common.collect.Iterables.filter; -import static com.google.common.collect.Iterables.transform; +import static org.zwobble.mammoth.internal.util.MammothIterables.lazyMap; public class NotesXmlReader { public static NotesXmlReader footnote(BodyXmlReader bodyReader) { @@ -31,7 +31,7 @@ public class NotesXmlReader { public InternalResult<List<Note>> readElement(XmlElement element) { Iterable<XmlElement> elements = filter(element.findChildren("w:" + tagName), this::isNoteElement); - return InternalResult.concat(transform(elements, this::readNoteElement)); + return InternalResult.concat(lazyMap(elements, this::readNoteElement)); } private boolean isNoteElement(XmlElement element) { diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/OfficeXml.java b/src/main/java/org/zwobble/mammoth/internal/docx/OfficeXml.java index 2b5c279..edec271 100644 --- a/src/main/java/org/zwobble/mammoth/internal/docx/OfficeXml.java +++ b/src/main/java/org/zwobble/mammoth/internal/docx/OfficeXml.java @@ -1,7 +1,6 @@ package org.zwobble.mammoth.internal.docx; import com.google.common.collect.ImmutableBiMap; -import com.google.common.collect.ImmutableList; import org.zwobble.mammoth.internal.xml.XmlElement; import org.zwobble.mammoth.internal.xml.XmlNode; import org.zwobble.mammoth.internal.xml.XmlNodeVisitor; @@ -11,8 +10,7 @@ import org.zwobble.mammoth.internal.xml.parsing.XmlParser; import java.io.InputStream; import java.util.List; -import static com.google.common.collect.Iterables.concat; -import static com.google.common.collect.Iterables.transform; +import static org.zwobble.mammoth.internal.util.MammothLists.eagerFlatMap; import static org.zwobble.mammoth.internal.util.MammothLists.list; public class OfficeXml { @@ -44,7 +42,7 @@ public class OfficeXml { XmlElement collapsedElement = new XmlElement( element.getName(), element.getAttributes(), - ImmutableList.copyOf(concat(transform(element.children(), OfficeXml::collapseAlternateContent)))); + eagerFlatMap(element.children(), OfficeXml::collapseAlternateContent)); return list(collapsedElement); } } diff --git a/src/main/java/org/zwobble/mammoth/internal/xml/XmlElementList.java b/src/main/java/org/zwobble/mammoth/internal/xml/XmlElementList.java index 3facde4..ef41c4e 100644 --- a/src/main/java/org/zwobble/mammoth/internal/xml/XmlElementList.java +++ b/src/main/java/org/zwobble/mammoth/internal/xml/XmlElementList.java @@ -1,11 +1,10 @@ package org.zwobble.mammoth.internal.xml; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; - import java.util.Iterator; import java.util.List; +import static org.zwobble.mammoth.internal.util.MammothLists.eagerFlatMap; + public class XmlElementList implements Iterable<XmlElement> { private final List<XmlElement> elements; @@ -19,10 +18,8 @@ public class XmlElementList implements Iterable<XmlElement> { } public XmlElementList findChildren(String name) { - Iterable<XmlElement> children = Iterables.concat( - Iterables.transform( - elements, - element -> element.findChildren(name))); - return new XmlElementList(ImmutableList.copyOf(children)); + return new XmlElementList(eagerFlatMap( + elements, + element -> element.findChildren(name))); } } diff --git a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java index 3b36482..dff85a8 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java +++ b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java @@ -14,8 +14,8 @@ import java.util.Set; import static com.google.common.collect.Iterables.any; import static com.google.common.collect.Iterables.filter; -import static com.google.common.collect.Iterables.transform; import static java.util.Arrays.asList; +import static org.zwobble.mammoth.internal.util.MammothIterables.lazyMap; public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { public static <T> Matcher<T> deepEquals(T value) { @@ -77,14 +77,14 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { if (actual.size() > expected.size()) { appendPath(mismatchDescription, path); mismatchDescription.appendText("extra elements:" + - indentedList(transform(Iterables.skip(actual, expected.size()), DeepReflectionMatcher::describeValue))); + indentedList(lazyMap(Iterables.skip(actual, expected.size()), DeepReflectionMatcher::describeValue))); return false; } if (actual.size() < expected.size()) { appendPath(mismatchDescription, path); mismatchDescription.appendText("missing elements:" + - indentedList(transform(Iterables.skip(expected, actual.size()), DeepReflectionMatcher::describeValue))); + indentedList(lazyMap(Iterables.skip(expected, actual.size()), DeepReflectionMatcher::describeValue))); return false; } @@ -105,7 +105,7 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { if (!missing.isEmpty()) { appendPath(mismatchDescription, path); mismatchDescription.appendText("missing elements:" + - indentedList(transform(missing, DeepReflectionMatcher::describeValue))); + indentedList(lazyMap(missing, DeepReflectionMatcher::describeValue))); return false; } @@ -116,7 +116,7 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { if (!extra.isEmpty()) { appendPath(mismatchDescription, path); mismatchDescription.appendText("extra elements:" + - indentedList(transform(extra, DeepReflectionMatcher::describeValue))); + indentedList(lazyMap(extra, DeepReflectionMatcher::describeValue))); return false; } @@ -145,7 +145,7 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { if (!extraElements.isEmpty()) { appendPath(mismatchDescription, path); mismatchDescription.appendText(prefix + - indentedList(transform( + indentedList(lazyMap( extraElements, key -> describeValue(key) + "=" + describeValue(actual.get(key))))); return false; @@ -205,21 +205,21 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { } } else if (value instanceof List) { List<?> list = (List)value; - return "[" + indentedList(transform(list, DeepReflectionMatcher::describeValue)) + "]"; + return "[" + indentedList(lazyMap(list, DeepReflectionMatcher::describeValue)) + "]"; } else if (value instanceof Set) { Set<?> list = (Set)value; - return "{" + indentedList(transform(list, DeepReflectionMatcher::describeValue)) + "}"; + return "{" + indentedList(lazyMap(list, DeepReflectionMatcher::describeValue)) + "}"; } else if (value instanceof Map) { Map<?, ?> map = (Map)value; String entries = indentedList( - transform( + lazyMap( map.entrySet(), entry -> describeValue(entry.getKey()) + "=" + describeValue(entry.getValue()))); return "{" + entries + "}"; } else { Class<?> clazz = value.getClass(); List<Field> fields = fields(clazz); - Iterable<String> fieldStrings = transform( + Iterable<String> fieldStrings = lazyMap( fields, field -> field.getName() + "=" + describeValue(readField(value, field))); return String.format("%s(%s)", clazz.getSimpleName(), indentedList(fieldStrings)); @@ -227,7 +227,7 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { } private static String indentedList(Iterable<String> values) { - return String.join(",", transform(values, value -> "\n " + indent(value))); + return String.join(",", lazyMap(values, value -> "\n " + indent(value))); } private static String indent(String value) { |
