27
27
import java .util .regex .Pattern ;
28
28
import java .util .stream .Stream ;
29
29
30
- import javax .annotation .Nullable ;
31
-
32
30
import com .puppycrawl .tools .checkstyle .StatelessCheck ;
33
31
import com .puppycrawl .tools .checkstyle .api .DetailNode ;
34
32
import com .puppycrawl .tools .checkstyle .api .JavadocTokenTypes ;
@@ -240,12 +238,15 @@ private void validateUntaggedSummary(DetailNode ast) {
240
238
}
241
239
else if (!period .isEmpty ()) {
242
240
if (summaryDoc .contains (period )) {
243
- final String firstSentence = getFirstSentenceOrNull (ast , period );
244
- if (firstSentence == null ) {
245
- log (ast .getLineNumber (), MSG_SUMMARY_FIRST_SENTENCE );
241
+ final Optional <String > firstSentence = getFirstSentence (ast , period );
242
+
243
+ if (firstSentence .isPresent ()) {
244
+ if (containsForbiddenFragment (firstSentence .get ())) {
245
+ log (ast .getLineNumber (), MSG_SUMMARY_JAVADOC );
246
+ }
246
247
}
247
- else if ( containsForbiddenFragment ( firstSentence )) {
248
- log (ast .getLineNumber (), MSG_SUMMARY_JAVADOC );
248
+ else {
249
+ log (ast .getLineNumber (), MSG_SUMMARY_FIRST_SENTENCE );
249
250
}
250
251
}
251
252
else {
@@ -600,28 +601,30 @@ private static String getStringInsideTag(String result, DetailNode detailNode) {
600
601
}
601
602
602
603
/**
603
- * Finds and returns the first sentence.
604
+ * Finds the first sentence.
604
605
*
605
606
* @param ast The Javadoc root node.
606
607
* @param period The configured period symbol.
607
- * @return The first sentence up to and excluding the period, or null if no ending was found.
608
+ * @return An Optional containing the first sentence
609
+ * up to and excluding the period, or an empty
610
+ * Optional if no ending was found.
608
611
*/
609
- @ Nullable
610
- private static String getFirstSentenceOrNull (DetailNode ast , String period ) {
612
+ private static Optional <String > getFirstSentence (DetailNode ast , String period ) {
611
613
final List <String > sentenceParts = new ArrayList <>();
612
- String sentence = null ;
614
+ Optional < String > result = Optional . empty () ;
613
615
for (String text : (Iterable <String >) streamTextParts (ast )::iterator ) {
614
- final String sentenceEnding = findSentenceEndingOrNull (text , period );
615
- if (sentenceEnding != null ) {
616
- sentenceParts .add (sentenceEnding );
617
- sentence = String .join ("" , sentenceParts );
616
+ final Optional <String > sentenceEnding = findSentenceEnding (text , period );
617
+
618
+ if (sentenceEnding .isPresent ()) {
619
+ sentenceParts .add (sentenceEnding .get ());
620
+ result = Optional .of (String .join ("" , sentenceParts ));
618
621
break ;
619
622
}
620
623
else {
621
624
sentenceParts .add (text );
622
625
}
623
626
}
624
- return sentence ;
627
+ return result ;
625
628
}
626
629
627
630
/**
@@ -643,18 +646,17 @@ private static Stream<String> streamTextParts(DetailNode node) {
643
646
}
644
647
645
648
/**
646
- * Finds the end of a sentence. If a sentence ending period was found, returns the whole string
647
- * up to and excluding that period. The end of sentence detection here could be replaced in the
649
+ * Finds the end of a sentence. The end of sentence detection here could be replaced in the
648
650
* future by Java's built-in BreakIterator class.
649
651
*
650
652
* @param text The string to search.
651
653
* @param period The period character to find.
652
- * @return The string up to and excluding the period, or null if no ending was found.
654
+ * @return An Optional containing the string up to and excluding the period,
655
+ * or empty Optional if no ending was found.
653
656
*/
654
- @ Nullable
655
- private static String findSentenceEndingOrNull (String text , String period ) {
657
+ private static Optional <String > findSentenceEnding (String text , String period ) {
656
658
int periodIndex = text .indexOf (period );
657
- String sentenceEnding = null ;
659
+ Optional < String > result = Optional . empty () ;
658
660
while (periodIndex >= 0 ) {
659
661
final int afterPeriodIndex = periodIndex + period .length ();
660
662
@@ -663,13 +665,14 @@ private static String findSentenceEndingOrNull(String text, String period) {
663
665
if (!DEFAULT_PERIOD .equals (period )
664
666
|| afterPeriodIndex >= text .length ()
665
667
|| Character .isWhitespace (text .charAt (afterPeriodIndex ))) {
666
- sentenceEnding = text .substring (0 , periodIndex );
668
+ final String resultStr = text .substring (0 , periodIndex );
669
+ result = Optional .of (resultStr );
667
670
break ;
668
671
}
669
672
else {
670
673
periodIndex = text .indexOf (period , afterPeriodIndex );
671
674
}
672
675
}
673
- return sentenceEnding ;
676
+ return result ;
674
677
}
675
678
}
0 commit comments