rewrite switch statements to not re-check the switch'd variable

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ss_border_property_template@1748070 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-06-12 23:43:16 +00:00
parent a63d82bbee
commit 0ae778e5cd

View File

@ -198,9 +198,10 @@ public final class BorderPropertyTemplate {
} }
/** /**
* Draws a group of cell borders for a cell range. The borders are not * Add a group of cell borders for a cell range to the border property template.
* applied to the cells at this time, just the template is drawn. To apply * The borders are not applied to the cells at this time, just the template is drawn
* the drawn borders to a sheet, use {@link #applyBorders}. * (<code>drawBorders</code> stages changes using the border property template).
* To apply the drawn borders to a sheet, use {@link #applyBorders}.
* *
* @param range range of cells on which borders are drawn. * @param range range of cells on which borders are drawn.
* @param borderType Type of border to draw. * @param borderType Type of border to draw.
@ -209,57 +210,58 @@ public final class BorderPropertyTemplate {
*/ */
public void drawBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) { public void drawBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
switch (extent) { switch (extent) {
case NONE: case NONE:
removeBorders(range); removeBorders(range);
break; break;
case ALL: case ALL:
drawHorizontalBorders(range, borderType, BorderExtent.ALL); drawHorizontalBorders(range, borderType, BorderExtent.ALL);
drawVerticalBorders(range, borderType, BorderExtent.ALL); drawVerticalBorders(range, borderType, BorderExtent.ALL);
break; break;
case INSIDE: case INSIDE:
drawHorizontalBorders(range, borderType, BorderExtent.INSIDE); drawHorizontalBorders(range, borderType, BorderExtent.INSIDE);
drawVerticalBorders(range, borderType, BorderExtent.INSIDE); drawVerticalBorders(range, borderType, BorderExtent.INSIDE);
break; break;
case OUTSIDE: case OUTSIDE:
drawOutsideBorders(range, borderType, BorderExtent.ALL); drawOutsideBorders(range, borderType, BorderExtent.ALL);
break; break;
case TOP: case TOP:
drawTopBorder(range, borderType); drawTopBorder(range, borderType);
break; break;
case BOTTOM: case BOTTOM:
drawBottomBorder(range, borderType); drawBottomBorder(range, borderType);
break; break;
case LEFT: case LEFT:
drawLeftBorder(range, borderType); drawLeftBorder(range, borderType);
break; break;
case RIGHT: case RIGHT:
drawRightBorder(range, borderType); drawRightBorder(range, borderType);
break; break;
case HORIZONTAL: case HORIZONTAL:
drawHorizontalBorders(range, borderType, BorderExtent.ALL); drawHorizontalBorders(range, borderType, BorderExtent.ALL);
break; break;
case INSIDE_HORIZONTAL: case INSIDE_HORIZONTAL:
drawHorizontalBorders(range, borderType, BorderExtent.INSIDE); drawHorizontalBorders(range, borderType, BorderExtent.INSIDE);
break; break;
case OUTSIDE_HORIZONTAL: case OUTSIDE_HORIZONTAL:
drawOutsideBorders(range, borderType, BorderExtent.HORIZONTAL); drawOutsideBorders(range, borderType, BorderExtent.HORIZONTAL);
break; break;
case VERTICAL: case VERTICAL:
drawVerticalBorders(range, borderType, BorderExtent.ALL); drawVerticalBorders(range, borderType, BorderExtent.ALL);
break; break;
case INSIDE_VERTICAL: case INSIDE_VERTICAL:
drawVerticalBorders(range, borderType, BorderExtent.INSIDE); drawVerticalBorders(range, borderType, BorderExtent.INSIDE);
break; break;
case OUTSIDE_VERTICAL: case OUTSIDE_VERTICAL:
drawOutsideBorders(range, borderType, BorderExtent.VERTICAL); drawOutsideBorders(range, borderType, BorderExtent.VERTICAL);
break; break;
} }
} }
/** /**
* Draws a group of cell borders for a cell range. The borders are not * Add a group of cell borders and colors for a cell range to the border property template.
* applied to the cells at this time, just the template is drawn. To apply * The borders are not applied to the cells at this time, just the template is drawn
* the drawn borders to a sheet, use {@link #applyBorders}. * (<code>drawBorders</code> stages changes using the border property template).
* To apply the drawn borders to a sheet, use {@link #applyBorders}.
* *
* @param range range of cells on which borders are drawn. * @param range range of cells on which borders are drawn.
* @param borderType Type of border to draw. * @param borderType Type of border to draw.
@ -374,21 +376,23 @@ public final class BorderPropertyTemplate {
*/ */
private void drawOutsideBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) { private void drawOutsideBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
switch (extent) { switch (extent) {
case ALL: case ALL:
case HORIZONTAL:
case VERTICAL:
if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) {
drawTopBorder(range, borderType); drawTopBorder(range, borderType);
drawBottomBorder(range, borderType); drawBottomBorder(range, borderType);
}
if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) {
drawLeftBorder(range, borderType); drawLeftBorder(range, borderType);
drawRightBorder(range, borderType); drawRightBorder(range, borderType);
} break;
break; case HORIZONTAL:
default: drawTopBorder(range, borderType);
throw new IllegalArgumentException( drawBottomBorder(range, borderType);
"Illegal BorderExtent. Allowed: ALL, HORIZONTAL, and VERTICAL"); break;
case VERTICAL:
drawLeftBorder(range, borderType);
drawRightBorder(range, borderType);
break;
default:
throw new IllegalArgumentException(
"Illegal BorderExtent. Allowed: ALL, HORIZONTAL, and VERTICAL");
} }
} }
@ -406,25 +410,27 @@ public final class BorderPropertyTemplate {
* </ul> * </ul>
*/ */
private void drawHorizontalBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) { private void drawHorizontalBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
switch (extent) { switch (extent) {
case ALL: case ALL:
case INSIDE: for (int i = firstRow; i <= lastRow; i++) {
int firstRow = range.getFirstRow(); CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstRow; i <= lastRow; i++) {
CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
if (extent == BorderExtent.ALL || i > firstRow) {
drawTopBorder(row, borderType); drawTopBorder(row, borderType);
}
if (extent == BorderExtent.ALL || i < lastRow) {
drawBottomBorder(row, borderType); drawBottomBorder(row, borderType);
} }
} break;
break; case INSIDE:
default: for (int i = firstRow; i <= lastRow; i++) {
throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE"); CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
if (i > firstRow) drawTopBorder(row, borderType);
if (i < lastRow) drawBottomBorder(row, borderType);
}
break;
default:
throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
} }
} }
@ -442,25 +448,27 @@ public final class BorderPropertyTemplate {
* </ul> * </ul>
*/ */
private void drawVerticalBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) { private void drawVerticalBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
switch (extent) { switch (extent) {
case ALL: case ALL:
case INSIDE: for (int i = firstCol; i <= lastCol; i++) {
int firstRow = range.getFirstRow(); CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstCol; i <= lastCol; i++) {
CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
if (extent == BorderExtent.ALL || i > firstCol) {
drawLeftBorder(row, borderType); drawLeftBorder(row, borderType);
}
if (extent == BorderExtent.ALL || i < lastCol) {
drawRightBorder(row, borderType); drawRightBorder(row, borderType);
} }
} break;
break; case INSIDE:
default: for (int i = firstCol; i <= lastCol; i++) {
throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE"); CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
if (i > firstCol) drawLeftBorder(row, borderType);
if (i < lastCol) drawRightBorder(row, borderType);
}
break;
default:
throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
} }
} }
@ -488,6 +496,12 @@ public final class BorderPropertyTemplate {
* Applies the drawn borders to a Sheet. The borders that are applied are * Applies the drawn borders to a Sheet. The borders that are applied are
* the ones that have been drawn by the {@link #drawBorders} and * the ones that have been drawn by the {@link #drawBorders} and
* {@link #drawBorderColors} methods. * {@link #drawBorderColors} methods.
* The same border property template can be applied to multiple sheets
* from the same or different workbooks.
*
* If the border property template contains cell addresses outside the valid range
* of <code>sheet</code>, borders for those cell addresses are quietly ignored and
* not applied to <code>sheet</code>.
* *
* @param sheet Sheet on which to apply borders * @param sheet Sheet on which to apply borders
* @since 3.15 beta 2 * @since 3.15 beta 2
@ -510,10 +524,11 @@ public final class BorderPropertyTemplate {
} }
/** /**
* Sets the color for a group of cell borders for a cell range. The borders * Sets the color for a group of cell borders for a cell range to the border property template.
* are not applied to the cells at this time, just the template is drawn. If * The borders are not applied to the cells at this time, just the template is drawn
* the borders do not exist, a {@link BorderStyle#THIN} border is used. To apply the * (<code>drawBorders</code> stages changes using the border property template).
* drawn borders to a sheet, use {@link #applyBorders}. * If the borders do not exist, a {@link BorderStyle#THIN} border is used.
* To apply the drawn borders to a sheet, use {@link #applyBorders}.
* *
* @param range range of cells on which colors are set. * @param range range of cells on which colors are set.
* @param color Color index from {@link IndexedColors} used to draw the borders. * @param color Color index from {@link IndexedColors} used to draw the borders.
@ -522,50 +537,50 @@ public final class BorderPropertyTemplate {
*/ */
public void drawBorderColors(CellRangeAddress range, short color, BorderExtent extent) { public void drawBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
switch (extent) { switch (extent) {
case NONE: case NONE:
removeBorderColors(range); removeBorderColors(range);
break; break;
case ALL: case ALL:
drawHorizontalBorderColors(range, color, BorderExtent.ALL); drawHorizontalBorderColors(range, color, BorderExtent.ALL);
drawVerticalBorderColors(range, color, BorderExtent.ALL); drawVerticalBorderColors(range, color, BorderExtent.ALL);
break; break;
case INSIDE: case INSIDE:
drawHorizontalBorderColors(range, color, BorderExtent.INSIDE); drawHorizontalBorderColors(range, color, BorderExtent.INSIDE);
drawVerticalBorderColors(range, color, BorderExtent.INSIDE); drawVerticalBorderColors(range, color, BorderExtent.INSIDE);
break; break;
case OUTSIDE: case OUTSIDE:
drawOutsideBorderColors(range, color, BorderExtent.ALL); drawOutsideBorderColors(range, color, BorderExtent.ALL);
break; break;
case TOP: case TOP:
drawTopBorderColor(range, color); drawTopBorderColor(range, color);
break; break;
case BOTTOM: case BOTTOM:
drawBottomBorderColor(range, color); drawBottomBorderColor(range, color);
break; break;
case LEFT: case LEFT:
drawLeftBorderColor(range, color); drawLeftBorderColor(range, color);
break; break;
case RIGHT: case RIGHT:
drawRightBorderColor(range, color); drawRightBorderColor(range, color);
break; break;
case HORIZONTAL: case HORIZONTAL:
drawHorizontalBorderColors(range, color, BorderExtent.ALL); drawHorizontalBorderColors(range, color, BorderExtent.ALL);
break; break;
case INSIDE_HORIZONTAL: case INSIDE_HORIZONTAL:
drawHorizontalBorderColors(range, color, BorderExtent.INSIDE); drawHorizontalBorderColors(range, color, BorderExtent.INSIDE);
break; break;
case OUTSIDE_HORIZONTAL: case OUTSIDE_HORIZONTAL:
drawOutsideBorderColors(range, color, BorderExtent.HORIZONTAL); drawOutsideBorderColors(range, color, BorderExtent.HORIZONTAL);
break; break;
case VERTICAL: case VERTICAL:
drawVerticalBorderColors(range, color, BorderExtent.ALL); drawVerticalBorderColors(range, color, BorderExtent.ALL);
break; break;
case INSIDE_VERTICAL: case INSIDE_VERTICAL:
drawVerticalBorderColors(range, color, BorderExtent.INSIDE); drawVerticalBorderColors(range, color, BorderExtent.INSIDE);
break; break;
case OUTSIDE_VERTICAL: case OUTSIDE_VERTICAL:
drawOutsideBorderColors(range, color, BorderExtent.VERTICAL); drawOutsideBorderColors(range, color, BorderExtent.VERTICAL);
break; break;
} }
} }
@ -683,21 +698,23 @@ public final class BorderPropertyTemplate {
*/ */
private void drawOutsideBorderColors(CellRangeAddress range, short color, BorderExtent extent) { private void drawOutsideBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
switch (extent) { switch (extent) {
case ALL: case ALL:
case HORIZONTAL:
case VERTICAL:
if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) {
drawTopBorderColor(range, color); drawTopBorderColor(range, color);
drawBottomBorderColor(range, color); drawBottomBorderColor(range, color);
}
if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) {
drawLeftBorderColor(range, color); drawLeftBorderColor(range, color);
drawRightBorderColor(range, color); drawRightBorderColor(range, color);
} break;
break; case HORIZONTAL:
default: drawTopBorderColor(range, color);
throw new IllegalArgumentException( drawBottomBorderColor(range, color);
"Illegal BorderExtent. Allowed: ALL, HORIZONTAL, and VERTICAL"); break;
case VERTICAL:
drawLeftBorderColor(range, color);
drawRightBorderColor(range, color);
break;
default:
throw new IllegalArgumentException(
"Illegal BorderExtent. Allowed: ALL, HORIZONTAL, and VERTICAL");
} }
} }
@ -715,25 +732,27 @@ public final class BorderPropertyTemplate {
* </ul> * </ul>
*/ */
private void drawHorizontalBorderColors(CellRangeAddress range, short color, BorderExtent extent) { private void drawHorizontalBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
switch (extent) { switch (extent) {
case ALL: case ALL:
case INSIDE: for (int i = firstRow; i <= lastRow; i++) {
int firstRow = range.getFirstRow(); CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstRow; i <= lastRow; i++) {
CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
if (extent == BorderExtent.ALL || i > firstRow) {
drawTopBorderColor(row, color); drawTopBorderColor(row, color);
}
if (extent == BorderExtent.ALL || i < lastRow) {
drawBottomBorderColor(row, color); drawBottomBorderColor(row, color);
} }
} break;
break; case INSIDE:
default: for (int i = firstRow; i <= lastRow; i++) {
throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE"); CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
if (i > firstRow) drawTopBorderColor(row, color);
if (i < lastRow) drawBottomBorderColor(row, color);
}
break;
default:
throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
} }
} }
@ -751,25 +770,27 @@ public final class BorderPropertyTemplate {
* </ul> * </ul>
*/ */
private void drawVerticalBorderColors(CellRangeAddress range, short color, BorderExtent extent) { private void drawVerticalBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
switch (extent) { switch (extent) {
case ALL: case ALL:
case INSIDE: for (int i = firstCol; i <= lastCol; i++) {
int firstRow = range.getFirstRow(); CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstCol; i <= lastCol; i++) {
CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
if (extent == BorderExtent.ALL || i > firstCol) {
drawLeftBorderColor(row, color); drawLeftBorderColor(row, color);
}
if (extent == BorderExtent.ALL || i < lastCol) {
drawRightBorderColor(row, color); drawRightBorderColor(row, color);
} }
} break;
break; case INSIDE:
default: for (int i = firstCol; i <= lastCol; i++) {
throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE"); CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
if (i > firstCol) drawLeftBorderColor(row, color);
if (i < lastCol) drawRightBorderColor(row, color);
}
break;
default:
throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
} }
} }