All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.comuns.io.xls.PoiCell Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.comuns.io.xls;

import java.util.Date;

import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.usermodel.Cell;

import com.google.common.base.Strings;

/**
 * @author [email protected] (Marcio Endo)
 */
class PoiCell implements CellWrapper {

  private final Cell cell;

  public PoiCell(Cell cell) {
    this.cell = cell;
  }

  @Override
  public boolean isNullOrEmpty() {
    return cell.getCellType() == Cell.CELL_TYPE_BLANK;
  }

  @Override
  public boolean getBoolean() {
    return cell.getBooleanCellValue();
  }

  @Override
  public Date getDate() {
    return cell.getDateCellValue();
  }

  @Override
  public double getNumber() {
    return cell.getNumericCellValue();
  }

  @Override
  public String getText() {
    int cellType = cell.getCellType();
    switch (cellType) {
    case Cell.CELL_TYPE_BLANK:
      return "";

    case Cell.CELL_TYPE_NUMERIC:
      double numValue = cell.getNumericCellValue();
      return doubleToString(numValue);

    default:
    case Cell.CELL_TYPE_STRING:
      String text = cell.getStringCellValue();
      return Strings.nullToEmpty(text).trim();
    }
  }

  @Override
  public String toString() {
    String letter = CellReference.convertNumToColString(cell.getColumnIndex());
    String text = getText();
    return String.format("column %s: '%s'", letter, text);
  }

  private String doubleToString(double numValue) {
    if ((numValue == Math.floor(numValue)) && !Double.isInfinite(numValue)) {
      return Integer.toString((int) numValue);
    } else {
      return Double.toString(numValue);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy