Skip to content

Commit 78afa19

Browse files
mohitsatrromani
authored andcommitted
Issue #14881: remove null from ValidateUntaggedSummary
1 parent 6062707 commit 78afa19

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/SummaryJavadocCheck.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import java.util.regex.Pattern;
2828
import java.util.stream.Stream;
2929

30-
import javax.annotation.Nullable;
31-
3230
import com.puppycrawl.tools.checkstyle.StatelessCheck;
3331
import com.puppycrawl.tools.checkstyle.api.DetailNode;
3432
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
@@ -240,12 +238,15 @@ private void validateUntaggedSummary(DetailNode ast) {
240238
}
241239
else if (!period.isEmpty()) {
242240
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+
}
246247
}
247-
else if (containsForbiddenFragment(firstSentence)) {
248-
log(ast.getLineNumber(), MSG_SUMMARY_JAVADOC);
248+
else {
249+
log(ast.getLineNumber(), MSG_SUMMARY_FIRST_SENTENCE);
249250
}
250251
}
251252
else {
@@ -600,28 +601,30 @@ private static String getStringInsideTag(String result, DetailNode detailNode) {
600601
}
601602

602603
/**
603-
* Finds and returns the first sentence.
604+
* Finds the first sentence.
604605
*
605606
* @param ast The Javadoc root node.
606607
* @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.
608611
*/
609-
@Nullable
610-
private static String getFirstSentenceOrNull(DetailNode ast, String period) {
612+
private static Optional<String> getFirstSentence(DetailNode ast, String period) {
611613
final List<String> sentenceParts = new ArrayList<>();
612-
String sentence = null;
614+
Optional<String> result = Optional.empty();
613615
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));
618621
break;
619622
}
620623
else {
621624
sentenceParts.add(text);
622625
}
623626
}
624-
return sentence;
627+
return result;
625628
}
626629

627630
/**
@@ -643,18 +646,17 @@ private static Stream<String> streamTextParts(DetailNode node) {
643646
}
644647

645648
/**
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
648650
* future by Java's built-in BreakIterator class.
649651
*
650652
* @param text The string to search.
651653
* @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.
653656
*/
654-
@Nullable
655-
private static String findSentenceEndingOrNull(String text, String period) {
657+
private static Optional<String> findSentenceEnding(String text, String period) {
656658
int periodIndex = text.indexOf(period);
657-
String sentenceEnding = null;
659+
Optional<String> result = Optional.empty();
658660
while (periodIndex >= 0) {
659661
final int afterPeriodIndex = periodIndex + period.length();
660662

@@ -663,13 +665,14 @@ private static String findSentenceEndingOrNull(String text, String period) {
663665
if (!DEFAULT_PERIOD.equals(period)
664666
|| afterPeriodIndex >= text.length()
665667
|| Character.isWhitespace(text.charAt(afterPeriodIndex))) {
666-
sentenceEnding = text.substring(0, periodIndex);
668+
final String resultStr = text.substring(0, periodIndex);
669+
result = Optional.of(resultStr);
667670
break;
668671
}
669672
else {
670673
periodIndex = text.indexOf(period, afterPeriodIndex);
671674
}
672675
}
673-
return sentenceEnding;
676+
return result;
674677
}
675678
}

0 commit comments

Comments
 (0)