--- src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java (revision 1760296) +++ src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java (working copy) @@ -276,6 +276,10 @@ return numberFormats.get(fmtId); } + /** + * Case sensitive search. + * Runs in O(n) time. Consider using a bidirectional map for faster reverse lookups. + */ private short getNumberFormatId(String fmt) { // Find the key, and return that for (Entry numFmt : numberFormats.entrySet()) { @@ -383,7 +387,7 @@ } public XSSFFont getFontAt(int idx) { - return fonts.get(idx); + return fonts.get(idx); // 0-based index } /** @@ -403,15 +407,15 @@ } if (idx != -1) { - return idx; + return idx; // 0-based index } idx = fonts.size(); fonts.add(font); - return idx; + return idx; // 0-based index } public int putFont(XSSFFont font) { - return putFont(font, false); + return putFont(font, false); // 0-based index } public XSSFCellStyle getStyleAt(int idx) { @@ -418,7 +422,7 @@ int styleXfId = 0; // 0 is the empty default - if(xfs.get(idx).getXfId() > 0) { + if(xfs.get(idx).getXfId() > 0) { // 0-based index styleXfId = (int) xfs.get(idx).getXfId(); } @@ -426,15 +430,19 @@ } public int putStyle(XSSFCellStyle style) { CTXf mainXF = style.getCoreXf(); + int idx = xfs.indexOf(mainXF); - if(! xfs.contains(mainXF)) { - xfs.add(mainXF); + if (idx != -1) { + return idx; // 0-based index } - return xfs.indexOf(mainXF); + + idx = xfs.size(); + xfs.add(mainXF); + return idx; // 0-based index } public XSSFCellBorder getBorderAt(int idx) { - return borders.get(idx); + return borders.get(idx); // 0-based index } /** @@ -447,15 +455,15 @@ public int putBorder(XSSFCellBorder border) { int idx = borders.indexOf(border); if (idx != -1) { - return idx; + return idx; // 0-based index } borders.add(border); border.setThemesTable(theme); - return borders.size() - 1; + return borders.size() - 1; // 0-based index } public XSSFCellFill getFillAt(int idx) { - return fills.get(idx); + return fills.get(idx); // 0-based index } public List getBorders(){ @@ -484,15 +492,15 @@ public int putFill(XSSFCellFill fill) { int idx = fills.indexOf(fill); if (idx != -1) { - return idx; + return idx; // 0-based index } fills.add(fill); - return fills.size() - 1; + return fills.size() - 1; // 0-based index } @Internal public CTXf getCellXfAt(int idx) { - return xfs.get(idx); + return xfs.get(idx); // 0-based index } /** @@ -505,18 +513,18 @@ @Internal public int putCellXf(CTXf cellXf) { xfs.add(cellXf); - return xfs.size(); + return xfs.size(); // 1-based index } @Internal public void replaceCellXfAt(int idx, CTXf cellXf) { - xfs.set(idx, cellXf); + xfs.set(idx, cellXf); // 0-based index } @Internal public CTXf getCellStyleXfAt(int idx) { try { - return styleXfs.get(idx); + return styleXfs.get(idx); // 0-based index } catch (final IndexOutOfBoundsException e) { return null; @@ -534,12 +542,12 @@ public int putCellStyleXf(CTXf cellStyleXf) { styleXfs.add(cellStyleXf); // TODO: check for duplicate - return styleXfs.size(); + return styleXfs.size(); // 1-based index } @Internal protected void replaceCellStyleXfAt(int idx, CTXf cellStyleXf) { - styleXfs.set(idx, cellStyleXf); + styleXfs.set(idx, cellStyleXf); // 0-based index } /** @@ -762,7 +770,7 @@ @Internal public CTDxf getDxfAt(int idx) { - return dxfs.get(idx); + return dxfs.get(idx); //0-based index } /** @@ -775,7 +783,7 @@ @Internal public int putDxf(CTDxf dxf) { this.dxfs.add(dxf); - return this.dxfs.size(); + return this.dxfs.size(); //1-based index } /** @@ -798,7 +806,7 @@ xf.setBorderId(0); xf.setXfId(0); int indexXf = putCellXf(xf); - return new XSSFCellStyle(indexXf - 1, xfSize - 1, this, theme); + return new XSSFCellStyle(indexXf - 1, xfSize - 1, this, theme); //0-based index } /**