add copy constructor

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ss_border_property_template@1748072 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-06-13 00:43:34 +00:00
parent d68b4ea41d
commit ca9431d71f
2 changed files with 56 additions and 0 deletions

View File

@ -197,6 +197,30 @@ public final class BorderPropertyTemplate {
_propertyTemplate = new HashMap<CellAddress, Map<String, Object>>();
}
/**
* 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<CellAddress, Map<String, Object>> other : prototype._propertyTemplate.entrySet()) {
CellAddress cell = other.getKey();
Map<String, Object> 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<String, Object> map = new HashMap<String, Object>(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

View File

@ -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