Skip to content

Commit

Permalink
Issue #14881: remove null from ValidateUntaggedSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitsatr authored and romani committed Dec 27, 2024
1 parent 6062707 commit 78afa19
Showing 1 changed file with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.util.regex.Pattern;
import java.util.stream.Stream;

import javax.annotation.Nullable;

import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.DetailNode;
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
Expand Down Expand Up @@ -240,12 +238,15 @@ private void validateUntaggedSummary(DetailNode ast) {
}
else if (!period.isEmpty()) {
if (summaryDoc.contains(period)) {
final String firstSentence = getFirstSentenceOrNull(ast, period);
if (firstSentence == null) {
log(ast.getLineNumber(), MSG_SUMMARY_FIRST_SENTENCE);
final Optional<String> firstSentence = getFirstSentence(ast, period);

if (firstSentence.isPresent()) {
if (containsForbiddenFragment(firstSentence.get())) {
log(ast.getLineNumber(), MSG_SUMMARY_JAVADOC);
}
}
else if (containsForbiddenFragment(firstSentence)) {
log(ast.getLineNumber(), MSG_SUMMARY_JAVADOC);
else {
log(ast.getLineNumber(), MSG_SUMMARY_FIRST_SENTENCE);
}
}
else {
Expand Down Expand Up @@ -600,28 +601,30 @@ private static String getStringInsideTag(String result, DetailNode detailNode) {
}

/**
* Finds and returns the first sentence.
* Finds the first sentence.
*
* @param ast The Javadoc root node.
* @param period The configured period symbol.
* @return The first sentence up to and excluding the period, or null if no ending was found.
* @return An Optional containing the first sentence
* up to and excluding the period, or an empty
* Optional if no ending was found.
*/
@Nullable
private static String getFirstSentenceOrNull(DetailNode ast, String period) {
private static Optional<String> getFirstSentence(DetailNode ast, String period) {
final List<String> sentenceParts = new ArrayList<>();
String sentence = null;
Optional<String> result = Optional.empty();
for (String text : (Iterable<String>) streamTextParts(ast)::iterator) {
final String sentenceEnding = findSentenceEndingOrNull(text, period);
if (sentenceEnding != null) {
sentenceParts.add(sentenceEnding);
sentence = String.join("", sentenceParts);
final Optional<String> sentenceEnding = findSentenceEnding(text, period);

if (sentenceEnding.isPresent()) {
sentenceParts.add(sentenceEnding.get());
result = Optional.of(String.join("", sentenceParts));
break;
}
else {
sentenceParts.add(text);
}
}
return sentence;
return result;
}

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

/**
* Finds the end of a sentence. If a sentence ending period was found, returns the whole string
* up to and excluding that period. The end of sentence detection here could be replaced in the
* Finds the end of a sentence. The end of sentence detection here could be replaced in the
* future by Java's built-in BreakIterator class.
*
* @param text The string to search.
* @param period The period character to find.
* @return The string up to and excluding the period, or null if no ending was found.
* @return An Optional containing the string up to and excluding the period,
* or empty Optional if no ending was found.
*/
@Nullable
private static String findSentenceEndingOrNull(String text, String period) {
private static Optional<String> findSentenceEnding(String text, String period) {
int periodIndex = text.indexOf(period);
String sentenceEnding = null;
Optional<String> result = Optional.empty();
while (periodIndex >= 0) {
final int afterPeriodIndex = periodIndex + period.length();

Expand All @@ -663,13 +665,14 @@ private static String findSentenceEndingOrNull(String text, String period) {
if (!DEFAULT_PERIOD.equals(period)
|| afterPeriodIndex >= text.length()
|| Character.isWhitespace(text.charAt(afterPeriodIndex))) {
sentenceEnding = text.substring(0, periodIndex);
final String resultStr = text.substring(0, periodIndex);
result = Optional.of(resultStr);
break;
}
else {
periodIndex = text.indexOf(period, afterPeriodIndex);
}
}
return sentenceEnding;
return result;
}
}

0 comments on commit 78afa19

Please sign in to comment.