Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ahornace authored and Vladimir Kotal committed Apr 9, 2021
1 parent bdb2f8b commit b13c5a0
Show file tree
Hide file tree
Showing 163 changed files with 897 additions and 1,532 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,17 @@ public class AnalyzerGuru {
/**
* Descending string length comparator for magics.
*/
private static final Comparator<String> descStrlenComparator =
new Comparator<String>() {
@Override public int compare(String s1, String s2) {
// DESC: s2 length <=> s1 length
int cmp = Integer.compare(s2.length(), s1.length());
if (cmp != 0) {
return cmp;
}

// the Comparator must also be "consistent with equals", so check
// string contents too when (length)cmp == 0. (ASC: s1 <=> s2.)
cmp = s1.compareTo(s2);
private static final Comparator<String> descStrlenComparator = (s1, s2) -> {
// DESC: s2 length <=> s1 length
int cmp = Integer.compare(s2.length(), s1.length());
if (cmp != 0) {
return cmp;
}

// the Comparator must also be "consistent with equals", so check
// string contents too when (length)cmp == 0. (ASC: s1 <=> s2.)
cmp = s1.compareTo(s2);
return cmp;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.opengrok.indexer.util.StringUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -182,40 +183,31 @@ private static String[] splitLines(String str, int width) {
b.setLength(0);
}

return res.stream().toArray(String[]::new);
return res.toArray(String[]::new);
}

private static List<MappedFactory> byKey(Map<String, AnalyzerFactory> mapped) {

List<MappedFactory> res = mapped.entrySet().stream().map((t) -> {
return new MappedFactory(t.getKey(), t.getValue());
}).collect(Collectors.toList());

res.sort((mf1, mf2) -> {
return mf1.key.toLowerCase(Locale.ROOT).compareTo(
mf2.key.toLowerCase(Locale.ROOT));
});
return res;
return mapped.entrySet().stream()
.map(t -> new MappedFactory(t.getKey(), t.getValue()))
.sorted(Comparator.comparing(mf -> mf.key.toLowerCase(Locale.ROOT)))
.collect(Collectors.toList());
}

private static List<MappedFactory> byFactory(Map<String, AnalyzerFactory> mapped) {

List<MappedFactory> res = mapped.entrySet().stream().map((t) -> {
return new MappedFactory(t.getKey(), t.getValue());
}).collect(Collectors.toList());

res.sort((mf1, mf2) -> {
String r1 = reportable(mf1.fac);
String r2 = reportable(mf2.fac);
int cmp = r1.toLowerCase(Locale.ROOT).compareTo(
r2.toLowerCase(Locale.ROOT));
if (cmp != 0) {
return cmp;
}
return mf1.key.toLowerCase(Locale.ROOT).compareTo(
mf2.key.toLowerCase(Locale.ROOT));
});
return res;
return mapped.entrySet().stream()
.map(t -> new MappedFactory(t.getKey(), t.getValue()))
.sorted((mf1, mf2) -> {
String r1 = reportable(mf1.fac);
String r2 = reportable(mf2.fac);
int cmp = r1.toLowerCase(Locale.ROOT).compareTo(
r2.toLowerCase(Locale.ROOT));
if (cmp != 0) {
return cmp;
}
return mf1.key.toLowerCase(Locale.ROOT).compareTo(
mf2.key.toLowerCase(Locale.ROOT));
})
.collect(Collectors.toList());
}

private static class MappedFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2018, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.analysis;
Expand Down Expand Up @@ -115,15 +115,12 @@ public static int translate(String line, int column, int tabSize) {
int newColumn = 0;
for (int i = 0; i < column; ++i) {
char c = line.charAt(i);
switch (c) {
case '\t':
// Fill up with spaces up to the next tab stop
newColumn += tabSize - (newColumn % tabSize);
break;
default:
// \r or \n are not expected so do not handle specially.
++newColumn;
break;
if (c == '\t') {
// Fill up with spaces up to the next tab stop
newColumn += tabSize - (newColumn % tabSize);
} else {
// \r or \n are not expected so do not handle specially.
++newColumn;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
*/

/*
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, 2018, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.analysis;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -82,7 +81,7 @@ private static <T> List<T> asList(T[] a) {
if (a == null) {
return Collections.emptyList();
}
return Collections.unmodifiableList(Arrays.asList(a));
return List.of(a);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2011, Jens Elkner.
* Portions Copyright (c) 2017, 2020, Chris Fraire <cfraire@me.com>.
*/
Expand Down Expand Up @@ -144,8 +144,7 @@ public static String disjointSpan(Writer out, String className,
* @return generated span id
*/
public static String generateId(Scope scope) {
String name = Integer.toString(scope.getLineFrom()) + scope.getName()
+ scope.getSignature();
String name = scope.getLineFrom() + scope.getName() + scope.getSignature();
int hash = name.hashCode();
return "scope_id_" + Integer.toHexString(hash);
}
Expand Down Expand Up @@ -344,27 +343,16 @@ public static void writeSymbolTable(Writer out, Definitions defs)
}

// We want the symbol table to be sorted
Comparator<Tag> cmp = (Tag tag1, Tag tag2) -> {
// Order by symbol name, and then by line number if multiple
// definitions use the same symbol name
int ret = tag1.symbol.compareTo(tag2.symbol);
if (ret == 0) {
ret = tag1.line - tag2.line;
}
return ret;
};
// Order by symbol name, and then by line number if multiple
// definitions use the same symbol name
Comparator<Tag> cmp = Comparator.comparing((Tag tag) -> tag.symbol).thenComparingInt(tag -> tag.line);

Map<String, SortedSet<Tag>> symbols
= new HashMap<>();
Map<String, SortedSet<Tag>> symbols = new HashMap<>();

for (Tag tag : defs.getTags()) {
XrefStyle style = XrefStyle.getStyle(tag.type);
if (style != null && style.title != null) {
SortedSet<Tag> tags = symbols.get(style.name);
if (tags == null) {
tags = new TreeSet<>(cmp);
symbols.put(style.name, tags);
}
SortedSet<Tag> tags = symbols.computeIfAbsent(style.name, k -> new TreeSet<>(cmp));
tags.add(tag);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2018, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.analysis;
Expand Down Expand Up @@ -132,8 +132,4 @@ public void reset() throws IOException {
startPosition = 0;
}

@Override
public final void close() throws IOException {
super.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.indexer.analysis;

Expand Down Expand Up @@ -73,7 +73,7 @@ public boolean matches(int line) {

@Override
public int compareTo(Scope o) {
return lineFrom < o.lineFrom ? -1 : lineFrom > o.lineFrom ? 1 : 0;
return Integer.compare(lineFrom, o.lineFrom);
}

public int getLineFrom() {
Expand Down Expand Up @@ -121,7 +121,7 @@ public void setSignature(String signature) {
public static final Scope GLOBAL_SCOPE = new Scope(0, 0, "global", null, null);

// tree of scopes sorted by starting line
private TreeSet<Scope> scopes = new TreeSet<>();
private final TreeSet<Scope> scopes = new TreeSet<>();

public Scopes() {
// nothing to do here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2018, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.analysis.document;
Expand Down Expand Up @@ -70,7 +70,7 @@ public void setOSOverride( String value ) {
}

/**
* Starts a run of the mandoc binary to receive input from {@link write}.
* Starts a run of the mandoc binary to receive input from {@link #write}.
* @throws IOException thrown if a read or write to the mandoc process
* fails.
* @throws MandocException if no mandoc binary is defined
Expand Down Expand Up @@ -102,9 +102,7 @@ public void start() throws IOException, MandocException {

if (LOGGER.isLoggable(Level.FINER)) {
StringBuilder sb = new StringBuilder();
command.forEach((s) -> {
sb.append(s).append(" ");
});
command.forEach(s -> sb.append(s).append(" "));
String cmd = sb.toString();
LOGGER.log(Level.FINER, "Executing mandoc command [{0}]", cmd);
}
Expand Down Expand Up @@ -175,7 +173,7 @@ public String finish() throws IOException, MandocException {
* Writes a character to the input of the run of mandoc.
* @param s the string to write.
* @throws IllegalStateException thrown if the runner was not successfully
* {@link start}ed.
* {@link #start}ed.
* @throws IOException thrown if a write to the mandoc process fails.
*/
public void write(String s) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2018, 2020, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.analysis.executables;
Expand Down Expand Up @@ -166,10 +166,7 @@ public String parseELF(FileChannel fch) throws IOException {
}

private boolean isReadable(int c) {
if (c > ' ' && c <= 127) {
return true;
}
return false;
return c > ' ' && c <= 127;
}

private String getName(int tab, int stroff, MappedByteBuffer fmap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
*/

/*
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, 2018, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.analysis.plain;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.opengrok.indexer.analysis.AbstractAnalyzer;
import org.opengrok.indexer.analysis.AnalyzerFactory;
import org.opengrok.indexer.analysis.FileAnalyzerFactory;
Expand Down Expand Up @@ -66,7 +68,7 @@ private boolean isPlainText(byte[] content) throws IOException {
if (lengthBOM > 0) {
return true;
}
String ascii = new String(content, "US-ASCII");
String ascii = new String(content, StandardCharsets.US_ASCII);
return isPlainText(ascii);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,8 @@ public boolean maybeEndQuote(String capture) {
* @return true if modifiers are OK
*/
public boolean areModifiersOK() {
switch (dHead.qopname) {
case "m": // named here a la Perl for the Ruby /pat/ operator
return true;
default:
return false;
}
// "m" named here a la Perl for the Ruby /pat/ operator
return "m".equals(dHead.qopname);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.analysis.vb;
Expand All @@ -38,13 +38,8 @@ public final class Consts {
populateKeywordSet(kwds);
reservedKeywords = Collections.unmodifiableSet(kwds);

HashSet<String> hashwords = new HashSet<>();
hashwords.add("#const"); // VB lang-reference/keywords
hashwords.add("#else"); // VB lang-reference/keywords
hashwords.add("#elseif"); // VB lang-reference/keywords
hashwords.add("#end"); // VB lang-reference/keywords
hashwords.add("#if"); // VB lang-reference/keywords
directives = Collections.unmodifiableSet(hashwords);
// VB lang-reference/keywords
directives = Set.of("#const", "#else", "#elseif", "#end", "#if");
}

private Consts() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2018, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.authorization;
Expand Down Expand Up @@ -104,8 +104,7 @@ public static AuthControlFlag get(String flag) throws IllegalArgumentException {
throw new IllegalArgumentException(
String.format("No control flag \"%s\", available flags are [%s]. %s",
flag,
Arrays.asList(AuthControlFlag.values())
.stream()
Arrays.stream(AuthControlFlag.values())
.map(AuthControlFlag::toString)
.collect(Collectors.joining(", ")),
ex.getLocalizedMessage()), ex);
Expand Down
Loading

0 comments on commit b13c5a0

Please sign in to comment.