
com.poiji.bind.ToExcel Maven / Gradle / Ivy
package com.poiji.bind;
import com.poiji.exception.PoijiExcelType;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import com.poiji.save.FileSaver;
import com.poiji.save.FileSaverFactory;
import java.io.File;
import java.io.OutputStream;
import java.util.Collection;
import java.util.stream.Stream;
public class ToExcel {
private Source source;
private Destination destination;
private Class javaType;
private PoijiOptions options;
public void save() {
validate();
final FileSaverFactory factory = new FileSaverFactory<>(javaType, options);
final FileSaver fileSaver = destination.getFileSaver(factory);
source.save(fileSaver);
}
private void validate() {
if (javaType == null) {
throw new PoijiException("Java type must be installed");
}
if (source == null) {
throw new PoijiException("Source must be installed");
}
if (destination == null) {
throw new PoijiException("Destination must be installed");
}
if (options == null){
options = PoijiOptions.PoijiOptionsBuilder.settings().build();
}
}
public ToExcel withSource(final Stream stream) {
this.source = new StreamSource<>(stream);
return this;
}
public ToExcel withSource(final Collection collection) {
this.source = new CollectionSource<>(collection);
return this;
}
public ToExcel withJavaType(final Class javaType) {
this.javaType = javaType;
return this;
}
public ToExcel withOptions(final PoijiOptions options) {
this.options = options;
return this;
}
public ToExcel withDestination(final File file) {
this.destination = new FileDestination(file);
return this;
}
public ToExcel withDestination(final OutputStream outputStream, final PoijiExcelType excelType) {
this.destination = new OutputStreamDestination(outputStream, excelType);
return this;
}
private interface Destination {
FileSaver getFileSaver(final FileSaverFactory factory);
}
private static class FileDestination implements Destination{
private final File file;
private final PoijiExcelType excelType;
public FileDestination(final File file) {
this.file = file;
this.excelType = PoijiExcelType.fromFileName(file.getName());
}
@Override
public FileSaver getFileSaver(final FileSaverFactory factory) {
return factory.toFile(file, excelType);
}
}
private static class OutputStreamDestination implements Destination{
private final OutputStream outputStream;
private final PoijiExcelType excelType;
public OutputStreamDestination(final OutputStream outputStream, final PoijiExcelType excelType) {
this.outputStream = outputStream;
this.excelType = excelType;
}
@Override
public FileSaver getFileSaver(final FileSaverFactory factory) {
return factory.toOutputStream(outputStream, excelType);
}
}
private interface Source {
void save(final FileSaver fileSaver);
}
private static class CollectionSource implements Source {
private final Collection collection;
public CollectionSource(final Collection collection) {
this.collection = collection;
}
@Override
public void save(final FileSaver fileSaver) {
fileSaver.save(collection);
}
}
private static class StreamSource implements Source {
private final Stream stream;
public StreamSource(final Stream stream) {
this.stream = stream;
}
@Override
public void save(final FileSaver fileSaver) {
fileSaver.save(stream);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy