Skip to content

Commit 7b98850

Browse files
committed
Internal refactoring
1 parent 5835ba2 commit 7b98850

File tree

1 file changed

+72
-55
lines changed

1 file changed

+72
-55
lines changed

src/main/java/org/apache/commons/io/input/Tailer.java

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public Builder() {
216216
*/
217217
@Override
218218
public Tailer get() {
219-
final Tailer tailer = new Tailer(tailable, getCharset(), tailerListener, delayDuration, tailFromEnd, reOpen, getBufferSize(), ignoreTouch);
219+
final Tailer tailer = new Tailer(this);
220220
if (startThread) {
221221
executorService.submit(tailer);
222222
}
@@ -514,7 +514,7 @@ public static Builder builder() {
514514
*
515515
* @param file the file to follow.
516516
* @param charset the character set to use for reading the file.
517-
* @param listener the TailerListener to use.
517+
* @param tailerListener the TailerListener to use.
518518
* @param delayMillis the delay between checks of the file for new content in milliseconds.
519519
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
520520
* @param reOpen whether to close/reopen the file between chunks.
@@ -523,12 +523,12 @@ public static Builder builder() {
523523
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
524524
*/
525525
@Deprecated
526-
public static Tailer create(final File file, final Charset charset, final TailerListener listener, final long delayMillis, final boolean end,
527-
final boolean reOpen, final int bufferSize) {
526+
public static Tailer create(final File file, final Charset charset, final TailerListener tailerListener, final long delayMillis, final boolean end,
527+
final boolean reOpen, final int bufferSize) {
528528
//@formatter:off
529529
return builder()
530530
.setFile(file)
531-
.setTailerListener(listener)
531+
.setTailerListener(tailerListener)
532532
.setCharset(charset)
533533
.setDelayDuration(Duration.ofMillis(delayMillis))
534534
.setTailFromEnd(end)
@@ -542,16 +542,16 @@ public static Tailer create(final File file, final Charset charset, final Tailer
542542
* Creates and starts a Tailer for the given file, starting at the beginning of the file with the default delay of 1.0s
543543
*
544544
* @param file the file to follow.
545-
* @param listener the TailerListener to use.
545+
* @param tailerListener the TailerListener to use.
546546
* @return The new tailer.
547547
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
548548
*/
549549
@Deprecated
550-
public static Tailer create(final File file, final TailerListener listener) {
550+
public static Tailer create(final File file, final TailerListener tailerListener) {
551551
//@formatter:off
552552
return builder()
553553
.setFile(file)
554-
.setTailerListener(listener)
554+
.setTailerListener(tailerListener)
555555
.get();
556556
//@formatter:on
557557
}
@@ -560,17 +560,17 @@ public static Tailer create(final File file, final TailerListener listener) {
560560
* Creates and starts a Tailer for the given file, starting at the beginning of the file
561561
*
562562
* @param file the file to follow.
563-
* @param listener the TailerListener to use.
563+
* @param tailerListener the TailerListener to use.
564564
* @param delayMillis the delay between checks of the file for new content in milliseconds.
565565
* @return The new tailer.
566566
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
567567
*/
568568
@Deprecated
569-
public static Tailer create(final File file, final TailerListener listener, final long delayMillis) {
569+
public static Tailer create(final File file, final TailerListener tailerListener, final long delayMillis) {
570570
//@formatter:off
571571
return builder()
572572
.setFile(file)
573-
.setTailerListener(listener)
573+
.setTailerListener(tailerListener)
574574
.setDelayDuration(Duration.ofMillis(delayMillis))
575575
.get();
576576
//@formatter:on
@@ -580,18 +580,18 @@ public static Tailer create(final File file, final TailerListener listener, fina
580580
* Creates and starts a Tailer for the given file with default buffer size.
581581
*
582582
* @param file the file to follow.
583-
* @param listener the TailerListener to use.
583+
* @param tailerListener the TailerListener to use.
584584
* @param delayMillis the delay between checks of the file for new content in milliseconds.
585585
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
586586
* @return The new tailer.
587587
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
588588
*/
589589
@Deprecated
590-
public static Tailer create(final File file, final TailerListener listener, final long delayMillis, final boolean end) {
590+
public static Tailer create(final File file, final TailerListener tailerListener, final long delayMillis, final boolean end) {
591591
//@formatter:off
592592
return builder()
593593
.setFile(file)
594-
.setTailerListener(listener)
594+
.setTailerListener(tailerListener)
595595
.setDelayDuration(Duration.ofMillis(delayMillis))
596596
.setTailFromEnd(end)
597597
.get();
@@ -602,19 +602,19 @@ public static Tailer create(final File file, final TailerListener listener, fina
602602
* Creates and starts a Tailer for the given file with default buffer size.
603603
*
604604
* @param file the file to follow.
605-
* @param listener the TailerListener to use.
605+
* @param tailerListener the TailerListener to use.
606606
* @param delayMillis the delay between checks of the file for new content in milliseconds.
607607
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
608608
* @param reOpen whether to close/reopen the file between chunks.
609609
* @return The new tailer.
610610
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
611611
*/
612612
@Deprecated
613-
public static Tailer create(final File file, final TailerListener listener, final long delayMillis, final boolean end, final boolean reOpen) {
613+
public static Tailer create(final File file, final TailerListener tailerListener, final long delayMillis, final boolean end, final boolean reOpen) {
614614
//@formatter:off
615615
return builder()
616616
.setFile(file)
617-
.setTailerListener(listener)
617+
.setTailerListener(tailerListener)
618618
.setDelayDuration(Duration.ofMillis(delayMillis))
619619
.setTailFromEnd(end)
620620
.setReOpen(reOpen)
@@ -626,7 +626,7 @@ public static Tailer create(final File file, final TailerListener listener, fina
626626
* Creates and starts a Tailer for the given file.
627627
*
628628
* @param file the file to follow.
629-
* @param listener the TailerListener to use.
629+
* @param tailerListener the TailerListener to use.
630630
* @param delayMillis the delay between checks of the file for new content in milliseconds.
631631
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
632632
* @param reOpen whether to close/reopen the file between chunks.
@@ -635,12 +635,12 @@ public static Tailer create(final File file, final TailerListener listener, fina
635635
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
636636
*/
637637
@Deprecated
638-
public static Tailer create(final File file, final TailerListener listener, final long delayMillis, final boolean end, final boolean reOpen,
638+
public static Tailer create(final File file, final TailerListener tailerListener, final long delayMillis, final boolean end, final boolean reOpen,
639639
final int bufferSize) {
640640
//@formatter:off
641641
return builder()
642642
.setFile(file)
643-
.setTailerListener(listener)
643+
.setTailerListener(tailerListener)
644644
.setDelayDuration(Duration.ofMillis(delayMillis))
645645
.setTailFromEnd(end)
646646
.setReOpen(reOpen)
@@ -653,19 +653,19 @@ public static Tailer create(final File file, final TailerListener listener, fina
653653
* Creates and starts a Tailer for the given file.
654654
*
655655
* @param file the file to follow.
656-
* @param listener the TailerListener to use.
656+
* @param tailerListener the TailerListener to use.
657657
* @param delayMillis the delay between checks of the file for new content in milliseconds.
658658
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
659659
* @param bufferSize buffer size.
660660
* @return The new tailer.
661661
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
662662
*/
663663
@Deprecated
664-
public static Tailer create(final File file, final TailerListener listener, final long delayMillis, final boolean end, final int bufferSize) {
664+
public static Tailer create(final File file, final TailerListener tailerListener, final long delayMillis, final boolean end, final int bufferSize) {
665665
//@formatter:off
666666
return builder()
667667
.setFile(file)
668-
.setTailerListener(listener)
668+
.setTailerListener(tailerListener)
669669
.setDelayDuration(Duration.ofMillis(delayMillis))
670670
.setTailFromEnd(end)
671671
.setBufferSize(bufferSize)
@@ -696,7 +696,7 @@ public static Tailer create(final File file, final TailerListener listener, fina
696696
/**
697697
* Whether to tail from the end or start of file
698698
*/
699-
private final boolean tailAtEnd;
699+
private final boolean tailFromEnd;
700700

701701
/**
702702
* The listener to notify of events when tailing.
@@ -723,131 +723,148 @@ public static Tailer create(final File file, final TailerListener listener, fina
723723
*
724724
* @param file the file to follow.
725725
* @param charset the Charset to be used for reading the file
726-
* @param listener the TailerListener to use.
726+
* @param tailerListener the TailerListener to use.
727727
* @param delayMillis the delay between checks of the file for new content in milliseconds.
728728
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
729729
* @param reOpen if true, close and reopen the file between reading chunks
730730
* @param bufSize Buffer size
731731
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
732732
*/
733733
@Deprecated
734-
public Tailer(final File file, final Charset charset, final TailerListener listener, final long delayMillis, final boolean end, final boolean reOpen,
735-
final int bufSize) {
736-
this(new TailablePath(file.toPath()), charset, listener, Duration.ofMillis(delayMillis), end, reOpen, bufSize, DEFAULT_IGNORE_TOUCH);
734+
public Tailer(final File file, final Charset charset, final TailerListener tailerListener, final long delayMillis, final boolean end, final boolean reOpen,
735+
final int bufSize) {
736+
this(new TailablePath(file.toPath()), charset, tailerListener, Duration.ofMillis(delayMillis), end, reOpen, bufSize, DEFAULT_IGNORE_TOUCH);
737737
}
738738

739739
/**
740740
* Creates a Tailer for the given file, starting from the beginning, with the default delay of 1.0s.
741741
*
742742
* @param file The file to follow.
743-
* @param listener the TailerListener to use.
743+
* @param tailerListener the TailerListener to use.
744744
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
745745
*/
746746
@Deprecated
747-
public Tailer(final File file, final TailerListener listener) {
748-
this(file, listener, DEFAULT_DELAY_MILLIS);
747+
public Tailer(final File file, final TailerListener tailerListener) {
748+
this(file, tailerListener, DEFAULT_DELAY_MILLIS);
749749
}
750750

751751
/**
752752
* Creates a Tailer for the given file, starting from the beginning.
753753
*
754754
* @param file the file to follow.
755-
* @param listener the TailerListener to use.
755+
* @param tailerListener the TailerListener to use.
756756
* @param delayMillis the delay between checks of the file for new content in milliseconds.
757757
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
758758
*/
759759
@Deprecated
760-
public Tailer(final File file, final TailerListener listener, final long delayMillis) {
761-
this(file, listener, delayMillis, false);
760+
public Tailer(final File file, final TailerListener tailerListener, final long delayMillis) {
761+
this(file, tailerListener, delayMillis, false);
762762
}
763763

764764
/**
765765
* Creates a Tailer for the given file, with a delay other than the default 1.0s.
766766
*
767767
* @param file the file to follow.
768-
* @param listener the TailerListener to use.
768+
* @param tailerListener the TailerListener to use.
769769
* @param delayMillis the delay between checks of the file for new content in milliseconds.
770770
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
771771
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
772772
*/
773773
@Deprecated
774-
public Tailer(final File file, final TailerListener listener, final long delayMillis, final boolean end) {
775-
this(file, listener, delayMillis, end, IOUtils.DEFAULT_BUFFER_SIZE);
774+
public Tailer(final File file, final TailerListener tailerListener, final long delayMillis, final boolean end) {
775+
this(file, tailerListener, delayMillis, end, IOUtils.DEFAULT_BUFFER_SIZE);
776776
}
777777

778778
/**
779779
* Creates a Tailer for the given file, with a delay other than the default 1.0s.
780780
*
781781
* @param file the file to follow.
782-
* @param listener the TailerListener to use.
782+
* @param tailerListener the TailerListener to use.
783783
* @param delayMillis the delay between checks of the file for new content in milliseconds.
784784
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
785785
* @param reOpen if true, close and reopen the file between reading chunks
786786
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
787787
*/
788788
@Deprecated
789-
public Tailer(final File file, final TailerListener listener, final long delayMillis, final boolean end, final boolean reOpen) {
790-
this(file, listener, delayMillis, end, reOpen, IOUtils.DEFAULT_BUFFER_SIZE);
789+
public Tailer(final File file, final TailerListener tailerListener, final long delayMillis, final boolean end, final boolean reOpen) {
790+
this(file, tailerListener, delayMillis, end, reOpen, IOUtils.DEFAULT_BUFFER_SIZE);
791791
}
792792

793793
/**
794794
* Creates a Tailer for the given file, with a specified buffer size.
795795
*
796796
* @param file the file to follow.
797-
* @param listener the TailerListener to use.
797+
* @param tailerListener the TailerListener to use.
798798
* @param delayMillis the delay between checks of the file for new content in milliseconds.
799799
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
800800
* @param reOpen if true, close and reopen the file between reading chunks
801801
* @param bufferSize Buffer size
802802
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
803803
*/
804804
@Deprecated
805-
public Tailer(final File file, final TailerListener listener, final long delayMillis, final boolean end, final boolean reOpen, final int bufferSize) {
806-
this(file, DEFAULT_CHARSET, listener, delayMillis, end, reOpen, bufferSize);
805+
public Tailer(final File file, final TailerListener tailerListener, final long delayMillis, final boolean end, final boolean reOpen, final int bufferSize) {
806+
this(file, DEFAULT_CHARSET, tailerListener, delayMillis, end, reOpen, bufferSize);
807807
}
808808

809809
/**
810810
* Creates a Tailer for the given file, with a specified buffer size.
811811
*
812812
* @param file the file to follow.
813-
* @param listener the TailerListener to use.
813+
* @param tailerListener the TailerListener to use.
814814
* @param delayMillis the delay between checks of the file for new content in milliseconds.
815815
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
816816
* @param bufferSize Buffer size
817817
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
818818
*/
819819
@Deprecated
820-
public Tailer(final File file, final TailerListener listener, final long delayMillis, final boolean end, final int bufferSize) {
821-
this(file, listener, delayMillis, end, false, bufferSize);
820+
public Tailer(final File file, final TailerListener tailerListener, final long delayMillis, final boolean end, final int bufferSize) {
821+
this(file, tailerListener, delayMillis, end, false, bufferSize);
822822
}
823823

824824
/**
825825
* Creates a Tailer for the given file, with a specified buffer size.
826826
*
827827
* @param tailable the file to follow.
828828
* @param charset the Charset to be used for reading the file
829-
* @param listener the TailerListener to use.
829+
* @param tailerListener the TailerListener to use.
830830
* @param delayDuration the delay between checks of the file for new content in milliseconds.
831-
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
831+
* @param tailAtEnd Set to true to tail from the end of the file, false to tail from the beginning of the file.
832832
* @param reOpen if true, close and reopen the file between reading chunks
833833
* @param ignoreTouch if true, file timestamp changes without content change get ignored
834834
* @param bufferSize Buffer size
835835
*/
836-
private Tailer(final Tailable tailable, final Charset charset, final TailerListener listener, final Duration delayDuration, final boolean end,
837-
final boolean reOpen, final int bufferSize, final boolean ignoreTouch) {
836+
private Tailer(final Tailable tailable, final Charset charset, final TailerListener tailerListener, final Duration delayDuration, final boolean tailAtEnd,
837+
final boolean reOpen, final int bufferSize, final boolean ignoreTouch) {
838838
this.tailable = Objects.requireNonNull(tailable, "tailable");
839-
this.listener = Objects.requireNonNull(listener, "listener");
839+
this.listener = Objects.requireNonNull(tailerListener, "listener");
840840
this.delayDuration = delayDuration;
841-
this.tailAtEnd = end;
841+
this.tailFromEnd = tailAtEnd;
842842
this.inbuf = IOUtils.byteArray(bufferSize);
843-
844843
// Save and prepare the listener
845-
listener.init(this);
844+
tailerListener.init(this);
846845
this.reOpen = reOpen;
847846
this.charset = charset;
848847
this.ignoreTouch = ignoreTouch;
849848
}
850849

850+
/**
851+
* Creates a Tailer for the given file, with a specified buffer size.
852+
*
853+
* @param builder holds construction data.
854+
*/
855+
private Tailer(final Builder builder) {
856+
this.tailable = Objects.requireNonNull(builder.tailable, "tailable");
857+
this.listener = Objects.requireNonNull(builder.tailerListener, "listener");
858+
this.delayDuration = builder.delayDuration;
859+
this.tailFromEnd = builder.tailFromEnd;
860+
this.inbuf = IOUtils.byteArray(builder.getBufferSize());
861+
// Save and prepare the listener
862+
listener.init(this);
863+
this.reOpen = builder.reOpen;
864+
this.charset = builder.getCharset();
865+
this.ignoreTouch = builder.ignoreTouch;
866+
}
867+
851868
/**
852869
* Requests the tailer to complete its current loop and return.
853870
*/
@@ -982,7 +999,7 @@ public void run() {
982999
ThreadUtils.sleep(delayDuration);
9831000
} else {
9841001
// The current position in the file
985-
position = tailAtEnd ? tailable.size() : 0;
1002+
position = tailFromEnd ? tailable.size() : 0;
9861003
last = tailable.lastModifiedFileTime();
9871004
reader.seek(position);
9881005
}

0 commit comments

Comments
 (0)