protected List<EvaluationConditionalFormatRule> getRules(Sheet sheet) {
final String sheetName = sheet.getSheetName();
List<EvaluationConditionalFormatRule> rules = formats.get(sheetName);
- if (rules == null && ! formats.containsKey(sheetName)) {
+ if (rules == null) {
+ if (formats.containsKey(sheetName)) {
+ return Collections.emptyList();
+ }
final SheetConditionalFormatting scf = sheet.getSheetConditionalFormatting();
final int count = scf.getNumConditionalFormattings();
rules = new ArrayList<EvaluationConditionalFormatRule>(count);
public List<EvaluationConditionalFormatRule> getConditionalFormattingForCell(final CellReference cellRef) {
String sheetName = cellRef.getSheetName();
Sheet sheet = null;
- if (sheetName == null) sheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
- else sheet = workbook.getSheet(sheetName);
+ if (sheetName == null) {
+ sheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
+ } else {
+ sheet = workbook.getSheet(sheetName);
+ }
final Cell cell = SheetUtil.getCell(sheet, cellRef.getRow(), cellRef.getCol());
- if (cell == null) return Collections.emptyList();
+ if (cell == null) {
+ return Collections.emptyList();
+ }
return getConditionalFormattingForCell(cell, cellRef);
}
boolean stopIfTrue = false;
for (EvaluationConditionalFormatRule rule : getRules(cell.getSheet())) {
- if (stopIfTrue) continue; // a previous rule matched and wants no more evaluations
+ if (stopIfTrue) {
+ continue; // a previous rule matched and wants no more evaluations
+ }
if (rule.matches(cell)) {
rules.add(rule);
for (CellRangeAddress region : rule.getRegions()) {
for (int r = region.getFirstRow(); r <= region.getLastRow(); r++) {
final Row row = sheet.getRow(r);
- if (row == null) continue; // no cells to check
+ if (row == null) {
+ continue; // no cells to check
+ }
for (int c = region.getFirstColumn(); c <= region.getLastColumn(); c++) {
final Cell cell = row.getCell(c);
- if (cell == null) continue;
+ if (cell == null) {
+ continue;
+ }
List<EvaluationConditionalFormatRule> cellRules = getConditionalFormattingForCell(cell);
- if (cellRules.contains(rule)) cells.add(cell);
+ if (cellRules.contains(rule)) {
+ cells.add(cell);
+ }
}
}
}
* Defined as equal sheet name and formatting and rule indexes
* @see java.lang.Object#equals(java.lang.Object)
*/
+ @Override
public boolean equals(Object obj) {
- if (obj == null) return false;
- if (! obj.getClass().equals(this.getClass())) return false;
+ if (obj == null) {
+ return false;
+ }
+ if (! obj.getClass().equals(this.getClass())) {
+ return false;
+ }
final EvaluationConditionalFormatRule r = (EvaluationConditionalFormatRule) obj;
return getSheet().getSheetName().equalsIgnoreCase(r.getSheet().getSheetName())
&& getFormattingIndex() == r.getFormattingIndex()
* @param o
* @return comparison based on sheet name, formatting index, and rule priority
*/
+ @Override
public int compareTo(EvaluationConditionalFormatRule o) {
int cmp = getSheet().getSheetName().compareToIgnoreCase(o.getSheet().getSheetName());
- if (cmp != 0) return cmp;
+ if (cmp != 0) {
+ return cmp;
+ }
final int x = getPriority();
final int y = o.getPriority();
// logic from Integer.compare()
cmp = (x < y) ? -1 : ((x == y) ? 0 : 1);
- if (cmp != 0) return cmp;
+ if (cmp != 0) {
+ return cmp;
+ }
cmp = new Integer(getFormattingIndex()).compareTo(new Integer(o.getFormattingIndex()));
- if (cmp != 0) return cmp;
+ if (cmp != 0) {
+ return cmp;
+ }
return new Integer(getRuleIndex()).compareTo(new Integer(o.getRuleIndex()));
}
+ @Override
public int hashCode() {
int hash = sheet.getSheetName().hashCode();
hash = 31 * hash + formattingIndex;
}
}
- if (region == null) return false; // cell not in range of this rule
+ if (region == null) {
+ // cell not in range of this rule
+ return false;
+ }
final ConditionType ruleType = getRule().getConditionType();
|| (DataValidationEvaluator.isType(cell,CellType.STRING)
&& (cell.getStringCellValue() == null || cell.getStringCellValue().isEmpty())
)
- ) return false;
+ ) {
+ return false;
+ }
ValueEval eval = unwrapEval(workbookEvaluator.evaluate(rule.getFormula1(), ConditionalFormattingEvaluator.getRef(cell), region));
ValueEval comp = unwrapEval(workbookEvaluator.evaluate(rule.getFormula1(), ConditionalFormattingEvaluator.getRef(cell), region));
// Copied for now from DataValidationEvaluator.ValidationEnum.FORMULA#isValidValue()
- if (comp instanceof BlankEval) return true;
- if (comp instanceof ErrorEval) return false;
+ if (comp instanceof BlankEval) {
+ return true;
+ }
+ if (comp instanceof ErrorEval) {
+ return false;
+ }
if (comp instanceof BoolEval) {
return ((BoolEval) comp).getBooleanValue();
}
private boolean checkFilter(Cell cell, CellRangeAddress region) {
final ConditionFilterType filterType = rule.getConditionFilterType();
- if (filterType == null) return false;
+ if (filterType == null) {
+ return false;
+ }
// TODO: this could/should be delegated to the Enum type, but that's in the usermodel package,
// we may not want evaluation code there. Of course, maybe the enum should go here in formula,
// numbers stored as text are ignored, but numbers formatted as text are treated as numbers.
final ValueAndFormat cv10 = getCellValue(cell);
- if (! cv10.isNumber()) return false;
+ if (! cv10.isNumber()) {
+ return false;
+ }
return getMeaningfulValues(region, false, new ValueFunction() {
+ @Override
public Set<ValueAndFormat> evaluate(List<ValueAndFormat> allValues) {
List<ValueAndFormat> values = allValues;
final ConditionFilterData conf = rule.getFilterConfiguration();
- if (! conf.getBottom()) Collections.sort(values, Collections.reverseOrder());
- else Collections.sort(values);
+ if (! conf.getBottom()) {
+ Collections.sort(values, Collections.reverseOrder());
+ } else {
+ Collections.sort(values);
+ }
int limit = (int) conf.getRank();
- if (conf.getPercent()) limit = allValues.size() * limit / 100;
- if (allValues.size() <= limit) return new HashSet<ValueAndFormat>(allValues);
+ if (conf.getPercent()) {
+ limit = allValues.size() * limit / 100;
+ }
+ if (allValues.size() <= limit) {
+ return new HashSet<ValueAndFormat>(allValues);
+ }
return new HashSet<ValueAndFormat>(allValues.subList(0, limit));
}
// Per Excel help, "duplicate" means matching value AND format
// https://support.office.com/en-us/article/Filter-for-unique-values-or-remove-duplicate-values-ccf664b0-81d6-449b-bbe1-8daaec1e83c2
return getMeaningfulValues(region, true, new ValueFunction() {
+ @Override
public Set<ValueAndFormat> evaluate(List<ValueAndFormat> allValues) {
List<ValueAndFormat> values = allValues;
Collections.sort(values);
// Per Excel help, "duplicate" means matching value AND format
// https://support.office.com/en-us/article/Filter-for-unique-values-or-remove-duplicate-values-ccf664b0-81d6-449b-bbe1-8daaec1e83c2
return getMeaningfulValues(region, true, new ValueFunction() {
+ @Override
public Set<ValueAndFormat> evaluate(List<ValueAndFormat> allValues) {
List<ValueAndFormat> values = allValues;
Collections.sort(values);
// actually ordered, so iteration order is predictable
List<ValueAndFormat> values = new ArrayList<ValueAndFormat>(getMeaningfulValues(region, false, new ValueFunction() {
+ @Override
public Set<ValueAndFormat> evaluate(List<ValueAndFormat> allValues) {
List<ValueAndFormat> values = allValues;
double total = 0;
final ValueAndFormat cv = getCellValue(cell);
Double val = cv.isNumber() ? cv.getValue() : null;
- if (val == null) return false;
+ if (val == null) {
+ return false;
+ }
double avg = values.get(0).value.doubleValue();
double stdDev = values.get(1).value.doubleValue();
OperatorEnum op = null;
if (conf.getAboveAverage()) {
- if (conf.getEqualAverage()) op = OperatorEnum.GREATER_OR_EQUAL;
- else op = OperatorEnum.GREATER_THAN;
+ if (conf.getEqualAverage()) {
+ op = OperatorEnum.GREATER_OR_EQUAL;
+ } else {
+ op = OperatorEnum.GREATER_THAN;
+ }
} else {
- if (conf.getEqualAverage()) op = OperatorEnum.LESS_OR_EQUAL;
- else op = OperatorEnum.LESS_THAN;
+ if (conf.getEqualAverage()) {
+ op = OperatorEnum.LESS_OR_EQUAL;
+ } else {
+ op = OperatorEnum.LESS_THAN;
+ }
}
return op != null && op.isValid(val, comp, null);
case CONTAINS_TEXT:
*/
private Set<ValueAndFormat> getMeaningfulValues(CellRangeAddress region, boolean withText, ValueFunction func) {
Set<ValueAndFormat> values = meaningfulRegionValues.get(region);
- if (values != null) return values;
+ if (values != null) {
+ return values;
+ }
List<ValueAndFormat> allValues = new ArrayList<ValueAndFormat>((region.getLastColumn() - region.getFirstColumn()+1) * (region.getLastRow() - region.getFirstRow() + 1));
for (int r=region.getFirstRow(); r <= region.getLastRow(); r++) {
final Row row = sheet.getRow(r);
- if (row == null) continue;
+ if (row == null) {
+ continue;
+ }
for (int c = region.getFirstColumn(); c <= region.getLastColumn(); c++) {
Cell cell = row.getCell(c);
final ValueAndFormat cv = getCellValue(cell);
- if (cv != null && (withText || cv.isNumber()) ) allValues.add(cv);
+ if (cv != null && (withText || cv.isNumber()) ) {
+ allValues.add(cv);
+ }
}
}
public static enum OperatorEnum {
NO_COMPARISON {
/** always false/invalid */
+ @Override
public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
return false;
}
},
BETWEEN {
+ @Override
public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
return cellValue.compareTo(v1) >= 0 && cellValue.compareTo(v2) <= 0;
}
},
NOT_BETWEEN {
+ @Override
public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
return cellValue.compareTo(v1) < 0 || cellValue.compareTo(v2) > 0;
}
},
EQUAL {
+ @Override
public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
// need to avoid instanceof, to work around a 1.6 compiler bug
if (cellValue.getClass() == String.class) {
}
},
NOT_EQUAL {
+ @Override
public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
// need to avoid instanceof, to work around a 1.6 compiler bug
if (cellValue.getClass() == String.class) {
}
},
GREATER_THAN {
+ @Override
public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
return cellValue.compareTo(v1) > 0;
}
},
LESS_THAN {
+ @Override
public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
return cellValue.compareTo(v1) < 0;
}
},
GREATER_OR_EQUAL {
+ @Override
public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
return cellValue.compareTo(v1) >= 0;
}
},
LESS_OR_EQUAL {
+ @Override
public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
return cellValue.compareTo(v1) <= 0;
}
/**
* Note: this class has a natural ordering that is inconsistent with equals.
*/
- protected class ValueAndFormat implements Comparable<ValueAndFormat> {
+ protected static class ValueAndFormat implements Comparable<ValueAndFormat> {
private final Double value;
private final String string;
return value;
}
+ @Override
public boolean equals(Object obj) {
+ if (!(obj instanceof ValueAndFormat)) {
+ return false;
+ }
ValueAndFormat o = (ValueAndFormat) obj;
return ( value == o.value || value.equals(o.value))
&& ( format == o.format || format.equals(o.format))
* @param o
* @return value comparison
*/
+ @Override
public int compareTo(ValueAndFormat o) {
- if (value == null && o.value != null) return 1;
- if (o.value == null && value != null) return -1;
+ if (value == null && o.value != null) {
+ return 1;
+ }
+ if (o.value == null && value != null) {
+ return -1;
+ }
int cmp = value == null ? 0 : value.compareTo(o.value);
- if (cmp != 0) return cmp;
+ if (cmp != 0) {
+ return cmp;
+ }
- if (string == null && o.string != null) return 1;
- if (o.string == null && string != null) return -1;
+ if (string == null && o.string != null) {
+ return 1;
+ }
+ if (o.string == null && string != null) {
+ return -1;
+ }
return string == null ? 0 : string.compareTo(o.string);
}
+ @Override
public int hashCode() {
return (string == null ? 0 : string.hashCode()) * 37 * 37 + 37 * (value == null ? 0 : value.hashCode()) + (format == null ? 0 : format.hashCode());
}
import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianConsts;
import org.apache.poi.util.RecordFormatException;
/**
//note that raw bytes includes the public comment identifier
int currentOffset = 4 + 16;//4 public comment identifier, 16 for outputrect
long countFormats = LittleEndian.getUInt(rawBytes, currentOffset);
- currentOffset += LittleEndian.INT_SIZE;
+ currentOffset += LittleEndianConsts.INT_SIZE;
List<EmrFormat> emrFormatList = new ArrayList<EmrFormat>();
for (long i = 0; i < countFormats; i++) {
emrFormatList.add(new EmrFormat(rawBytes, currentOffset));
- currentOffset += 4 * LittleEndian.INT_SIZE;
+ currentOffset += 4 * LittleEndianConsts.INT_SIZE;
}
List<HemfMultiFormatsData> list = new ArrayList<HemfMultiFormatsData>();
for (EmrFormat emrFormat : emrFormatList) {
return list;
}
- private class EmrFormat {
+ private static class EmrFormat {
long signature;
long version;
int size;
int offset;
public EmrFormat(byte[] rawBytes, int currentOffset) {
- signature = LittleEndian.getUInt(rawBytes, currentOffset); currentOffset += LittleEndian.INT_SIZE;
- version = LittleEndian.getUInt(rawBytes, currentOffset); currentOffset += LittleEndian.INT_SIZE;
+ signature = LittleEndian.getUInt(rawBytes, currentOffset); currentOffset += LittleEndianConsts.INT_SIZE;
+ version = LittleEndian.getUInt(rawBytes, currentOffset); currentOffset += LittleEndianConsts.INT_SIZE;
//spec says this must be a 32bit "aligned" typo for "signed"?
//realistically, this has to be an int...
- size = LittleEndian.getInt(rawBytes, currentOffset); currentOffset += LittleEndian.INT_SIZE;
+ size = LittleEndian.getInt(rawBytes, currentOffset); currentOffset += LittleEndianConsts.INT_SIZE;
//y, can be long, but realistically?
- offset = LittleEndian.getInt(rawBytes, currentOffset); currentOffset += LittleEndian.INT_SIZE;
+ offset = LittleEndian.getInt(rawBytes, currentOffset); currentOffset += LittleEndianConsts.INT_SIZE;
if (size < 0) {
throw new RecordFormatException("size for emrformat must be > 0");
}
private final byte[] wmfBytes;
public WindowsMetafile(byte[] rawBytes) {
super(rawBytes);
- int offset = LittleEndian.INT_SIZE;//public comment identifier
- int version = LittleEndian.getUShort(rawBytes, offset); offset += LittleEndian.SHORT_SIZE;
- int reserved = LittleEndian.getUShort(rawBytes, offset); offset += LittleEndian.SHORT_SIZE;
- offset += LittleEndian.INT_SIZE; //checksum
- offset += LittleEndian.INT_SIZE; //flags
- long winMetafileSizeLong = LittleEndian.getUInt(rawBytes, offset); offset += LittleEndian.INT_SIZE;
+ int offset = LittleEndianConsts.INT_SIZE;//public comment identifier
+ int version = LittleEndian.getUShort(rawBytes, offset); offset += LittleEndianConsts.SHORT_SIZE;
+ int reserved = LittleEndian.getUShort(rawBytes, offset); offset += LittleEndianConsts.SHORT_SIZE;
+ offset += LittleEndianConsts.INT_SIZE; //checksum
+ offset += LittleEndianConsts.INT_SIZE; //flags
+ long winMetafileSizeLong = LittleEndian.getUInt(rawBytes, offset); offset += LittleEndianConsts.INT_SIZE;
if (winMetafileSizeLong == 0L) {
wmfBytes = new byte[0];
return;