From: Javen O'Neal Date: Mon, 13 Jun 2016 00:43:34 +0000 (+0000) Subject: add copy constructor X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ca9431d71f6a7c633c4e5a06e821acea347bbabd;p=poi.git add copy constructor git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ss_border_property_template@1748072 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/ss/util/BorderPropertyTemplate.java b/src/java/org/apache/poi/ss/util/BorderPropertyTemplate.java index d101a6437c..0446e79cda 100644 --- a/src/java/org/apache/poi/ss/util/BorderPropertyTemplate.java +++ b/src/java/org/apache/poi/ss/util/BorderPropertyTemplate.java @@ -197,6 +197,30 @@ public final class BorderPropertyTemplate { _propertyTemplate = new HashMap>(); } + /** + * Copy constructor + * + * Create a template from an existing template. + * Changes made to either template do not affect the other template. + * @since 3.15 beta 2 + */ + public BorderPropertyTemplate(BorderPropertyTemplate prototype) { + this(); + // deep copy the _propertyTemplate map from prototype + for (Entry> other : prototype._propertyTemplate.entrySet()) { + CellAddress cell = other.getKey(); + Map otherMap = other.getValue(); + + // The keys in otherMap are immutable Strings + // The values in otherMap are immutable Shorts or BorderStyles + // Therefore, this map's data cannot be modified through protoype + Map map = new HashMap(otherMap); + + // CellAddress is an immutable class, therefore it is ok to reference the same instance + _propertyTemplate.put(cell, map); + } + } + /** * Add a group of cell borders for a cell range to the border property template. * The borders are not applied to the cells at this time, just the template is drawn diff --git a/src/testcases/org/apache/poi/ss/util/TestBorderPropertyTemplate.java b/src/testcases/org/apache/poi/ss/util/TestBorderPropertyTemplate.java index 588ac4dd16..d9642ed1d1 100644 --- a/src/testcases/org/apache/poi/ss/util/TestBorderPropertyTemplate.java +++ b/src/testcases/org/apache/poi/ss/util/TestBorderPropertyTemplate.java @@ -18,6 +18,7 @@ package org.apache.poi.ss.util; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import java.io.IOException; @@ -43,6 +44,37 @@ public final class TestBorderPropertyTemplate { private static final short BLUE = IndexedColors.BLUE.getIndex(); private static final short AUTOMATIC = IndexedColors.AUTOMATIC.getIndex(); + @Test + public void createTemplateFromExisting() throws IOException { + CellRangeAddress a1a1 = new CellRangeAddress(0, 0, 0, 0); //A1:A1 + CellRangeAddress b2b2 = new CellRangeAddress(1, 1, 1, 1); //B2:B2 + CellRangeAddress c3c3 = new CellRangeAddress(2, 2, 2, 2); //C3:C3 + CellAddress a1 = new CellAddress(0, 0); //A1 + CellAddress b2 = new CellAddress(1, 1); //B2 + CellAddress c3 = new CellAddress(2, 2); //C3 + + BorderPropertyTemplate pt1 = new BorderPropertyTemplate(); + pt1.drawBorders(a1a1, BorderStyle.THIN, RED, BorderExtent.TOP); + + // check to make sure borders were copied + BorderPropertyTemplate pt2 = new BorderPropertyTemplate(pt1); + assertEquals(1, pt2.getNumBorders(a1)); + assertThin(pt2.getTemplateProperty(a1, CellUtil.BORDER_TOP)); + assertRed(pt2.getTemplateProperty(a1, CellUtil.TOP_BORDER_COLOR)); + + // Changes to original template should not affect copied template. + pt1.drawBorders(b2b2, BorderStyle.THIN, RED, BorderExtent.TOP); + assertEquals(0, pt2.getNumBorders(b2)); + assertNull(pt2.getTemplateProperty(b2, CellUtil.BORDER_TOP)); + assertNull(pt2.getTemplateProperty(b2, CellUtil.TOP_BORDER_COLOR)); + + // Changes to copied template should not affect original template + pt2.drawBorders(c3c3, BorderStyle.THIN, RED, BorderExtent.TOP); + assertEquals(0, pt1.getNumBorders(c3)); + assertNull(pt1.getTemplateProperty(c3, CellUtil.BORDER_TOP)); + assertNull(pt1.getTemplateProperty(c3, CellUtil.TOP_BORDER_COLOR)); + } + @Test public void getNumBorders() throws IOException { CellRangeAddress a1a1 = new CellRangeAddress(0, 0, 0, 0); //A1:A1