Skip to content

Commit

Permalink
code cleanup as suggested by IDEA's code inspections
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaber committed Mar 23, 2015
1 parent f7176eb commit 0dbc4c1
Show file tree
Hide file tree
Showing 30 changed files with 81 additions and 75 deletions.
Expand Up @@ -205,7 +205,7 @@ private String toString(String readingDelimiter, boolean includeChunks) {
sb.append(token.getToken());
} else {
if (!element.isWhitespace()) {
sb.append(token.toString());
sb.append(token);
if (iterator.hasNext()) {
sb.append(readingDelimiter);
}
Expand Down
Expand Up @@ -129,7 +129,7 @@ private static String getBuildDate() {
* <li>ONLYPARA - only paragraph-level rules</li>
* <li>ONLYNONPARA - only sentence-level rules</li></ul>
*/
public static enum ParagraphHandling {
public enum ParagraphHandling {
/**
* Handle normally - all kinds of rules run.
*/
Expand Down
Expand Up @@ -29,6 +29,9 @@ public final class Languages {

private static final List<Language> LANGUAGES = getAllLanguages();

private Languages() {
}

/**
* Language classes are detected at runtime by searching the classpath for files named
* {@code META-INF/org/languagetool/language-module.properties}. Those file(s)
Expand Down
Expand Up @@ -37,11 +37,11 @@ public ResourceBundleWithFallback(ResourceBundle bundle, ResourceBundle fallback

@Override
public Object handleGetObject(String key) {
final String string = bundle.getString(key);
if (string.trim().isEmpty()) {
final String s = bundle.getString(key);
if (s.trim().isEmpty()) {
return fallbackBundle.getString(key);
}
return string;
return s;
}

@Override
Expand Down
Expand Up @@ -48,7 +48,7 @@ public String getUrl() {
}

@Override
public final String toString() {
public String toString() {
return name;
}

Expand Down
Expand Up @@ -121,7 +121,7 @@ public String toString() {
return indexes.toString();
}

protected class LuceneSearcher {
protected static class LuceneSearcher {
final FSDirectory directory;
final IndexReader reader;
final IndexSearcher searcher;
Expand Down
Expand Up @@ -81,10 +81,10 @@ public AnnotatedText build() {
Map<Integer,Integer> mapping = new HashMap<>();
mapping.put(0, 0);
for (TextPart part : parts) {
if (part.getType().equals(TextPart.Type.TEXT)) {
if (part.getType() == TextPart.Type.TEXT) {
plainTextPosition += part.getPart().length();
totalPosition += part.getPart().length();
} else if (part.getType().equals(TextPart.Type.MARKUP)) {
} else if (part.getType() == TextPart.Type.MARKUP) {
totalPosition += part.getPart().length();
}
mapping.put(plainTextPosition, totalPosition);
Expand Down
Expand Up @@ -88,8 +88,7 @@ public RuleMatch acceptRuleMatch(RuleMatch match, Map<String, String> args, Anal
String message = match.getMessage()
.replace("{realDay}", getDayOfWeek(dateFromDate))
.replace("{day}", getDayOfWeek(calFromDateString));
RuleMatch newMatch = new RuleMatch(match.getRule(), match.getFromPos(), match.getToPos(), message, match.getShortMessage());
return newMatch;
return new RuleMatch(match.getRule(), match.getFromPos(), match.getToPos(), message, match.getShortMessage());
} else {
return null;
}
Expand Down
Expand Up @@ -109,7 +109,7 @@ public String toString() {
* by default by the category author.
* @return True if the category is turned off by default.
*/
public final boolean isDefaultOff() {
public boolean isDefaultOff() {
return defaultOff;
}

Expand Down
Expand Up @@ -68,8 +68,8 @@ public RuleMatch[] match(AnalyzedSentence sourceText,
}

private boolean isLengthDifferent(final String src, final String trg) {
final double skew = (((double) src.length() / (double) trg.length()) * 100.00);
return (skew > MAX_SKEW || skew < MIN_SKEW);
final double skew = ((double) src.length() / (double) trg.length()) * 100.00;
return skew > MAX_SKEW || skew < MIN_SKEW;
}

@Override
Expand Down
Expand Up @@ -41,7 +41,7 @@ public IncorrectBitextExample(final StringPair example) {
}

/**
* @deprecated use {@link #IncorrectBitextExample(org.languagetool.bitext.StringPair, List<String>)} instead (deprecated since 2.9)
* @deprecated use {@link #IncorrectBitextExample(org.languagetool.bitext.StringPair, List)} instead (deprecated since 2.9)
*/
public IncorrectBitextExample(final StringPair example, final String[] corrections) {
this.example = example;
Expand Down
Expand Up @@ -58,9 +58,8 @@ protected boolean testAllReadings(final AnalyzedTokenReadings[] tokens,
final int numberOfReadings = tokens[tokenNo].getReadingsLength();
matcher.prepareAndGroup(firstMatchToken, tokens, rule.getLanguage());

for (int l = 0; l < numberOfReadings; l++) {
final AnalyzedToken matchToken = tokens[tokenNo]
.getAnalyzedToken(l);
for (int i = 0; i < numberOfReadings; i++) {
final AnalyzedToken matchToken = tokens[tokenNo].getAnalyzedToken(i);
boolean tested = false;
prevMatched = prevMatched || prevSkipNext > 0
&& prevElement != null
Expand Down Expand Up @@ -93,13 +92,13 @@ protected boolean testAllReadings(final AnalyzedTokenReadings[] tokens,
if (rule.isGroupsOrUnification()) {
if (!matcher.getPatternToken().isUnificationNeutral()) {
thisMatched &= testUnificationAndGroups(thisMatched,
l + 1 == numberOfReadings, matchToken, matcher, tested);
i + 1 == numberOfReadings, matchToken, matcher, tested);
}
}
}
if (thisMatched) {
for (int l = 0; l < numberOfReadings; l++) {
if (matcher.isExceptionMatchedCompletely(tokens[tokenNo].getAnalyzedToken(l))) {
for (int i = 0; i < numberOfReadings; i++) {
if (matcher.isExceptionMatchedCompletely(tokens[tokenNo].getAnalyzedToken(i))) {
return false;
}
}
Expand Down
Expand Up @@ -50,7 +50,7 @@ class FalseFriendRuleHandler extends XMLRuleHandler {
private StringBuilder translation = new StringBuilder();
private boolean inTranslation;

public FalseFriendRuleHandler(final Language textLanguage, final Language motherTongue) {
FalseFriendRuleHandler(final Language textLanguage, final Language motherTongue) {
messages = ResourceBundle.getBundle(
JLanguageTool.MESSAGE_BUNDLE, motherTongue.getLocale());
formatter = new MessageFormat("");
Expand Down Expand Up @@ -199,7 +199,7 @@ private String formatTranslations(final List<StringBuilder> translations) {
for (final Iterator<StringBuilder> iter = translations.iterator(); iter.hasNext();) {
final StringBuilder trans = iter.next();
sb.append('"');
sb.append(trans.toString());
sb.append(trans);
sb.append('"');
if (iter.hasNext()) {
sb.append(", ");
Expand Down
Expand Up @@ -177,7 +177,7 @@ public int getTokenRef() {
* @return true if match converts the case of the token.
*/
public boolean convertsCase() {
return !caseConversionType.equals(CaseConversion.NONE);
return caseConversionType != CaseConversion.NONE;
}

/** @since 2.3 */
Expand Down
Expand Up @@ -60,9 +60,9 @@ public class PatternRuleHandler extends XMLRuleHandler {
private String ruleGroupDescription;
private int startPos = -1;
private int endPos = -1;
private int tokenCountForMarker = 0;
private int tokenCountForMarker;

private int antiPatternCounter = 0;
private int antiPatternCounter;

private boolean inRule;

Expand Down
Expand Up @@ -50,7 +50,7 @@ final class PatternRuleMatcher extends AbstractPatternRulePerformer {
this.patternTokenMatchers = createElementMatchers();
}

final RuleMatch[] match(final AnalyzedSentence sentence) throws IOException {
RuleMatch[] match(final AnalyzedSentence sentence) throws IOException {
final List<RuleMatch> ruleMatches = new ArrayList<>();

final AnalyzedTokenReadings[] tokens = sentence.getTokensWithoutWhitespace();
Expand Down
Expand Up @@ -520,7 +520,7 @@ public final void setMinOccurrence(final int i) {
*/
public final void setMaxOccurrence(final int i) {
if (i == 0) {
throw new IllegalArgumentException("maxOccurrences may not be 0: " + i);
throw new IllegalArgumentException("maxOccurrences may not be 0");
}
maxOccurrence = i;
}
Expand Down
Expand Up @@ -44,7 +44,7 @@ RuleFilter getFilter(String className) {
}
Object filter = constructor.newInstance();
if (filter instanceof RuleFilter) {
return ((RuleFilter) filter);
return (RuleFilter) filter;
} else {
throw new RuntimeException("Filter class '" + className + "' must implement interface " + RuleFilter.class.getSimpleName());
}
Expand Down
Expand Up @@ -89,7 +89,7 @@ private MappingAndTags loadMapping(final InputStream inputStream, final String e
return result;
}

class MappingAndTags {
static class MappingAndTags {
Map<String, List<String>> mapping = new HashMap<>();
Set<String> tags = new HashSet<>();
}
Expand Down
Expand Up @@ -151,7 +151,7 @@ public final AnalyzedSentence disambiguate(final AnalyzedSentence input) {
int lenCounter = 0;
while (j < anTokens.length) {
if (!anTokens[j].isWhitespace()) {
if ((j == i) && (myCount == 1)) {
if (j == i && myCount == 1) {
tokens.append(anTokens[j].getToken().toLowerCase());
} else {
tokens.append(anTokens[j].getToken());
Expand Down Expand Up @@ -179,7 +179,7 @@ public final AnalyzedSentence disambiguate(final AnalyzedSentence input) {
if (mStartNoSpace.containsKey(tok.substring(0, 1))) {
int j = i;
while (j < anTokens.length && !anTokens[j].isWhitespace()) {
if ((j == i) && (myCount == 1)) {
if (j == i && myCount == 1) {
tokens.append(anTokens[j].getToken().toLowerCase());
} else {
tokens.append(anTokens[j].getToken());
Expand Down
Expand Up @@ -39,7 +39,7 @@ class DisambiguationPatternRuleReplacer extends AbstractPatternRulePerformer {

private final List<Boolean> pTokensMatched;

public DisambiguationPatternRuleReplacer(DisambiguationPatternRule rule) {
DisambiguationPatternRuleReplacer(DisambiguationPatternRule rule) {
super(rule, rule.getLanguage().getDisambiguationUnifier());
pTokensMatched = new ArrayList<>(rule.getPatternTokens().size());
}
Expand Down Expand Up @@ -420,7 +420,7 @@ private AnalyzedTokenReadings[] executeAction(final AnalyzedSentence sentence,
private void annotateChange(AnalyzedTokenReadings atr,
final String prevValue, String prevAnot) {
atr.setHistoricalAnnotations(prevAnot + "\n" + rule.getId() + ":"
+ rule.getSubId() + " " + prevValue + " -> " + atr.toString());
+ rule.getSubId() + " " + prevValue + " -> " + atr);
}

private AnalyzedTokenReadings replaceTokens(AnalyzedTokenReadings oldAtr,
Expand Down
Expand Up @@ -54,7 +54,7 @@ class DisambiguationRuleHandler extends XMLRuleHandler {

private int startPos = -1;
private int endPos = -1;
private int tokenCountForMarker = 0;
private int tokenCountForMarker;

private Match posSelector;

Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.languagetool.Language;

import java.io.*;
import java.lang.Character;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;
Expand All @@ -39,7 +40,7 @@ public final class StringTools {
/**
* Constants for printing XML rule matches.
*/
public static enum XmlPrintMode {
public enum XmlPrintMode {
/**
* Normally output the rule matches by starting and
* ending the XML output on every call.
Expand Down Expand Up @@ -334,12 +335,12 @@ public static String listToString(final Collection<String> l, final String delim
* for a single space in a word (for example, if the language supports numbers
* formatted with spaces as single tokens, as Catalan in LanguageTool).
*
* @param string String to be filtered.
* @return Filtered string.
* @param s String to be filtered.
* @return Filtered s.
*/
public static String trimWhitespace(final String string) {
public static String trimWhitespace(final String s) {
final StringBuilder filter = new StringBuilder();
String str = string.trim();
String str = s.trim();
for (int i = 0; i < str.length(); i++) {
while (str.charAt(i) <= ' ' && i < str.length() &&
(str.charAt(i + 1) <= ' ' || i > 1 && str.charAt(i - 1) <= ' ')) {
Expand Down Expand Up @@ -403,7 +404,7 @@ public static boolean isWhitespace(final String str) {
// We need u200B​​ to be detected as whitespace for Khmer, as it was the case before Java 7.
return true;
}
return java.lang.Character.isWhitespace(trimStr.charAt(0));
return Character.isWhitespace(trimStr.charAt(0));
}
return false;
}
Expand Down
Expand Up @@ -87,8 +87,8 @@ public static void testSplit(final String[] sentences, final SentenceTokenizer s
final StringBuilder inputString = new StringBuilder();
final List<String> input = new ArrayList<>();
Collections.addAll(input, sentences);
for (final String string : input) {
inputString.append(string);
for (final String s : input) {
inputString.append(s);
}
assertEquals(input, sTokenizer.tokenize(inputString.toString()));
}
Expand Down
Expand Up @@ -108,7 +108,7 @@ private void testBadSentence(final String origBadSentence,
rule.getMessage().contains("<suggestion>")
);
assertTrue(lang + ": Incorrect suggestions: "
+ suggestedCorrection.toString() + " != "
+ suggestedCorrection + " != "
+ matches[0].getSuggestedReplacements() + " for rule " + rule,
suggestedCorrection.equals(matches[0]
.getSuggestedReplacements()));
Expand Down
Expand Up @@ -297,7 +297,7 @@ private void testBadSentences(JLanguageTool languageTool, JLanguageTool allRules
final AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(badSentence);
final StringBuilder sb = new StringBuilder("Analyzed token readings:");
for (AnalyzedTokenReadings atr : analyzedSentence.getTokens()) {
sb.append(" ").append(atr.toString());
sb.append(" ").append(atr);
}
fail(lang + " rule " + rule + ":\n\"" + badSentence + "\"\n"
+ "Errors expected: 1\n"
Expand Down Expand Up @@ -389,12 +389,12 @@ private void assertSuggestions(String sentence, Language lang, List<String> expe
if (realSuggestions.size() == 0) {
boolean expectedEmptyCorrection = expectedCorrections.size() == 1 && expectedCorrections.get(0).length() == 0;
assertTrue(lang + ": Incorrect suggestions: "
+ expectedCorrections.toString() + " != "
+ expectedCorrections + " != "
+ " <no suggestion> for rule " + rule + " on input: " + sentence,
expectedEmptyCorrection);
} else {
assertEquals(lang + ": Incorrect suggestions: "
+ expectedCorrections.toString() + " != "
+ expectedCorrections + " != "
+ realSuggestions + " for rule " + rule + " on input: " + sentence,
expectedCorrections, realSuggestions);
}
Expand Down

0 comments on commit 0dbc4c1

Please sign in to comment.