// Next, from the columns width, calculate how many co-ordinate
// positons there are per millimetre
- coordinatePositionsPerMM = ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS /
- colWidthMM;
+ coordinatePositionsPerMM = colWidthMM == 0 ? 0
+ : ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS / colWidthMM;
// From this figure, determine how many co-ordinat positions to
// inset the left hand or bottom edge of the image.
inset = (int)(coordinatePositionsPerMM * overlapMM);
row = sheet.createRow(toRow);
}
// Get the row's height in millimetres and add to the running total.
- rowHeightMM = row.getHeightInPoints() /
- ConvertImageUnits.POINTS_PER_MILLIMETRE;
+ rowHeightMM = row.getHeightInPoints() / ConvertImageUnits.POINTS_PER_MILLIMETRE;
totalRowHeightMM += rowHeightMM;
toRow++;
}
overlapMM = 0.0D;
}
- rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS /
- rowHeightMM;
+ rowCoordinatesPerMM = rowHeightMM == 0 ? 0
+ : ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM;
inset = (int)(overlapMM * rowCoordinatesPerMM);
- clientAnchorDetail = new ClientAnchorDetail(startingRow,
- toRow, inset);
+ clientAnchorDetail = new ClientAnchorDetail(startingRow, toRow, inset);
}
return(clientAnchorDetail);
}
// is then instantiated.
if(sheet instanceof HSSFSheet) {
colWidthMM = reqImageWidthMM;
- colCoordinatesPerMM = ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS /
- colWidthMM;
+ colCoordinatesPerMM = colWidthMM == 0 ? 0
+ : ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS / colWidthMM;
pictureWidthCoordinates = (int)(reqImageWidthMM * colCoordinatesPerMM);
}
ConvertImageUnits.POINTS_PER_MILLIMETRE));
if(sheet instanceof HSSFSheet) {
rowHeightMM = reqImageHeightMM;
- rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS /
- rowHeightMM;
+ rowCoordinatesPerMM = rowHeightMM == 0 ? 0
+ : ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM;
pictureHeightCoordinates = (int)(reqImageHeightMM *
rowCoordinatesPerMM);
}
public final class BigGridDemo {
private static final String XML_ENCODING = "UTF-8";
+ private static final Random rnd = new Random();
+
private BigGridDemo() {}
public static void main(String[] args) throws Exception {
private static void generate(Writer out, Map<String, XSSFCellStyle> styles) throws Exception {
- Random rnd = new Random();
Calendar calendar = Calendar.getInstance();
SpreadsheetWriter sw = new SpreadsheetWriter(out);
* Instances of this class are used to evaluate a single cell. This is usually
* after some values have been set. The evaluation is actually performed
* by a WorkbookUtil instance. The evaluate() method of the WorkbookUtil
- * class returns an EvaluationResult which encapsulates the results and
+ * class returns an EvaluationResult which encapsulates the results and
* information from the evaluation.
- *
+ *
* @author Jon Svede ( jon [at] loquatic [dot] com )
* @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
private String cell ;
private double expectedValue ;
private double precision ;
- private double precisionToUse ;
private double globalPrecision ;
private boolean requiredToPass;
-
-
+
+
private ExcelAntEvaluationResult result ;
-
+
private ExcelAntWorkbookUtil wbUtil ;
-
+
private boolean showDelta;
-
-
+
+
public ExcelAntEvaluateCell() {}
protected void setWorkbookUtil( ExcelAntWorkbookUtil wb ) {
wbUtil = wb ;
}
-
+
public void setShowDelta( boolean value ) {
showDelta = value ;
}
-
+
protected boolean showDelta() {
return showDelta ;
}
-
+
public void setCell(String cell) {
this.cell = cell;
}
-
+
public void setRequiredToPass( boolean val ) {
requiredToPass = val ;
}
-
+
protected boolean requiredToPass() {
return requiredToPass ;
}
public void setPrecision(double precision) {
this.precision = precision;
}
-
+
protected void setGlobalPrecision( double prec ) {
globalPrecision = prec ;
}
return expectedValue;
}
+ @SuppressWarnings("squid:S4275")
protected double getPrecision() {
- return precisionToUse;
- }
-
- @Override
- public void execute() throws BuildException {
-
- precisionToUse = 0 ;
-
// if there is a globalPrecision we will use it unless there is also
// precision set at the evaluate level, then we use that. If there
// is not a globalPrecision, we will use the local precision.
log( "test precision = " + precision + "\tglobal precision = " + globalPrecision, Project.MSG_VERBOSE ) ;
if( globalPrecision > 0 ) {
if( precision > 0 ) {
- precisionToUse = precision ;
log( "Using evaluate precision of " + precision + " over the " +
- "global precision of " + globalPrecision, Project.MSG_VERBOSE ) ;
+ "global precision of " + globalPrecision, Project.MSG_VERBOSE ) ;
+ return precision ;
} else {
- precisionToUse = globalPrecision ;
log( "Using global precision of " + globalPrecision, Project.MSG_VERBOSE ) ;
+ return globalPrecision ;
}
} else {
- precisionToUse = precision ;
log( "Using evaluate precision of " + precision, Project.MSG_VERBOSE ) ;
+ return precision ;
}
- result = wbUtil.evaluateCell(cell, expectedValue, precisionToUse ) ;
-
+ }
+
+ @Override
+ public void execute() throws BuildException {
+ result = wbUtil.evaluateCell(cell, expectedValue, getPrecision() ) ;
+
StringBuilder sb = new StringBuilder() ;
sb.append( "evaluation of cell " ) ;
- sb.append( cell ) ;
+ sb.append( cell ) ;
sb.append( " resulted in " ) ;
sb.append( result.getReturnValue() ) ;
if(showDelta) {
sb.append(" with a delta of ").append(result.getDelta());
}
-
+
log( sb.toString(), Project.MSG_DEBUG) ;
}
-
+
public ExcelAntEvaluationResult getResult() {
return result ;
}
package org.apache.poi.sl.draw.geom;
-import static java.lang.Math.abs;
-import static java.lang.Math.atan2;
-import static java.lang.Math.cos;
-import static java.lang.Math.max;
-import static java.lang.Math.min;
-import static java.lang.Math.sin;
-import static java.lang.Math.sqrt;
-import static java.lang.Math.tan;
-import static java.lang.Math.toDegrees;
-import static java.lang.Math.toRadians;
+import static java.lang.Math.*;
+
+import java.util.regex.Pattern;
import org.apache.poi.sl.draw.binding.CTGeomGuide;
muldiv,addsub,adddiv,ifelse,val,abs,sqrt,max,min,at2,sin,cos,tan,cat2,sat2,pin,mod
}
+ private static final Pattern WHITESPACE = Pattern.compile("\\s+");
+
+ @SuppressWarnings({"FieldCanBeLocal", "unused"})
private final String name, fmla;
private final Op op;
private final String[] operands;
public Guide(String nm, String fm){
name = nm;
fmla = fm;
- operands = fm.split("\\s+");
- op = Op.valueOf(operands[0].replace("*", "mul").replace("/", "div").replace("+", "add").replace("-", "sub").replace("?:", "ifelse"));
+ operands = WHITESPACE.split(fm);
+ switch (operands[0]) {
+ case "*/": op = Op.muldiv; break;
+ case "+-": op = Op.addsub; break;
+ case "+/": op = Op.adddiv; break;
+ case "?:": op = Op.ifelse; break;
+ default: op = Op.valueOf(operands[0]); break;
+ }
}
public String getName(){
return name;
}
- String getFormula(){
- return fmla;
- }
-
@Override
public double evaluate(Context ctx) {
double x = (operands.length > 1) ? ctx.getValue(operands[1]) : 0;
return abs(x);
case adddiv:
// Add Divide Formula
- return (x + y) / z;
+ return (z == 0) ? 0 : (x + y) / z;
case addsub:
// Add Subtract Formula
return (x + y) - z;
return x * cos(toRadians(y / OOXML_DEGREE));
case cat2:
// Cosine ArcTan Formula: "cat2 x y z" = (x * cos(arctan(z / y) )) = value of this guide
- return x*cos(atan2(z, y));
+ return x * cos(atan2(z, y));
case ifelse:
// If Else Formula: "?: x y z" = if (x > 0), then y = value of this guide,
// else z = value of this guide
return sqrt(x*x + y*y + z*z);
case muldiv:
// Multiply Divide Formula
- return (x * y) / z;
+ return (z == 0) ? 0 : (x * y) / z;
case pin:
// Pin To Formula: "pin x y z" = if (y < x), then x = value of this guide
// else if (y > z), then z = value of this guide
// else y = value of this guide
- if(y < x) {
- return x;
- } else if (y > z) {
- return z;
- } else {
- return y;
- }
+ return max(x, min(y, z));
case sat2:
// Sine ArcTan Formula: "sat2 x y z" = (x*sin(arctan(z / y))) = value of this guide
- return x*sin(atan2(z, y));
+ return x * sin(atan2(z, y));
case sin:
// Sine Formula: "sin x y" = (x * sin( y )) = value of this guide
return x * sin(toRadians(y / OOXML_DEGREE));
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.usermodel.DateUtil;
+import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil;
*
* Algorithm inspired by www.dwheeler.com/yearfrac
*/
+@Internal
final class YearFracCalculator {
private static final int MS_PER_HOUR = 60 * 60 * 1000;
private static final int MS_PER_DAY = 24 * MS_PER_HOUR;
* @param startDateVal assumed to be less than or equal to endDateVal
* @param endDateVal assumed to be greater than or equal to startDateVal
*/
- public static double basis0(int startDateVal, int endDateVal) {
+ private static double basis0(int startDateVal, int endDateVal) {
SimpleDate startDate = createDate(startDateVal);
SimpleDate endDate = createDate(endDateVal);
int date1day = startDate.day;
* @param startDateVal assumed to be less than or equal to endDateVal
* @param endDateVal assumed to be greater than or equal to startDateVal
*/
- public static double basis1(int startDateVal, int endDateVal) {
+ private static double basis1(int startDateVal, int endDateVal) {
+ assert(startDateVal <= endDateVal);
SimpleDate startDate = createDate(startDateVal);
SimpleDate endDate = createDate(endDateVal);
double yearLength;
if (isGreaterThanOneYear(startDate, endDate)) {
yearLength = averageYearLength(startDate.year, endDate.year);
+ assert(yearLength > 0);
} else if (shouldCountFeb29(startDate, endDate)) {
yearLength = DAYS_PER_LEAP_YEAR;
} else {
* @param startDateVal assumed to be less than or equal to endDateVal
* @param endDateVal assumed to be greater than or equal to startDateVal
*/
- public static double basis2(int startDateVal, int endDateVal) {
+ private static double basis2(int startDateVal, int endDateVal) {
return (endDateVal - startDateVal) / 360.0;
}
/**
* @param startDateVal assumed to be less than or equal to endDateVal
* @param endDateVal assumed to be greater than or equal to startDateVal
*/
- public static double basis3(double startDateVal, double endDateVal) {
+ private static double basis3(double startDateVal, double endDateVal) {
return (endDateVal - startDateVal) / 365.0;
}
/**
* @param startDateVal assumed to be less than or equal to endDateVal
* @param endDateVal assumed to be greater than or equal to startDateVal
*/
- public static double basis4(int startDateVal, int endDateVal) {
+ private static double basis4(int startDateVal, int endDateVal) {
SimpleDate startDate = createDate(startDateVal);
SimpleDate endDate = createDate(endDateVal);
int date1day = startDate.day;
}
private static double averageYearLength(int startYear, int endYear) {
+ assert(startYear <= endYear);
int dayCount = 0;
for (int i=startYear; i<=endYear; i++) {
- dayCount += DAYS_PER_NORMAL_YEAR;
- if (isLeapYear(i)) {
- dayCount++;
- }
+ dayCount += isLeapYear(i) ? DAYS_PER_LEAP_YEAR : DAYS_PER_NORMAL_YEAR;
}
double numberOfYears = endYear-startYear+1;
return dayCount / numberOfYears;
return true;
}
// all other centuries are *not* leap years
- if (i % 100 == 0) {
- return false;
- }
- return true;
+ return i % 100 != 0;
}
private static boolean isGreaterThanOneYear(SimpleDate start, SimpleDate end) {
+ assert(start.year <= end.year);
if (start.year == end.year) {
return false;
}
}
private static SimpleDate createDate(int dayCount) {
- /** use UTC time-zone to avoid daylight savings issues */
+ /* use UTC time-zone to avoid daylight savings issues */
Calendar cal = LocaleUtil.getLocaleCalendar(LocaleUtil.TIMEZONE_UTC);
DateUtil.setCalendar(cal, dayCount, 0, false, false);
return new SimpleDate(cal);
package org.apache.poi.ss.formula.functions;
-import org.apache.poi.ss.formula.eval.*;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.EvaluationException;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
/**
* Calculates the internal rate of return.
final double value = values[k];
fValue += value / denominator;
denominator *= factor;
+ if (denominator == 0) {
+ return Double.NaN;
+ }
fDerivative -= k * value / denominator;
}
- // the essense of the Newton-Raphson Method
- x1 = x0 - fValue/fDerivative;
+ // the essence of the Newton-Raphson Method
+ if (fDerivative == 0) {
+ return Double.NaN;
+ }
+ x1 = x0 - fValue/fDerivative;
if (Math.abs(x1 - x0) <= absoluteAccuracy) {
return x1;
* @author Johan Karlsteen
*/
public final class LinearRegressionFunction extends Fixed2ArgFunction {
-
+
private static abstract class ValueArray implements ValueVector {
private final int _size;
protected ValueArray(int size) {
}
protected ValueEval getItemInternal(int index) {
- int sIx = (index % _width) + _ref.getFirstSheetIndex();
+ int sIx = (index % _width) + _ref.getFirstSheetIndex();
return _ref.getInnerValueEval(sIx);
}
}
public enum FUNCTION {INTERCEPT, SLOPE}
public FUNCTION function;
-
+
public LinearRegressionFunction(FUNCTION function) {
this.function = function;
}
-
+
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex,
ValueEval arg0, ValueEval arg1) {
double result;
}
return new NumberEval(result);
}
-
+
private double evaluateInternal(ValueVector x, ValueVector y, int size)
throws EvaluationException {
// error handling is as if the x is fully evaluated before y
- ErrorEval firstXerr = null;
ErrorEval firstYerr = null;
boolean accumlatedSome = false;
// first pass: read in data, compute xbar and ybar
double sumx = 0.0, sumy = 0.0;
-
+
for (int i = 0; i < size; i++) {
ValueEval vx = x.getItem(i);
ValueEval vy = y.getItem(i);
if (vx instanceof ErrorEval) {
- if (firstXerr == null) {
- firstXerr = (ErrorEval) vx;
- continue;
- }
+ throw new EvaluationException((ErrorEval) vx);
}
if (vy instanceof ErrorEval) {
if (firstYerr == null) {
}
}
// only count pairs if both elements are numbers
+ // all other combinations of value types are silently ignored
if (vx instanceof NumberEval && vy instanceof NumberEval) {
accumlatedSome = true;
NumberEval nx = (NumberEval) vx;
NumberEval ny = (NumberEval) vy;
sumx += nx.getNumberValue();
sumy += ny.getNumberValue();
- } else {
- // all other combinations of value types are silently ignored
}
}
+
+ if (firstYerr != null) {
+ throw new EvaluationException(firstYerr);
+ }
+
+ if (!accumlatedSome) {
+ throw new EvaluationException(ErrorEval.DIV_ZERO);
+ }
+
double xbar = sumx / size;
double ybar = sumy / size;
-
+
// second pass: compute summary statistics
double xxbar = 0.0, xybar = 0.0;
for (int i = 0; i < size; i++) {
ValueEval vx = x.getItem(i);
ValueEval vy = y.getItem(i);
-
- if (vx instanceof ErrorEval) {
- if (firstXerr == null) {
- firstXerr = (ErrorEval) vx;
- continue;
- }
- }
- if (vy instanceof ErrorEval) {
- if (firstYerr == null) {
- firstYerr = (ErrorEval) vy;
- continue;
- }
- }
-
+
// only count pairs if both elements are numbers
+ // all other combinations of value types are silently ignored
if (vx instanceof NumberEval && vy instanceof NumberEval) {
NumberEval nx = (NumberEval) vx;
NumberEval ny = (NumberEval) vy;
xxbar += (nx.getNumberValue() - xbar) * (nx.getNumberValue() - xbar);
xybar += (nx.getNumberValue() - xbar) * (ny.getNumberValue() - ybar);
- } else {
- // all other combinations of value types are silently ignored
}
}
- double beta1 = xybar / xxbar;
- double beta0 = ybar - beta1 * xbar;
-
- if (firstXerr != null) {
- throw new EvaluationException(firstXerr);
- }
- if (firstYerr != null) {
- throw new EvaluationException(firstYerr);
- }
- if (!accumlatedSome) {
+
+ if (xxbar == 0 ) {
throw new EvaluationException(ErrorEval.DIV_ZERO);
}
-
- if(function == FUNCTION.INTERCEPT) {
- return beta0;
- } else {
- return beta1;
- }
+
+ double beta1 = xybar / xxbar;
+ double beta0 = ybar - beta1 * xbar;
+
+ return (function == FUNCTION.INTERCEPT) ? beta0 : beta1;
}
private static ValueVector createValueVector(ValueEval arg) throws EvaluationException {
return XMLEventFactory.newInstance();
}
+ @SuppressWarnings("squid:S4435")
public static TransformerFactory getTransformerFactory() {
TransformerFactory factory = TransformerFactory.newInstance();
trySet(factory::setFeature, FEATURE_SECURE_PROCESSING, true);