diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-01-03 14:36:59 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-01-03 14:36:59 +0000 |
| commit | 4f8ad8f0588564679cb8ccb9ae4592738810d800 (patch) | |
| tree | fa8d0d1c2115079e362e0218b5d530f1502138c4 | |
| parent | 07685b4a361f8827728513f9d7360bd91dc1a246 (diff) | |
Add map as shorter alias for ImmutableMap.of
12 files changed, 65 insertions, 52 deletions
diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java index e2408e9..e834b3b 100644 --- a/src/main/java/org/zwobble/mammoth/Mammoth.java +++ b/src/main/java/org/zwobble/mammoth/Mammoth.java @@ -1,19 +1,15 @@ package org.zwobble.mammoth; import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableBiMap; -import com.google.common.collect.ImmutableMap; import org.zwobble.mammoth.documents.*; import org.zwobble.mammoth.docx.BodyXmlReader; import org.zwobble.mammoth.docx.DocumentXmlReader; import org.zwobble.mammoth.docx.OfficeXml; import org.zwobble.mammoth.docx.Styles; import org.zwobble.mammoth.xml.XmlElement; -import org.zwobble.mammoth.xml.parsing.XmlParser; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -25,7 +21,7 @@ public class Mammoth { ZipEntry entry = zipFile.getEntry("word/document.xml"); XmlElement documentXml = OfficeXml.parseXml(zipFile.getInputStream(entry)); - Styles styles = new Styles(ImmutableMap.of(), ImmutableMap.of()); + Styles styles = Styles.EMPTY; Document document = new DocumentXmlReader(new BodyXmlReader(styles)).readElement(documentXml); return convertToHtml(document); } catch (IOException e) { diff --git a/src/main/java/org/zwobble/mammoth/docx/Styles.java b/src/main/java/org/zwobble/mammoth/docx/Styles.java index 3ee7372..8784ee2 100644 --- a/src/main/java/org/zwobble/mammoth/docx/Styles.java +++ b/src/main/java/org/zwobble/mammoth/docx/Styles.java @@ -1,13 +1,14 @@ package org.zwobble.mammoth.docx; -import com.google.common.collect.ImmutableMap; import org.zwobble.mammoth.documents.Style; import java.util.Map; import java.util.Optional; +import static org.zwobble.mammoth.util.MammothMaps.map; + public class Styles { - public static final Styles EMPTY = new Styles(ImmutableMap.of(), ImmutableMap.of()); + public static final Styles EMPTY = new Styles(map(), map()); private final Map<String, Style> paragraphStyles; private final Map<String, Style> characterStyles; diff --git a/src/main/java/org/zwobble/mammoth/util/MammothMaps.java b/src/main/java/org/zwobble/mammoth/util/MammothMaps.java index 6e988dd..782bd91 100644 --- a/src/main/java/org/zwobble/mammoth/util/MammothMaps.java +++ b/src/main/java/org/zwobble/mammoth/util/MammothMaps.java @@ -6,6 +6,22 @@ import java.util.Map; import java.util.function.Function; public class MammothMaps { + public static <K, V> Map<K, V> map() { + return ImmutableMap.of(); + } + + public static <K, V> Map<K, V> map(K key1, V value1) { + return ImmutableMap.of(key1, value1); + } + + public static <K, V> Map<K, V> map(K key1, V value1, K key2, V value2) { + return ImmutableMap.of(key1, value1, key2, value2); + } + + public static <K, V> Map<K, V> map(K key1, V value1, K key2, V value2, K key3, V value3) { + return ImmutableMap.of(key1, value1, key2, value2, key3, value3); + } + 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) { diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java index f592415..c44b7bb 100644 --- a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java +++ b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java @@ -2,17 +2,17 @@ package org.zwobble.mammoth.xml; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import java.util.List; import java.util.Map; import java.util.Optional; import static com.google.common.collect.Iterables.*; +import static org.zwobble.mammoth.util.MammothMaps.map; public class XmlElement implements XmlNode, XmlElementLike { public XmlElement(String name) { - this(name, ImmutableMap.of()); + this(name, map()); } public XmlElement(String name, Map<String, String> attributes) { diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlNodes.java b/src/main/java/org/zwobble/mammoth/xml/XmlNodes.java index d168fa6..9531f17 100644 --- a/src/main/java/org/zwobble/mammoth/xml/XmlNodes.java +++ b/src/main/java/org/zwobble/mammoth/xml/XmlNodes.java @@ -6,6 +6,8 @@ import com.google.common.collect.ImmutableMap; import java.util.List; import java.util.Map; +import static org.zwobble.mammoth.util.MammothMaps.map; + public class XmlNodes { public static XmlElement element(String name) { return element(name, ImmutableList.of()); @@ -16,7 +18,7 @@ public class XmlNodes { } public static XmlElement element(String name, List<XmlNode> children) { - return element(name, ImmutableMap.of(), children); + return element(name, map(), children); } public static XmlElement element(String name, Map<String, String> attributes, List<XmlNode> children) { diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java index 1ead78c..42fd5c7 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -1,7 +1,6 @@ package org.zwobble.mammoth.tests.docx; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.natpryce.makeiteasy.Maker; import org.hamcrest.Matcher; import org.junit.Test; @@ -16,9 +15,7 @@ import org.zwobble.mammoth.xml.XmlNodes; import java.util.List; import java.util.Optional; -import static com.natpryce.makeiteasy.MakeItEasy.a; -import static com.natpryce.makeiteasy.MakeItEasy.make; -import static com.natpryce.makeiteasy.MakeItEasy.with; +import static com.natpryce.makeiteasy.MakeItEasy.*; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasProperty; @@ -27,6 +24,7 @@ import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.CHILDREN import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.PARAGRAPH; import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.STYLES; import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.bodyReader; +import static org.zwobble.mammoth.util.MammothMaps.map; import static org.zwobble.mammoth.xml.XmlNodes.element; public class BodyXmlTests { @@ -65,7 +63,7 @@ public class BodyXmlTests { public void paragraphHasStyleIdReadFromParagraphPropertiesIfPresent() { XmlElement element = paragraphXml(ImmutableList.of( element("w:pPr", ImmutableList.of( - element("w:pStyle", ImmutableMap.of("w:val", "Heading1")))))); + element("w:pStyle", map("w:val", "Heading1")))))); assertThat( read(a(bodyReader), element), hasStyle(Optional.of(new Style("Heading1", Optional.empty())))); @@ -75,12 +73,12 @@ public class BodyXmlTests { public void whenParagraphHasStyleIdInStylesThenStyleNameIsReadFromStyles() { XmlElement element = paragraphXml(ImmutableList.of( element("w:pPr", ImmutableList.of( - element("w:pStyle", ImmutableMap.of("w:val", "Heading1")))))); + element("w:pStyle", map("w:val", "Heading1")))))); Style style = new Style("Heading1", Optional.of("Heading 1")); Styles styles = new Styles( - ImmutableMap.of("Heading1", style), - ImmutableMap.of()); + map("Heading1", style), + map()); assertThat( read(a(bodyReader, with(STYLES, styles)), element), hasStyle(Optional.of(style))); diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/ContentTypesXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/ContentTypesXmlTests.java index 2fc0692..287a709 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/ContentTypesXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/ContentTypesXmlTests.java @@ -1,7 +1,6 @@ package org.zwobble.mammoth.tests.docx; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import org.junit.Test; import org.zwobble.mammoth.docx.ContentTypes; import org.zwobble.mammoth.xml.XmlElement; @@ -10,13 +9,14 @@ import java.util.Optional; import static org.junit.Assert.assertEquals; import static org.zwobble.mammoth.docx.ContentTypesXml.readContentTypesXmlElement; +import static org.zwobble.mammoth.util.MammothMaps.map; import static org.zwobble.mammoth.xml.XmlNodes.element; public class ContentTypesXmlTests { @Test public void contentTypeIsBasedOnDefaultForExtensionIfThereIsNoOverride() { XmlElement element = element("content-types:Types", ImmutableList.of( - element("content-types:Default", ImmutableMap.of( + element("content-types:Default", map( "Extension", "png", "ContentType", "image/png")))); ContentTypes contentTypes = readContentTypesXmlElement(element); @@ -28,10 +28,10 @@ public class ContentTypesXmlTests { @Test public void contentTypeIsBasedOnOverrideIfPresent() { XmlElement element = element("content-types:Types", ImmutableList.of( - element("content-types:Default", ImmutableMap.of( + element("content-types:Default", map( "Extension", "png", "ContentType", "image/png")), - element("content-types:Override", ImmutableMap.of( + element("content-types:Override", map( "PartName", "/word/media/hat.png", "ContentType", "image/hat")))); ContentTypes contentTypes = readContentTypesXmlElement(element); diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java index 16d14ad..58edf54 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java @@ -1,7 +1,6 @@ package org.zwobble.mammoth.tests.docx; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import org.junit.Test; import org.zwobble.mammoth.documents.Note; import org.zwobble.mammoth.docx.BodyXmlReader; @@ -15,6 +14,7 @@ import static com.natpryce.makeiteasy.MakeItEasy.make; import static org.hamcrest.MatcherAssert.assertThat; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.PARAGRAPH; +import static org.zwobble.mammoth.util.MammothMaps.map; import static org.zwobble.mammoth.xml.XmlNodes.element; public class NotesXmlReaderTests { @@ -23,7 +23,7 @@ public class NotesXmlReaderTests { @Test public void idAndBodyOfFootnoteAreRead() { XmlElement element = element("w:footnotes", ImmutableList.of( - element("w:footnote", ImmutableMap.of("w:id", "1"), ImmutableList.of( + element("w:footnote", map("w:id", "1"), ImmutableList.of( element("w:p"))))); NotesXmlReader reader = new NotesXmlReader(bodyReader, "footnote"); @@ -46,7 +46,7 @@ public class NotesXmlReaderTests { private void assertFootnoteTypeIsIgnored(String noteType) { XmlElement element = element("w:footnotes", ImmutableList.of( - element("w:footnote", ImmutableMap.of("w:id", "1", "w:type", noteType), ImmutableList.of( + element("w:footnote", map("w:id", "1", "w:type", noteType), ImmutableList.of( element("w:p"))))); NotesXmlReader reader = new NotesXmlReader(bodyReader, "footnote"); diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/NumberingXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/NumberingXmlTests.java index a10575f..dc4d5f6 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/NumberingXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/NumberingXmlTests.java @@ -1,7 +1,6 @@ package org.zwobble.mammoth.tests.docx; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import org.junit.Test; import org.zwobble.mammoth.docx.Numbering; import org.zwobble.mammoth.xml.XmlElement; @@ -10,17 +9,18 @@ import java.util.Optional; import static org.junit.Assert.assertEquals; import static org.zwobble.mammoth.docx.NumberingXml.readNumberingXmlElement; +import static org.zwobble.mammoth.util.MammothMaps.map; import static org.zwobble.mammoth.xml.XmlNodes.element; public class NumberingXmlTests { private final static XmlElement SAMPLE_NUMBERING_XML = element("w:numbering", ImmutableList.of( - element("w:abstractNum", ImmutableMap.of("w:abstractNumId", "42"), ImmutableList.of( - element("w:lvl", ImmutableMap.of("w:ilvl", "0"), ImmutableList.of( - element("w:numFmt", ImmutableMap.of("w:val", "bullet")))), - element("w:lvl", ImmutableMap.of("w:ilvl", "1"), ImmutableList.of( - element("w:numFmt", ImmutableMap.of("w:val", "decimal")))))), - element("w:num", ImmutableMap.of("w:numId", "47"), ImmutableList.of( - element("w:abstractNumId", ImmutableMap.of("w:val", "42")))))); + element("w:abstractNum", map("w:abstractNumId", "42"), ImmutableList.of( + element("w:lvl", map("w:ilvl", "0"), ImmutableList.of( + element("w:numFmt", map("w:val", "bullet")))), + element("w:lvl", map("w:ilvl", "1"), ImmutableList.of( + element("w:numFmt", map("w:val", "decimal")))))), + element("w:num", map("w:numId", "47"), ImmutableList.of( + element("w:abstractNumId", map("w:val", "42")))))); @Test public void findLevelReturnsNoneIfNumWithIdCannotBeFound() { diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/RelationshipsXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/RelationshipsXmlTests.java index 1ba9ea8..19e74f3 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/RelationshipsXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/RelationshipsXmlTests.java @@ -1,20 +1,20 @@ package org.zwobble.mammoth.tests.docx; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import org.junit.Test; import org.zwobble.mammoth.docx.Relationships; import org.zwobble.mammoth.xml.XmlElement; import static org.junit.Assert.assertEquals; import static org.zwobble.mammoth.docx.RelationshipsXml.readRelationshipsXmlElement; +import static org.zwobble.mammoth.util.MammothMaps.map; import static org.zwobble.mammoth.xml.XmlNodes.element; public class RelationshipsXmlTests { @Test public void targetIsReadFromRelationshipElement() { XmlElement element = element("relationship:Relationships", ImmutableList.of( - element("relationships:Relationship", ImmutableMap.of( + element("relationships:Relationship", map( "Id", "rId8", "Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", "Target", "http://example.com")))); diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/StylesXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/StylesXmlTests.java index eb28af9..398c7ca 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/StylesXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/StylesXmlTests.java @@ -1,7 +1,6 @@ package org.zwobble.mammoth.tests.docx; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import org.junit.Test; import org.zwobble.mammoth.docx.Styles; import org.zwobble.mammoth.xml.XmlElement; @@ -10,6 +9,7 @@ import java.util.Optional; import static org.junit.Assert.assertEquals; import static org.zwobble.mammoth.docx.StylesXml.readStylesXmlElement; +import static org.zwobble.mammoth.util.MammothMaps.map; import static org.zwobble.mammoth.xml.XmlNodes.element; public class StylesXmlTests { @@ -25,7 +25,7 @@ public class StylesXmlTests { @Test public void paragraphStyleCanBeFoundById() { XmlElement element = element("w:styles", ImmutableList.of( - element("w:style", ImmutableMap.of("w:type", "paragraph", "w:styleId", "Heading1"), ImmutableList.of( + element("w:style", map("w:type", "paragraph", "w:styleId", "Heading1"), ImmutableList.of( nameElement("Heading 1"))))); Styles styles = readStylesXmlElement(element); @@ -36,7 +36,7 @@ public class StylesXmlTests { @Test public void characterStyleCanBeFoundById() { XmlElement element = element("w:styles", ImmutableList.of( - element("w:style", ImmutableMap.of("w:type", "character", "w:styleId", "Heading1Char"), ImmutableList.of( + element("w:style", map("w:type", "character", "w:styleId", "Heading1Char"), ImmutableList.of( nameElement("Heading 1 Char"))))); Styles styles = readStylesXmlElement(element); @@ -47,9 +47,9 @@ public class StylesXmlTests { @Test public void paragraphAndCharacterStylesAreDistinct() { XmlElement element = element("w:styles", ImmutableList.of( - element("w:style", ImmutableMap.of("w:type", "paragraph", "w:styleId", "Heading1"), ImmutableList.of( + element("w:style", map("w:type", "paragraph", "w:styleId", "Heading1"), ImmutableList.of( nameElement("Heading 1"))), - element("w:style", ImmutableMap.of("w:type", "character", "w:styleId", "Heading1Char"), ImmutableList.of( + element("w:style", map("w:type", "character", "w:styleId", "Heading1Char"), ImmutableList.of( nameElement("Heading 1 Char"))))); Styles styles = readStylesXmlElement(element); @@ -61,8 +61,8 @@ public class StylesXmlTests { @Test public void styleNameIsNoneIfNameElementDoesNotExist() { XmlElement element = element("w:styles", ImmutableList.of( - element("w:style", ImmutableMap.of("w:type", "paragraph", "w:styleId", "Heading1")), - element("w:style", ImmutableMap.of("w:type", "character", "w:styleId", "Heading1Char")))); + element("w:style", map("w:type", "paragraph", "w:styleId", "Heading1")), + element("w:style", map("w:type", "character", "w:styleId", "Heading1Char")))); Styles styles = readStylesXmlElement(element); @@ -71,6 +71,6 @@ public class StylesXmlTests { } private XmlElement nameElement(String name) { - return element("w:name", ImmutableMap.of("w:val", name)); + return element("w:name", map("w:val", name)); } } diff --git a/src/test/java/org/zwobble/mammoth/tests/xml/XmlParserTests.java b/src/test/java/org/zwobble/mammoth/tests/xml/XmlParserTests.java index ae02b79..c19ec4c 100644 --- a/src/test/java/org/zwobble/mammoth/tests/xml/XmlParserTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/xml/XmlParserTests.java @@ -2,15 +2,14 @@ package org.zwobble.mammoth.tests.xml; import com.google.common.collect.ImmutableBiMap; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import org.hamcrest.Matcher; import org.hamcrest.collection.IsEmptyIterable; import org.hamcrest.collection.IsIterableContainingInOrder; import org.junit.Test; import org.zwobble.mammoth.xml.XmlElement; import org.zwobble.mammoth.xml.XmlNode; -import org.zwobble.mammoth.xml.parsing.XmlParser; import org.zwobble.mammoth.xml.XmlTextNode; +import org.zwobble.mammoth.xml.parsing.XmlParser; import java.io.ByteArrayInputStream; import java.util.List; @@ -18,6 +17,7 @@ import java.util.Map; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; +import static org.zwobble.mammoth.util.MammothMaps.map; public class XmlParserTests { private final XmlParser parser = new XmlParser(ImmutableBiMap.of()); @@ -43,15 +43,15 @@ public class XmlParserTests { public void canParseAttributesOfTag() { assertThat( parser.parseString("<body name='bob'></body>"), - isElement("body", ImmutableMap.of("name", "bob"))); + isElement("body", map("name", "bob"))); } @Test public void canParseElementWithDescendantElements() { assertThat( parser.parseString("<body><a><b/></a><a/></body>"), - isElement("body", ImmutableMap.of(), ImmutableList.of( - isElement("a", ImmutableMap.of(), ImmutableList.of( + isElement("body", map(), ImmutableList.of( + isElement("a", map(), ImmutableList.of( isElement("b"))), isElement("a")))); } @@ -60,7 +60,7 @@ public class XmlParserTests { public void canParseTextNodes() { assertThat( parser.parseString("<body>Hello!</body>"), - isElement("body", ImmutableMap.of(), ImmutableList.of( + isElement("body", map(), ImmutableList.of( isTextNode("Hello!")))); } @@ -75,7 +75,7 @@ public class XmlParserTests { public void unmappedNamespaceUrisInAttributeNamesAreIncludedInBracesAsPrefix() { assertThat( parser.parseString("<body xmlns:w='word' w:name='bob'></body>"), - isElement("body", ImmutableMap.of("{word}name", "bob"))); + isElement("body", map("{word}name", "bob"))); } @Test @@ -91,7 +91,7 @@ public class XmlParserTests { XmlParser parser = new XmlParser(ImmutableBiMap.of("x", "word")); assertThat( parser.parseString("<body xmlns:w='word' w:name='bob'/>"), - isElement("body", ImmutableMap.of("x:name", "bob"))); + isElement("body", map("x:name", "bob"))); } @Test @@ -102,7 +102,7 @@ public class XmlParserTests { } private Matcher<XmlElement> isElement(String name) { - return isElement(name, ImmutableMap.of()); + return isElement(name, map()); } private Matcher<XmlElement> isElement(String name, Map<String, String> attributes) { |
