br.com.objectos.xls.WorksheetRowWriter.java Source code

Java tutorial

Introduction

Here is the source code for br.com.objectos.xls.WorksheetRowWriter.java

Source

/*
 * Copyright 2014 Objectos, Fbrica 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.xls;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;

/**
 * @author marcio.endo@objectos.com.br (Marcio Endo)
 */
public class WorksheetRowWriter implements WorksheetRowWriterStyle {

    private final WorksheetRow row;
    private final List<CanStyle> canStyleList = new ArrayList<>();

    boolean blank;

    WorksheetRowWriter(WorksheetRow row) {
        this.row = row;
    }

    @Override
    public WorksheetRowWriterBoolean add(boolean trueOrFalse) {
        writeThis();
        return new WorksheetRowWriterBoolean(row, trueOrFalse);
    }

    @Override
    public WorksheetRowWriterNumber add(double value) {
        writeThis();
        return new WorksheetRowWriterNumber(row, value);
    }

    @Override
    public WorksheetRowWriterXlsSerializable add(IsXlsSerializable xls) {
        writeThis();
        return new WorksheetRowWriterXlsSerializable(row, xls);
    }

    @Override
    public WorksheetRowWriterDate add(LocalDate date) {
        writeThis();
        return new WorksheetRowWriterDate(row, date);
    }

    @Override
    public WorksheetRowWriterText add(String text) {
        writeThis();
        return new WorksheetRowWriterText(row, text);
    }

    @Override
    public WorksheetRowWriter addBlank() {
        writeThis();
        return new WorksheetRowWriterBlank(row);
    }

    @Override
    public final WorksheetRow write() {
        writeThis();
        return row();
    }

    @Override
    public WorksheetRowWriter with(Styles.Builder styles) {
        styles.addAll(canStyleList);
        return this;
    }

    void setFormatString(String formatString) {
        Objects.requireNonNull(formatString);
        PoiFormatString format = new PoiFormatString(formatString);
        canStyleList.add(format);
    }

    <T> T checkDefaultValueNotNull(T defaultValue) {
        return Objects.requireNonNull(defaultValue, "defaultValue cannot be null");
    }

    Cell nextCell() {
        return row.nextCell();
    }

    WorksheetRow row() {
        return row;
    }

    Worksheet worksheet() {
        return row.worksheet();
    }

    void write(Cell cell) {
    }

    void writeThis() {
        Cell cell = nextCell();
        if (cell == null) {
            return;
        }

        if (blank) {
            writeBlank(cell);
        } else {
            writeCell(cell);
        }

        Optional<CellStyle> maybeStyle = row.getCellStyle(canStyleList);
        if (maybeStyle.isPresent()) {
            CellStyle cellStyle = maybeStyle.get();
            cell.setCellStyle(cellStyle);
        }
    }

    private void writeBlank(Cell cell) {
        cell.setCellType(Cell.CELL_TYPE_BLANK);
    }

    private void writeCell(Cell cell) {
        write(cell);
    }

}