<commit_before>/*
 * Copyright 2007-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.ymate.platform.commons;

import net.ymate.platform.commons.lang.BlurObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * SHEET页分析处理器接口
 *
 * @param <T> 元素类型
 * @author 刘镇 (<EMAIL>) on 2018/5/25 上午6:37
 */
public interface ISheetHandler<T> {

    class Default implements ISheetHandler<Object[]> {

        private CellMeta[] cellMetas;

        @Override
        public List<Object[]> handle(Sheet sheet) throws Exception {
            List<Object[]> results = new ArrayList<>();
            for (int rowIdx = sheet.getFirstRowNum(); rowIdx <= sheet.getLastRowNum(); rowIdx++) {
                Row sheetRow = sheet.getRow(rowIdx);
                if (rowIdx == sheet.getFirstRowNum()) {
                    List<CellMeta> metaList = new ArrayList<>();
                    for (short cellIdx = sheetRow.getFirstCellNum(); cellIdx <= sheetRow.getLastCellNum(); cellIdx++) {
                        Object cellValue = parseCell(sheetRow.getCell(cellIdx));
                        if (cellValue != null) {
                            metaList.add(new CellMeta(BlurObject.bind(cellValue).toStringValue(), cellIdx));
                        }
                    }
                    cellMetas = metaList.toArray(new CellMeta[0]);
                } else {
                    results.add(parseRow(sheetRow));
                }
            }
            return results;
        }

        @Override
        public CellMeta[] getCellMetas() {
            return cellMetas;
        }

        @Override
        public Object[] parseRow(Row row) throws Exception {
            Object[] result = new Object[cellMetas.length];
            for (int idx = 0; idx < cellMetas.length; idx++) {
                CellMeta cellMeta = cellMetas[idx];
                result[idx] = new Object[]{cellMeta.getName(), parseCell(row.getCell(cellMeta.getCellIndex()))};
            }
            return result;
        }

        private Object parseCell(Cell cell) throws Exception {
            Object value = null;
            if (cell != null) {
                switch (cell.getCellType()) {
                    case STRING:
                        value = cell.getStringCellValue();
                        break;
                    case NUMERIC:
                        if (HSSFDateUtil.isCellDateFormatted(cell) && cell.getDateCellValue() != null) {
                            value = cell.getDateCellValue().getTime();
                        } else {
                            value = new DecimalFormat("0").format(cell.getNumericCellValue());
                        }
                        break;
                    case FORMULA:
                        value = cell.getCellFormula();
                        break;
                    case BOOLEAN:
                        value = cell.getBooleanCellValue();
                        break;
                    case BLANK:<commit_msg>优化代码去掉编译警告<commit_after>/*
 * Copyright 2007-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.ymate.platform.commons;

import net.ymate.platform.commons.lang.BlurObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * SHEET页分析处理器接口
 *
 * @param <T> 元素类型
 * @author 刘镇 (<EMAIL>) on 2018/5/25 上午6:37
 */
public interface ISheetHandler<T> {

    class Default implements ISheetHandler<Object[]> {

        private CellMeta[] cellMetas;

        @Override
        public List<Object[]> handle(Sheet sheet) throws Exception {
            List<Object[]> results = new ArrayList<>();
            for (int rowIdx = sheet.getFirstRowNum(); rowIdx <= sheet.getLastRowNum(); rowIdx++) {
                Row sheetRow = sheet.getRow(rowIdx);
                if (rowIdx == sheet.getFirstRowNum()) {
                    List<CellMeta> metaList = new ArrayList<>();
                    for (short cellIdx = sheetRow.getFirstCellNum(); cellIdx <= sheetRow.getLastCellNum(); cellIdx++) {
                        Object cellValue = parseCell(sheetRow.getCell(cellIdx));
                        if (cellValue != null) {
                            metaList.add(new CellMeta(BlurObject.bind(cellValue).toStringValue(), cellIdx));
                        }
                    }
                    cellMetas = metaList.toArray(new CellMeta[0]);
                } else {
                    results.add(parseRow(sheetRow));
                }
            }
            return results;
        }

        @Override
        public CellMeta[] getCellMetas() {
            return cellMetas;
        }

        @Override
        public Object[] parseRow(Row row) throws Exception {
            Object[] result = new Object[cellMetas.length];
            for (int idx = 0; idx < cellMetas.length; idx++) {
                CellMeta cellMeta = cellMetas[idx];
                result[idx] = new Object[]{cellMeta.getName(), parseCell(row.getCell(cellMeta.getCellIndex()))};
            }
            return result;
        }

        private Object parseCell(Cell cell) throws Exception {
            Object value = null;
            if (cell != null) {
                switch (cell.getCellType()) {
                    case STRING:
                        value = cell.getStringCellValue();
                        break;
                    case NUMERIC:
                        if (DateUtil.isCellDateFormatted(cell) && cell.getDateCellValue() != null) {
                            value = cell.getDateCellValue().getTime();
                        } else {
                            value = new DecimalFormat("0").format(cell.getNumericCellValue());
                        }
                        break;
                    case FORMULA:
                        value = cell.getCellFormula();
                        break;
                    case BOOLEAN:
                        value = cell.getBooleanCellValue();
                        break;
                    case BLANK:
{'fraction_non_alphanumeric': 0.09935262253930506, 'fraction_numerical': 0.0068701281543136474, 'mean_word_length': 1.751726644856416, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '143493', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1939, 'n_words': 507}