Removed variable that stores the number of merged regions in

its record since the array should be the basis for this number.  Less
manual index tracking, the better.

Thanks for problem report!
PR: 27005
Reported by: skalchihin@mgsm.ru


git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/branches/REL_2_BRANCH@353510 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Danny Muid 2004-02-18 16:48:17 +00:00
parent ddfb52a7f5
commit c96db64e3c
2 changed files with 27 additions and 21 deletions

View File

@ -61,19 +61,18 @@ import java.util.Iterator;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
/** /**
* Title: Merged Cells Record<P> * Title: Merged Cells Record
* <br>
* Description: Optional record defining a square area of cells to "merged" into * Description: Optional record defining a square area of cells to "merged" into
* one cell. <P> * one cell. <br>
* REFERENCE: NONE (UNDOCUMENTED PRESENTLY) <P> * REFERENCE: NONE (UNDOCUMENTED PRESENTLY) <br>
* @author Andrew C. Oliver (acoliver at apache dot org) * @author Andrew C. Oliver (acoliver at apache dot org)
* @version 2.0-pre * @version 2.0-pre
*/ */
public class MergeCellsRecord public class MergeCellsRecord
extends Record extends Record
{ {
public final static short sid = 0xe5; public final static short sid = 0xe5;
private short field_1_num_areas;
private ArrayList field_2_regions; private ArrayList field_2_regions;
public MergeCellsRecord() public MergeCellsRecord()
@ -109,11 +108,11 @@ public class MergeCellsRecord
protected void fillFields(byte [] data, short size, int offset) protected void fillFields(byte [] data, short size, int offset)
{ {
field_1_num_areas = LittleEndian.getShort(data, 0 + offset); short numAreas = LittleEndian.getShort(data, 0 + offset);
field_2_regions = new ArrayList(field_1_num_areas + 10); field_2_regions = new ArrayList(numAreas + 10);
int pos = 2; int pos = 2;
for (int k = 0; k < field_1_num_areas; k++) for (int k = 0; k < numAreas; k++)
{ {
MergedRegion region = MergedRegion region =
new MergedRegion(LittleEndian new MergedRegion(LittleEndian
@ -135,7 +134,9 @@ public class MergeCellsRecord
public short getNumAreas() public short getNumAreas()
{ {
return field_1_num_areas; //if the array size is larger than a short (65536), the record can't hold that many merges anyway
if (field_2_regions == null) return 0;
return (short)field_2_regions.size();
} }
/** /**
@ -143,13 +144,14 @@ public class MergeCellsRecord
* it will be incremented automatically or decremented when an area is removed. If * it will be incremented automatically or decremented when an area is removed. If
* you are setting this to 0 then you are a terrible person. Just remove the record. * you are setting this to 0 then you are a terrible person. Just remove the record.
* (just kidding about you being a terrible person..hehe) * (just kidding about you being a terrible person..hehe)
* * @deprecated We now link the size to the actual array of merged regions
* @see #getNumAreas()
* @param numareas number of areas * @param numareas number of areas
*/ */
public void setNumAreas(short numareas) public void setNumAreas(short numareas)
{ {
field_1_num_areas = numareas;
} }
/** /**
@ -175,7 +177,6 @@ public class MergeCellsRecord
colto); colto);
field_2_regions.add(region); field_2_regions.add(region);
field_1_num_areas++;
return field_2_regions.size() - 1; return field_2_regions.size() - 1;
} }
@ -187,7 +188,6 @@ public class MergeCellsRecord
public void removeAreaAt(int area) public void removeAreaAt(int area)
{ {
field_2_regions.remove(area); field_2_regions.remove(area);
field_1_num_areas--;
} }
/** /**
@ -246,9 +246,9 @@ public class MergeCellsRecord
retval.append("[MERGEDCELLS]").append("\n"); retval.append("[MERGEDCELLS]").append("\n");
retval.append(" .sid =").append(sid).append("\n"); retval.append(" .sid =").append(sid).append("\n");
retval.append(" .numregions =").append(field_1_num_areas) retval.append(" .numregions =").append(getNumAreas())
.append("\n"); .append("\n");
for (int k = 0; k < field_1_num_areas; k++) for (int k = 0; k < getNumAreas(); k++)
{ {
MergedRegion region = ( MergedRegion ) field_2_regions.get(k); MergedRegion region = ( MergedRegion ) field_2_regions.get(k);
@ -325,8 +325,7 @@ public class MergeCellsRecord
} }
public Object clone() { public Object clone() {
MergeCellsRecord rec = new MergeCellsRecord(); MergeCellsRecord rec = new MergeCellsRecord();
rec.field_1_num_areas = field_1_num_areas;
rec.field_2_regions = new ArrayList(); rec.field_2_regions = new ArrayList();
Iterator iterator = field_2_regions.iterator(); Iterator iterator = field_2_regions.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {

View File

@ -2,7 +2,7 @@
/* ==================================================================== /* ====================================================================
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2003 The Apache Software Foundation. All rights * Copyright (c) 2004 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -55,10 +55,14 @@
package org.apache.poi.hssf.usermodel; package org.apache.poi.hssf.usermodel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region; import org.apache.poi.hssf.util.Region;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/** /**
* Test the ability to clone a sheet. * Test the ability to clone a sheet.
@ -77,7 +81,10 @@ public class TestCloneSheet extends TestCase {
HSSFWorkbook b = new HSSFWorkbook(); HSSFWorkbook b = new HSSFWorkbook();
HSSFSheet s = b.createSheet("Test"); HSSFSheet s = b.createSheet("Test");
s.addMergedRegion(new Region((short)0,(short)0,(short)1,(short)1)); s.addMergedRegion(new Region((short)0,(short)0,(short)1,(short)1));
b.cloneSheet(0); HSSFSheet clonedSheet = b.cloneSheet(0);
assertEquals("One merged area", 1, clonedSheet.getNumMergedRegions());
} }
catch(Exception e){e.printStackTrace();fail(e.getMessage());} catch(Exception e){e.printStackTrace();fail(e.getMessage());}
} }