1
1
/*
2
- * Copyright 2016-2023 the original author or authors.
2
+ * Copyright 2016-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
25
25
import java .util .List ;
26
26
import java .util .Map ;
27
27
import java .util .Set ;
28
+ import java .util .function .Consumer ;
28
29
29
30
import org .apache .commons .logging .Log ;
30
31
import org .apache .commons .logging .LogFactory ;
58
59
* @author Glenn Renfro
59
60
* @author Mahmoud Ben Hassine
60
61
* @author Drummond Dawson
62
+ * @author Daeho Kwon
61
63
* @since 4.0
62
64
* @see FlatFileItemReader
63
65
*/
@@ -87,9 +89,9 @@ public class FlatFileItemReaderBuilder<T> {
87
89
88
90
private LineTokenizer lineTokenizer ;
89
91
90
- private DelimitedBuilder <T > delimitedBuilder ;
92
+ private DelimitedSpec <T > delimitedSpec ;
91
93
92
- private FixedLengthBuilder <T > fixedLengthBuilder ;
94
+ private FixedLengthSpec <T > fixedLengthSpec ;
93
95
94
96
private Class <T > targetType ;
95
97
@@ -304,30 +306,56 @@ public FlatFileItemReaderBuilder<T> lineTokenizer(LineTokenizer tokenizer) {
304
306
}
305
307
306
308
/**
307
- * Returns an instance of a {@link DelimitedBuilder } for building a
309
+ * Returns an instance of a {@link DelimitedSpec } for building a
308
310
* {@link DelimitedLineTokenizer}. The {@link DelimitedLineTokenizer} configured by
309
311
* this builder will only be used if one is not explicitly configured via
310
312
* {@link FlatFileItemReaderBuilder#lineTokenizer}
311
- * @return a {@link DelimitedBuilder }
313
+ * @return a {@link DelimitedSpec }
312
314
*
313
315
*/
314
- public DelimitedBuilder <T > delimited () {
315
- this .delimitedBuilder = new DelimitedBuilder <>(this );
316
- updateTokenizerValidation (this .delimitedBuilder , 1 );
317
- return this .delimitedBuilder ;
316
+ public DelimitedSpec <T > delimited () {
317
+ this .delimitedSpec = new DelimitedSpec <>(this );
318
+ updateTokenizerValidation (this .delimitedSpec , 1 );
319
+ return this .delimitedSpec ;
318
320
}
319
321
320
322
/**
321
- * Returns an instance of a {@link FixedLengthBuilder} for building a
323
+ * Configure a {@link DelimitedSpec} using a lambda.
324
+ * @param consumer the spec to configure
325
+ * @return the current builder instance
326
+ */
327
+ public FlatFileItemReaderBuilder <T > delimited (Consumer <DelimitedSpec <T >> consumer ) {
328
+ DelimitedSpec <T > builder = new DelimitedSpec <>(this );
329
+ consumer .accept (builder );
330
+ this .delimitedSpec = builder ;
331
+ updateTokenizerValidation (this .delimitedSpec , 1 );
332
+ return this ;
333
+ }
334
+
335
+ /**
336
+ * Returns an instance of a {@link FixedLengthSpec} for building a
322
337
* {@link FixedLengthTokenizer}. The {@link FixedLengthTokenizer} configured by this
323
338
* builder will only be used if the {@link FlatFileItemReaderBuilder#lineTokenizer}
324
339
* has not been configured.
325
- * @return a {@link FixedLengthBuilder }
340
+ * @return a {@link FixedLengthSpec }
326
341
*/
327
- public FixedLengthBuilder <T > fixedLength () {
328
- this .fixedLengthBuilder = new FixedLengthBuilder <>(this );
329
- updateTokenizerValidation (this .fixedLengthBuilder , 2 );
330
- return this .fixedLengthBuilder ;
342
+ public FixedLengthSpec <T > fixedLength () {
343
+ this .fixedLengthSpec = new FixedLengthSpec <>(this );
344
+ updateTokenizerValidation (this .fixedLengthSpec , 2 );
345
+ return this .fixedLengthSpec ;
346
+ }
347
+
348
+ /**
349
+ * Configure a {@link FixedLengthSpec} using a lambda.
350
+ * @param consumer the spec to configure
351
+ * @return the current builder instance
352
+ */
353
+ public FlatFileItemReaderBuilder <T > fixedLength (Consumer <FixedLengthSpec <T >> consumer ) {
354
+ FixedLengthSpec <T > builder = new FixedLengthSpec <>(this );
355
+ consumer .accept (builder );
356
+ this .fixedLengthSpec = builder ;
357
+ updateTokenizerValidation (this .fixedLengthSpec , 2 );
358
+ return this ;
331
359
}
332
360
333
361
/**
@@ -449,11 +477,11 @@ public FlatFileItemReader<T> build() {
449
477
if (this .lineTokenizer != null ) {
450
478
lineMapper .setLineTokenizer (this .lineTokenizer );
451
479
}
452
- else if (this .fixedLengthBuilder != null ) {
453
- lineMapper .setLineTokenizer (this .fixedLengthBuilder .build ());
480
+ else if (this .fixedLengthSpec != null ) {
481
+ lineMapper .setLineTokenizer (this .fixedLengthSpec .build ());
454
482
}
455
- else if (this .delimitedBuilder != null ) {
456
- lineMapper .setLineTokenizer (this .delimitedBuilder .build ());
483
+ else if (this .delimitedSpec != null ) {
484
+ lineMapper .setLineTokenizer (this .delimitedSpec .build ());
457
485
}
458
486
else {
459
487
throw new IllegalStateException ("No LineTokenizer implementation was provided." );
@@ -519,7 +547,7 @@ private void updateTokenizerValidation(Object tokenizer, int index) {
519
547
*
520
548
* @param <T> the type of the parent {@link FlatFileItemReaderBuilder}
521
549
*/
522
- public static class DelimitedBuilder <T > {
550
+ public static class DelimitedSpec <T > {
523
551
524
552
private final FlatFileItemReaderBuilder <T > parent ;
525
553
@@ -535,7 +563,7 @@ public static class DelimitedBuilder<T> {
535
563
536
564
private boolean strict = true ;
537
565
538
- protected DelimitedBuilder (FlatFileItemReaderBuilder <T > parent ) {
566
+ protected DelimitedSpec (FlatFileItemReaderBuilder <T > parent ) {
539
567
this .parent = parent ;
540
568
}
541
569
@@ -545,7 +573,7 @@ protected DelimitedBuilder(FlatFileItemReaderBuilder<T> parent) {
545
573
* @return The instance of the builder for chaining.
546
574
* @see DelimitedLineTokenizer#setDelimiter(String)
547
575
*/
548
- public DelimitedBuilder <T > delimiter (String delimiter ) {
576
+ public DelimitedSpec <T > delimiter (String delimiter ) {
549
577
this .delimiter = delimiter ;
550
578
return this ;
551
579
}
@@ -556,7 +584,7 @@ public DelimitedBuilder<T> delimiter(String delimiter) {
556
584
* @return The instance of the builder for chaining.
557
585
* @see DelimitedLineTokenizer#setQuoteCharacter(char)
558
586
*/
559
- public DelimitedBuilder <T > quoteCharacter (char quoteCharacter ) {
587
+ public DelimitedSpec <T > quoteCharacter (char quoteCharacter ) {
560
588
this .quoteCharacter = quoteCharacter ;
561
589
return this ;
562
590
}
@@ -567,7 +595,7 @@ public DelimitedBuilder<T> quoteCharacter(char quoteCharacter) {
567
595
* @return The instance of the builder for chaining.
568
596
* @see DelimitedLineTokenizer#setIncludedFields(int[])
569
597
*/
570
- public DelimitedBuilder <T > includedFields (Integer ... fields ) {
598
+ public DelimitedSpec <T > includedFields (Integer ... fields ) {
571
599
this .includedFields .addAll (Arrays .asList (fields ));
572
600
return this ;
573
601
}
@@ -578,7 +606,7 @@ public DelimitedBuilder<T> includedFields(Integer... fields) {
578
606
* @return The instance of the builder for chaining.
579
607
* @see DelimitedLineTokenizer#setIncludedFields(int[])
580
608
*/
581
- public DelimitedBuilder <T > addIncludedField (int field ) {
609
+ public DelimitedSpec <T > addIncludedField (int field ) {
582
610
this .includedFields .add (field );
583
611
return this ;
584
612
}
@@ -592,7 +620,7 @@ public DelimitedBuilder<T> addIncludedField(int field) {
592
620
* @return The instance of the builder for chaining.
593
621
* @see DelimitedLineTokenizer#setFieldSetFactory(FieldSetFactory)
594
622
*/
595
- public DelimitedBuilder <T > fieldSetFactory (FieldSetFactory fieldSetFactory ) {
623
+ public DelimitedSpec <T > fieldSetFactory (FieldSetFactory fieldSetFactory ) {
596
624
this .fieldSetFactory = fieldSetFactory ;
597
625
return this ;
598
626
}
@@ -618,7 +646,7 @@ public FlatFileItemReaderBuilder<T> names(String... names) {
618
646
* @since 5.1
619
647
* @param strict the strict flag to set
620
648
*/
621
- public DelimitedBuilder <T > strict (boolean strict ) {
649
+ public DelimitedSpec <T > strict (boolean strict ) {
622
650
this .strict = strict ;
623
651
return this ;
624
652
}
@@ -677,7 +705,7 @@ public DelimitedLineTokenizer build() {
677
705
*
678
706
* @param <T> the type of the parent {@link FlatFileItemReaderBuilder}
679
707
*/
680
- public static class FixedLengthBuilder <T > {
708
+ public static class FixedLengthSpec <T > {
681
709
682
710
private final FlatFileItemReaderBuilder <T > parent ;
683
711
@@ -689,7 +717,7 @@ public static class FixedLengthBuilder<T> {
689
717
690
718
private FieldSetFactory fieldSetFactory = new DefaultFieldSetFactory ();
691
719
692
- protected FixedLengthBuilder (FlatFileItemReaderBuilder <T > parent ) {
720
+ protected FixedLengthSpec (FlatFileItemReaderBuilder <T > parent ) {
693
721
this .parent = parent ;
694
722
}
695
723
@@ -699,7 +727,7 @@ protected FixedLengthBuilder(FlatFileItemReaderBuilder<T> parent) {
699
727
* @return This instance for chaining
700
728
* @see FixedLengthTokenizer#setColumns(Range[])
701
729
*/
702
- public FixedLengthBuilder <T > columns (Range ... ranges ) {
730
+ public FixedLengthSpec <T > columns (Range ... ranges ) {
703
731
this .ranges .addAll (Arrays .asList (ranges ));
704
732
return this ;
705
733
}
@@ -710,7 +738,7 @@ public FixedLengthBuilder<T> columns(Range... ranges) {
710
738
* @return This instance for chaining
711
739
* @see FixedLengthTokenizer#setColumns(Range[])
712
740
*/
713
- public FixedLengthBuilder <T > addColumns (Range range ) {
741
+ public FixedLengthSpec <T > addColumns (Range range ) {
714
742
this .ranges .add (range );
715
743
return this ;
716
744
}
@@ -722,7 +750,7 @@ public FixedLengthBuilder<T> addColumns(Range range) {
722
750
* @return This instance for chaining
723
751
* @see FixedLengthTokenizer#setColumns(Range[])
724
752
*/
725
- public FixedLengthBuilder <T > addColumns (Range range , int index ) {
753
+ public FixedLengthSpec <T > addColumns (Range range , int index ) {
726
754
this .ranges .add (index , range );
727
755
return this ;
728
756
}
@@ -745,7 +773,7 @@ public FlatFileItemReaderBuilder<T> names(String... names) {
745
773
* @return This instance for chaining
746
774
* @see FixedLengthTokenizer#setStrict(boolean)
747
775
*/
748
- public FixedLengthBuilder <T > strict (boolean strict ) {
776
+ public FixedLengthSpec <T > strict (boolean strict ) {
749
777
this .strict = strict ;
750
778
return this ;
751
779
}
@@ -759,7 +787,7 @@ public FixedLengthBuilder<T> strict(boolean strict) {
759
787
* @return The instance of the builder for chaining.
760
788
* @see FixedLengthTokenizer#setFieldSetFactory(FieldSetFactory)
761
789
*/
762
- public FixedLengthBuilder <T > fieldSetFactory (FieldSetFactory fieldSetFactory ) {
790
+ public FixedLengthSpec <T > fieldSetFactory (FieldSetFactory fieldSetFactory ) {
763
791
this .fieldSetFactory = fieldSetFactory ;
764
792
return this ;
765
793
}
0 commit comments