Browse Source

optimize FillConfig initialization

#3318-FillConfig
Tiger Wang 2 years ago committed by Tiger Wang
parent
commit
99d03a98ca
  1. 2
      easyexcel-core/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  2. 28
      easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java
  3. 49
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillConfigTest.java

2
easyexcel-core/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java

@ -98,7 +98,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
if (fillConfig == null) { if (fillConfig == null) {
fillConfig = FillConfig.builder().build(); fillConfig = FillConfig.builder().build();
} }
fillConfig.init(); // fillConfig.init();
Object realData; Object realData;
// The data prefix that is populated this time // The data prefix that is populated this time

28
easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java

@ -21,37 +21,25 @@ import lombok.Setter;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class FillConfig { public class FillConfig {
private WriteDirectionEnum direction;
@Builder.Default
private WriteDirectionEnum direction = WriteDirectionEnum.VERTICAL;
/** /**
* Create a new row each time you use the list parameter.The default create if necessary. * Create a new row each time you use the list parameter.The default create if necessary.
* <p> * <p>
* Warnning:If you use <code>forceNewRow</code> set true, will not be able to use asynchronous write file, simply * Warnning:If you use <code>forceNewRow</code> set true, will not be able to use asynchronous write file, simply
* say the whole file will be stored in memory. * say the whole file will be stored in memory.
*/ */
private Boolean forceNewRow; @Builder.Default
private Boolean forceNewRow = Boolean.FALSE;
/** /**
* Automatically inherit style * Automatically inherit style
* *
* default true. * default true.
*/ */
private Boolean autoStyle; @Builder.Default
private Boolean autoStyle = Boolean.TRUE;
private boolean hasInit;
public void init() {
if (hasInit) {
return;
}
if (direction == null) {
direction = WriteDirectionEnum.VERTICAL;
}
if (forceNewRow == null) {
forceNewRow = Boolean.FALSE;
}
if (autoStyle == null) {
autoStyle = Boolean.TRUE;
}
hasInit = true;
}
} }

49
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillConfigTest.java

@ -0,0 +1,49 @@
package com.alibaba.easyexcel.test.core.fill;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.fill.FillConfig;;
public class FillConfigTest {
@Test
void creationByNewTest() {
// Non-arguments constructor
FillConfig config = new FillConfig();
assertEquals(config.getDirection(), WriteDirectionEnum.VERTICAL);
assertFalse(config.getForceNewRow());
assertTrue(config.getAutoStyle());
// Full-arguments constructor
config = new FillConfig(WriteDirectionEnum.HORIZONTAL, Boolean.TRUE, Boolean.FALSE);
assertEquals(config.getDirection(), WriteDirectionEnum.HORIZONTAL);
assertTrue(config.getForceNewRow());
assertFalse(config.getAutoStyle());
}
@Test
void creationByBuilderTest() {
// default builder
FillConfig config = FillConfig.builder().build();
assertEquals(config.getDirection(), WriteDirectionEnum.VERTICAL);
assertFalse(config.getForceNewRow());
assertTrue(config.getAutoStyle());
// builder with some actions
config = FillConfig.builder()
.direction(WriteDirectionEnum.HORIZONTAL)
.autoStyle(Boolean.FALSE)
.forceNewRow(Boolean.TRUE)
.build();
assertEquals(config.getDirection(), WriteDirectionEnum.HORIZONTAL);
assertTrue(config.getForceNewRow());
assertFalse(config.getAutoStyle());
}
}
Loading…
Cancel
Save