aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/poi/ss
diff options
context:
space:
mode:
authorVladislav Galas <gallon@apache.org>2019-01-26 23:20:07 +0000
committerVladislav Galas <gallon@apache.org>2019-01-26 23:20:07 +0000
commita08b69df801bac0fdb40071fb2007b6bfac37bb1 (patch)
treed178accd5bcb7d52eba98a7c0de25b5b7d58896b /src/java/org/apache/poi/ss
parent53eee01b5dcd28ab894f85403a0e640c054f089f (diff)
downloadpoi-a08b69df801bac0fdb40071fb2007b6bfac37bb1.tar.gz
poi-a08b69df801bac0fdb40071fb2007b6bfac37bb1.zip
pulled *Cell.setCellValue(String), setCellValue(RichTextString) to the common base
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1852255 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/ss')
-rw-r--r--src/java/org/apache/poi/ss/usermodel/CellBase.java70
1 files changed, 68 insertions, 2 deletions
diff --git a/src/java/org/apache/poi/ss/usermodel/CellBase.java b/src/java/org/apache/poi/ss/usermodel/CellBase.java
index 7506ae3419..7193075fa7 100644
--- a/src/java/org/apache/poi/ss/usermodel/CellBase.java
+++ b/src/java/org/apache/poi/ss/usermodel/CellBase.java
@@ -17,6 +17,7 @@
package org.apache.poi.ss.usermodel;
+import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.FormulaParseException;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
@@ -25,6 +26,7 @@ import org.apache.poi.util.Removal;
import java.util.Calendar;
import java.util.Date;
+import java.util.Locale;
/**
* Common implementation-independent logic shared by all implementations of {@link Cell}.
@@ -196,7 +198,7 @@ public abstract class CellBase implements Cell {
* {@inheritDoc}
*/
@Override
- public final void setCellValue(double value) {
+ public void setCellValue(double value) {
if(Double.isInfinite(value)) {
// Excel does not support positive/negative infinities,
// rather, it gives a #DIV/0! error in these cases.
@@ -239,7 +241,7 @@ public abstract class CellBase implements Cell {
* {@inheritDoc}
*/
@Override
- public final void setCellValue(Calendar value) {
+ public void setCellValue(Calendar value) {
if(value == null) {
setBlank();
return;
@@ -255,4 +257,68 @@ public abstract class CellBase implements Cell {
* @param value the new calendar value to set
*/
protected abstract void setCellValueImpl(Calendar value);
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void setCellValue(String value) {
+ if(value == null){
+ setBlank();
+ return;
+ }
+
+ checkLength(value);
+
+ setCellValueImpl(value);
+ }
+
+ /**
+ * Implementation-specific way to set a string value.
+ * The value is guaranteed to be non-null and to satisfy the length limitation imposed by the spreadsheet version.
+ * The implementation is expected to adjust cell type accordingly, so that after this call
+ * getCellType() or getCachedFormulaResultType() (whichever appropriate) would return {@link CellType#STRING}.
+ * @param value the new value to set.
+ */
+ protected abstract void setCellValueImpl(String value);
+
+ private void checkLength(String value) {
+ if(value.length() > getSpreadsheetVersion().getMaxTextLength()){
+ final String message = String.format(Locale.ROOT,
+ "The maximum length of cell contents (text) is %d characters",
+ getSpreadsheetVersion().getMaxTextLength());
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void setCellValue(RichTextString value) {
+ if(value == null || value.getString() == null){
+ setBlank();
+ return;
+ }
+
+ checkLength(value.getString());
+
+ setCellValueImpl(value);
+ }
+
+ /**
+ * Implementation-specific way to set a RichTextString value.
+ * The value is guaranteed to be non-null, having non-null value, and to satisfy the length limitation imposed
+ * by the spreadsheet version.
+ * The implementation is expected to adjust cell type accordingly, so that after this call
+ * getCellType() or getCachedFormulaResultType() (whichever appropriate) would return {@link CellType#STRING}.
+ * @param value the new value to set.
+ */
+ protected abstract void setCellValueImpl(RichTextString value);
+
+ /**
+ * Get the spreadsheet version for the given implementation.
+ * @return the spreadsheet version
+ */
+ protected abstract SpreadsheetVersion getSpreadsheetVersion();
}