
br.com.objectos.xls.pojo.plugin.SpreadsheetPlugin Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2014-2015 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.xls.pojo.plugin;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.lang.model.element.Modifier;
import br.com.objectos.io.Directory;
import br.com.objectos.metainf.Services;
import br.com.objectos.pojo.plugin.AbstractPlugin;
import br.com.objectos.pojo.plugin.Contribution;
import br.com.objectos.pojo.plugin.Plugin;
import br.com.objectos.pojo.plugin.PojoAction;
import br.com.objectos.pojo.plugin.PojoInfo;
import br.com.objectos.xls.pojo.Spreadsheet;
import com.squareup.javapoet.ArrayTypeName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;
/**
* @author [email protected] (Marcio Endo)
*/
@Services(Plugin.class)
public class SpreadsheetPlugin extends AbstractPlugin implements PojoAction {
private static final String NEW_SPREADSHEET = "___new_spreadsheet___";
@Override
protected void configure() {
executeWhen(pojo(instanceOf(Spreadsheet.class)));
when(property(instanceOf(byte[].class))).and(hasName("write")).ignore();
when(property(instanceOf(byte[].class))).and(hasName("writeUnchecked")).ignore();
execute(this);
}
@Override
public Contribution execute(PojoInfo pojoInfo) {
List methodList = methodList(pojoInfo);
Contribution.Builder builder = Contribution.builder()
.addMethod(isWritableTo0())
.addMethod(isWritableTo1())
.addMethod(isWritableTo2())
.addMethod(isWritableTo3())
.addMethod(isWritableTo4())
.addMethod(isWritableTo5())
.addMethod(newSpreadsheet(methodList));
for (SpreadsheetMethod method : methodList) {
method.addTo(builder);
}
return builder.build();
}
private MethodSpec isWritableTo0() {
return MethodSpec.methodBuilder("write")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.returns(ArrayTypeName.of(TypeName.BYTE))
.addException(IOException.class)
.addStatement("return $L().write()", NEW_SPREADSHEET)
.build();
}
private MethodSpec isWritableTo1() {
return MethodSpec.methodBuilder("writeTo")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(Directory.class, "dir")
.addParameter(String.class, "name")
.addException(IOException.class)
.addStatement("$L().writeTo(dir, name)", NEW_SPREADSHEET)
.build();
}
private MethodSpec isWritableTo2() {
return MethodSpec.methodBuilder("writeTo")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(File.class, "file")
.addException(IOException.class)
.addStatement("$L().writeTo(file)", NEW_SPREADSHEET)
.build();
}
private MethodSpec isWritableTo3() {
return MethodSpec.methodBuilder("writeUnchecked")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.returns(ArrayTypeName.of(TypeName.BYTE))
.addStatement("return $L().writeUnchecked()", NEW_SPREADSHEET)
.build();
}
private MethodSpec isWritableTo4() {
return MethodSpec.methodBuilder("writeUncheckedTo")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(Directory.class, "dir")
.addParameter(String.class, "name")
.addStatement("$L().writeUncheckedTo(dir, name)", NEW_SPREADSHEET)
.build();
}
private MethodSpec isWritableTo5() {
return MethodSpec.methodBuilder("writeUncheckedTo")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(File.class, "file")
.addStatement("$L().writeUncheckedTo(file)", NEW_SPREADSHEET)
.build();
}
private List methodList(PojoInfo pojoInfo) {
return pojoInfo.propertyStream()
.map(SpreadsheetMethod::of)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
}
private MethodSpec newSpreadsheet(List methodList) {
MethodSpec.Builder newSpreadsheet = MethodSpec.methodBuilder(NEW_SPREADSHEET)
.addModifiers(Modifier.PRIVATE)
.returns(br.com.objectos.xls.Spreadsheet.class)
.addStatement("$1T spreadsheet = $1T.newSpreadsheet()", br.com.objectos.xls.Spreadsheet.class);
for (SpreadsheetMethod method : methodList) {
method.newSpreadsheetStatement(newSpreadsheet);
}
return newSpreadsheet
.addStatement("return spreadsheet")
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy