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

Java tutorial

Introduction

Here is the source code for br.com.objectos.xls.Spreadsheet.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.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import br.com.objectos.io.Directory;
import br.com.objectos.io.IsWritableTo;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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

    private final Map<StyleKey, CellStyle> styleMap = new HashMap<>();
    private final Object styleLock = new Object();

    private final Workbook workbook;

    private Spreadsheet(Workbook workbook) {
        this.workbook = workbook;
    }

    public static Spreadsheet newSpreadsheet() {
        Workbook workbook = new XSSFWorkbook();
        return new Spreadsheet(workbook);
    }

    public static Spreadsheet newSpreadsheet(Workbook workbook) {
        return new Spreadsheet(workbook);
    }

    public Worksheet newWorksheet() {
        Sheet sheet = workbook.createSheet();
        return new Worksheet(this, sheet);
    }

    public Worksheet newWorksheet(String name) {
        Sheet sheet = workbook.createSheet(name);
        return new Worksheet(this, sheet);
    }

    @Override
    public byte[] write() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        workbook.write(baos);
        return baos.toByteArray();
    }

    @Override
    public void writeTo(Directory dir, String name) throws IOException {
        br.com.objectos.io.File file = dir.fileAt(name);
        writeTo(file.toFile());
    }

    @Override
    public void writeTo(File file) throws IOException {
        FileOutputStream stream = new FileOutputStream(file);
        workbook.write(stream);
    }

    @Override
    public byte[] writeUnchecked() {
        try {
            return write();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public void writeUncheckedTo(Directory dir, String name) {
        try {
            writeTo(dir, name);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public void writeUncheckedTo(File file) {
        try {
            writeTo(file);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    Optional<CellStyle> getCellStyle(List<CanStyle> canStyleList) {
        CellStyle maybeStyle = null;

        if (!canStyleList.isEmpty()) {
            StyleKey key = new StyleKey(canStyleList);
            maybeStyle = styleMap.get(key);

            if (maybeStyle == null) {
                synchronized (styleLock) {
                    maybeStyle = styleMap.get(key);
                    if (maybeStyle == null) {
                        maybeStyle = workbook.createCellStyle();
                        styleMap.put(key, maybeStyle);

                        DataFormat dataFormat = workbook.createDataFormat();

                        Font font = workbook.createFont();
                        font.setFontName("Arial");
                        font.setFontHeightInPoints((short) 10);
                        maybeStyle.setFont(font);

                        for (CanStyle style : canStyleList) {
                            style.applyTo(maybeStyle, dataFormat, font);
                        }
                    }
                }
            }
        }

        return Optional.ofNullable(maybeStyle);
    }

    private static class StyleKey {

        private final List<CanStyle> list;

        public StyleKey(List<CanStyle> list) {
            this.list = list;
        }

        @Override
        public int hashCode() {
            return Objects.hash(list);
        }

        @Override
        public boolean equals(Object obj) {
            StyleKey that = (StyleKey) obj;
            return Objects.equals(list, that.list);
        }

    }

}