Skip to content

Commit c978a7d

Browse files
authored
Various minor code cleanup (#1109)
1 parent ef44e24 commit c978a7d

File tree

19 files changed

+113
-112
lines changed

19 files changed

+113
-112
lines changed

builtins/src/main/java/org/jline/builtins/Commands.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,13 @@ public static void history(LineReader reader, PrintStream out, PrintStream err,
240240
} else if (opt.isSet("save")) {
241241
history.save();
242242
} else if (opt.isSet("A")) {
243-
Path file = opt.args().size() > 0 ? currentDir.resolve(opt.args().get(0)) : null;
243+
Path file = !opt.args().isEmpty() ? currentDir.resolve(opt.args().get(0)) : null;
244244
history.append(file, increment);
245245
} else if (opt.isSet("R")) {
246-
Path file = opt.args().size() > 0 ? currentDir.resolve(opt.args().get(0)) : null;
246+
Path file = !opt.args().isEmpty() ? currentDir.resolve(opt.args().get(0)) : null;
247247
history.read(file, increment);
248248
} else if (opt.isSet("W")) {
249-
Path file = opt.args().size() > 0 ? currentDir.resolve(opt.args().get(0)) : null;
249+
Path file = !opt.args().isEmpty() ? currentDir.resolve(opt.args().get(0)) : null;
250250
history.write(file, increment);
251251
} else {
252252
done = false;
@@ -361,7 +361,7 @@ public ReExecute(History history, Options opt) throws IOException {
361361
if (edit) {
362362
cmdFile = File.createTempFile("jline-history-", null);
363363
cmdWriter = new FileWriter(cmdFile);
364-
} else if (opt.args().size() > 0) {
364+
} else if (!opt.args().isEmpty()) {
365365
String[] s = opt.args().get(argId).split("=");
366366
if (s.length == 2) {
367367
argId = argId + 1;
@@ -640,7 +640,7 @@ public static void keymap(LineReader reader, PrintStream out, PrintStream err, S
640640
if (opt.isSet("l")) {
641641
boolean commands = opt.isSet("L");
642642
// TODO: handle commands
643-
if (opt.args().size() > 0) {
643+
if (!opt.args().isEmpty()) {
644644
for (String arg : opt.args()) {
645645
KeyMap<Binding> map = keyMaps.get(arg);
646646
if (map == null) {
@@ -704,7 +704,7 @@ public static void keymap(LineReader reader, PrintStream out, PrintStream err, S
704704
err.println("keymap: keymap can not be selected with -N");
705705
return;
706706
}
707-
if (opt.args().size() > 0) {
707+
if (!opt.args().isEmpty()) {
708708
err.println("keymap: too many arguments for -d");
709709
return;
710710
}
@@ -869,9 +869,9 @@ public static void keymap(LineReader reader, PrintStream out, PrintStream err, S
869869
err.println("keymap: option -p requires a prefix string");
870870
return;
871871
}
872-
if (opt.args().size() > 0 || !opt.isSet("e") && !opt.isSet("v")) {
872+
if (!opt.args().isEmpty() || !opt.isSet("e") && !opt.isSet("v")) {
873873
Map<String, Binding> bound = map.getBoundKeys();
874-
String seq = opt.args().size() > 0 ? KeyMap.translate(opt.args().get(0)) : null;
874+
String seq = !opt.args().isEmpty() ? KeyMap.translate(opt.args().get(0)) : null;
875875
Map.Entry<String, Binding> begin = null;
876876
String last = null;
877877
Iterator<Entry<String, Binding>> iterator = bound.entrySet().iterator();
@@ -1682,21 +1682,21 @@ public static void highlighter(
16821682
pathStream.filter(pathMatcher::matches).forEach(p -> out.println(p.getFileName()));
16831683
}
16841684
} else {
1685-
File themeFile;
1685+
Path themeFile;
16861686
if (opt.isSet("view")) {
1687-
themeFile = new File(replaceFileName(currentTheme, opt.get("view")));
1687+
themeFile = Paths.get(replaceFileName(currentTheme, opt.get("view")));
16881688
} else {
1689-
themeFile = currentTheme.toFile();
1689+
themeFile = currentTheme;
16901690
}
1691-
out.println(themeFile.getAbsolutePath());
1692-
try (BufferedReader reader = new BufferedReader(new FileReader(themeFile))) {
1691+
out.println(themeFile.toAbsolutePath());
1692+
try (BufferedReader reader = Files.newBufferedReader(themeFile)) {
16931693
String line;
16941694
List<List<String>> tokens = new ArrayList<>();
16951695
int maxKeyLen = 0;
16961696
int maxValueLen = 0;
16971697
while ((line = reader.readLine()) != null) {
16981698
line = line.trim();
1699-
if (line.length() > 0 && !line.startsWith("#")) {
1699+
if (!line.isEmpty() && !line.startsWith("#")) {
17001700
List<String> parts = Arrays.asList(line.split("\\s+", 2));
17011701
if (parts.get(0).matches(REGEX_TOKEN_NAME)) {
17021702
if (parts.get(0).length() > maxKeyLen) {

builtins/src/main/java/org/jline/builtins/Less.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.io.BufferedReader;
1212
import java.io.File;
1313
import java.io.FileNotFoundException;
14-
import java.io.FileReader;
1514
import java.io.FilterInputStream;
1615
import java.io.IOException;
1716
import java.io.InputStream;
@@ -218,11 +217,11 @@ public Less(Terminal terminal, Path currentDir, Options opts, ConfigurationPath
218217
}
219218

220219
private void parseConfig(Path file) throws IOException {
221-
try (BufferedReader reader = new BufferedReader(new FileReader(file.toFile()))) {
220+
try (BufferedReader reader = Files.newBufferedReader(file)) {
222221
String line = reader.readLine();
223222
while (line != null) {
224223
line = line.trim();
225-
if (line.length() > 0 && !line.startsWith("#")) {
224+
if (!line.isEmpty() && !line.startsWith("#")) {
226225
List<String> parts = SyntaxHighlighter.RuleSplitter.split(line);
227226
if (parts.get(0).equals(COMMAND_INCLUDE)) {
228227
SyntaxHighlighter.nanorcInclude(parts.get(1), syntaxFiles);
@@ -840,20 +839,18 @@ private void addFile() throws IOException, InterruptedException {
840839
LineEditor lineEditor = new LineEditor(begPos);
841840
while (true) {
842841
checkInterrupted();
843-
Operation op;
844-
switch (op = bindingReader.readBinding(fileKeyMap)) {
845-
case ACCEPT:
846-
String name = buffer.substring(begPos);
847-
addSource(name);
848-
try {
849-
openSource();
850-
} catch (Exception exp) {
851-
ssp.restore(name);
852-
}
853-
return;
854-
default:
855-
curPos = lineEditor.editBuffer(op, curPos);
856-
break;
842+
Operation op = bindingReader.readBinding(fileKeyMap);
843+
if (op == Operation.ACCEPT) {
844+
String name = buffer.substring(begPos);
845+
addSource(name);
846+
try {
847+
openSource();
848+
} catch (Exception exp) {
849+
ssp.restore(name);
850+
}
851+
return;
852+
} else if (op != null) {
853+
curPos = lineEditor.editBuffer(op, curPos);
857854
}
858855
if (curPos > begPos) {
859856
display(false, curPos);
@@ -911,7 +908,7 @@ private boolean search() throws IOException, InterruptedException {
911908
try {
912909
String _pattern = buffer.substring(1);
913910
if (type == '&') {
914-
displayPattern = _pattern.length() > 0 ? _pattern : null;
911+
displayPattern = !_pattern.isEmpty() ? _pattern : null;
915912
getPattern(true);
916913
} else {
917914
pattern = _pattern;
@@ -1025,7 +1022,7 @@ protected void openSource() throws IOException {
10251022
if (displayMessage) {
10261023
AttributedStringBuilder asb = new AttributedStringBuilder();
10271024
asb.style(AttributedStyle.INVERSE);
1028-
asb.append(source.getName() + " (press RETURN)");
1025+
asb.append(source.getName()).append(" (press RETURN)");
10291026
asb.toAttributedString().println(terminal);
10301027
terminal.writer().flush();
10311028
terminal.reader().read();
@@ -1039,7 +1036,7 @@ protected void openSource() throws IOException {
10391036
throw exp;
10401037
} else {
10411038
AttributedStringBuilder asb = new AttributedStringBuilder();
1042-
asb.append(source.getName() + " not found!");
1039+
asb.append(source.getName()).append(" not found!");
10431040
asb.toAttributedString().println(terminal);
10441041
terminal.writer().flush();
10451042
open = false;
@@ -1384,7 +1381,7 @@ synchronized boolean display(boolean oneScreen, Integer curPos) throws IOExcepti
13841381
}
13851382
if (buffer.length() > 0) {
13861383
msg.append(" ").append(buffer);
1387-
} else if (bindingReader.getCurrentBuffer().length() > 0
1384+
} else if (!bindingReader.getCurrentBuffer().isEmpty()
13881385
&& terminal.reader().peek(1) == NonBlockingReader.READ_EXPIRED) {
13891386
msg.append(" ").append(printable(bindingReader.getCurrentBuffer()));
13901387
} else if (message != null) {

builtins/src/main/java/org/jline/builtins/Nano.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.io.ByteArrayInputStream;
1414
import java.io.ByteArrayOutputStream;
1515
import java.io.File;
16-
import java.io.FileReader;
1716
import java.io.IOException;
1817
import java.io.InputStream;
1918
import java.io.InputStreamReader;
@@ -1450,7 +1449,7 @@ public String up(String hint) {
14501449
}
14511450
boolean found = false;
14521451
for (int pid = patternId; pid < patterns.size(); pid++) {
1453-
if (hint.length() == 0 || patterns.get(pid).startsWith(hint)) {
1452+
if (hint.isEmpty() || patterns.get(pid).startsWith(hint)) {
14541453
patternId = pid + 1;
14551454
out = patterns.get(pid);
14561455
found = true;
@@ -1467,7 +1466,7 @@ public String up(String hint) {
14671466

14681467
public String down(String hint) {
14691468
String out = hint;
1470-
if (patterns.size() > 0) {
1469+
if (!patterns.isEmpty()) {
14711470
if (lastMoveUp) {
14721471
patternId--;
14731472
}
@@ -1493,7 +1492,7 @@ public String down(String hint) {
14931492
}
14941493

14951494
public void add(String pattern) {
1496-
if (pattern.trim().length() == 0) {
1495+
if (pattern.trim().isEmpty()) {
14971496
return;
14981497
}
14991498
patterns.remove(pattern);
@@ -1512,7 +1511,7 @@ public void persist() {
15121511
try (BufferedWriter writer = Files.newBufferedWriter(
15131512
historyFile.toAbsolutePath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
15141513
for (String s : patterns) {
1515-
if (s.trim().length() > 0) {
1514+
if (!s.trim().isEmpty()) {
15161515
writer.append(s);
15171516
writer.newLine();
15181517
}
@@ -1656,11 +1655,11 @@ public Nano(Terminal terminal, Path root, Options opts, ConfigurationPath config
16561655
}
16571656

16581657
private void parseConfig(Path file) throws IOException {
1659-
try (BufferedReader reader = new BufferedReader(new FileReader(file.toFile()))) {
1658+
try (BufferedReader reader = Files.newBufferedReader(file)) {
16601659
String line;
16611660
while ((line = reader.readLine()) != null) {
16621661
line = line.trim();
1663-
if (line.length() > 0 && !line.startsWith("#")) {
1662+
if (!line.isEmpty() && !line.startsWith("#")) {
16641663
List<String> parts = SyntaxHighlighter.RuleSplitter.split(line);
16651664
if (parts.get(0).equals(COMMAND_INCLUDE)) {
16661665
SyntaxHighlighter.nanorcInclude(parts.get(1), syntaxFiles);
@@ -2151,7 +2150,7 @@ private boolean save(String name) throws IOException {
21512150
setMessage("Wrote " + buffer.lines.size() + " lines");
21522151
return true;
21532152
} catch (IOException e) {
2154-
setMessage("Error writing " + name + ": " + e.toString());
2153+
setMessage("Error writing " + name + ": " + e);
21552154
return false;
21562155
} finally {
21572156
Files.deleteIfExists(t);
@@ -2312,7 +2311,7 @@ private String getReadMessage() {
23122311
return sb.toString();
23132312
}
23142313

2315-
void gotoLine() throws IOException {
2314+
void gotoLine() {
23162315
KeyMap<Operation> readKeyMap = new KeyMap<>();
23172316
readKeyMap.setUnicode(Operation.INSERT);
23182317
for (char i = 32; i < 256; i++) {
@@ -2360,7 +2359,7 @@ void gotoLine() throws IOException {
23602359
int[] args = {0, 0};
23612360
try {
23622361
for (int i = 0; i < pos.length; i++) {
2363-
if (pos[i].trim().length() > 0) {
2362+
if (!pos[i].trim().isEmpty()) {
23642363
args[i] = Integer.parseInt(pos[i]) - 1;
23652364
if (args[i] < 0) {
23662365
throw new NumberFormatException();
@@ -2786,12 +2785,7 @@ String replace() throws IOException {
27862785
if (editBuffer.length() > 0) {
27872786
replaceTerm = editBuffer.toString();
27882787
}
2789-
if (replaceTerm == null) {
2790-
setMessage("Cancelled");
2791-
throw new IllegalArgumentException();
2792-
} else {
2793-
patternHistory.add(replaceTerm);
2794-
}
2788+
patternHistory.add(replaceTerm);
27952789
return replaceTerm;
27962790
case HELP:
27972791
help("nano-replace-help.txt");
@@ -2868,7 +2862,7 @@ String computeCurPos() {
28682862
sb.append("/");
28692863
sb.append(buffer.length(buffer.lines.get(buffer.line)) + 1);
28702864
sb.append(" (");
2871-
if (buffer.lines.get(buffer.line).length() > 0) {
2865+
if (!buffer.lines.get(buffer.line).isEmpty()) {
28722866
sb.append(Math.round(
28732867
(100.0 * (buffer.offsetInLine + buffer.column)) / (buffer.length(buffer.lines.get(buffer.line)))));
28742868
} else {
@@ -2965,7 +2959,7 @@ void smoothScrolling() {
29652959
setMessage("Smooth scrolling " + (smoothScrolling ? "enabled" : "disabled"));
29662960
}
29672961

2968-
void mouseSupport() throws IOException {
2962+
void mouseSupport() {
29692963
mouseSupport = !mouseSupport;
29702964
setMessage("Mouse support " + (mouseSupport ? "enabled" : "disabled"));
29712965
terminal.trackMouse(mouseSupport ? Terminal.MouseTracking.Normal : Terminal.MouseTracking.Off);

builtins/src/main/java/org/jline/builtins/ScreenTerminal.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ private void csi_DECSTR(String p) {
10831083

10841084
private String[] vt100_parse_params(String p, String[] defaults) {
10851085
String prefix = "";
1086-
if (p.length() > 0) {
1086+
if (!p.isEmpty()) {
10871087
if (p.charAt(0) >= '<' && p.charAt(0) <= '?') {
10881088
prefix = "" + p.charAt(0);
10891089
p = p.substring(1);
@@ -1094,7 +1094,7 @@ private String[] vt100_parse_params(String p, String[] defaults) {
10941094
String[] values = new String[n];
10951095
for (int i = 0; i < n; i++) {
10961096
String value = null;
1097-
if (i < ps.length && ps[i].length() > 0) {
1097+
if (i < ps.length && !ps[i].isEmpty()) {
10981098
value = prefix + ps[i];
10991099
}
11001100
if (value == null && i < defaults.length) {
@@ -1111,7 +1111,7 @@ private String[] vt100_parse_params(String p, String[] defaults) {
11111111
private int[] vt100_parse_params(String p, int[] defaults) {
11121112
String prefix = "";
11131113
p = p == null ? "" : p;
1114-
if (p.length() > 0) {
1114+
if (!p.isEmpty()) {
11151115
if (p.charAt(0) >= '<' && p.charAt(0) <= '?') {
11161116
prefix = p.substring(0, 1);
11171117
p = p.substring(1);

builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ protected static SyntaxHighlighter build(
8181
try {
8282
if (colorTheme.isEmpty() && p.getFileName().toString().endsWith(TYPE_NANORCTHEME)) {
8383
out.setCurrentTheme(p);
84-
try (BufferedReader reader = new BufferedReader(new FileReader(p.toFile()))) {
84+
try (BufferedReader reader = Files.newBufferedReader(p)) {
8585
String line;
8686
while ((line = reader.readLine()) != null) {
8787
line = line.trim();
88-
if (line.length() > 0 && !line.startsWith("#")) {
88+
if (!line.isEmpty() && !line.startsWith("#")) {
8989
List<String> parts = Arrays.asList(line.split("\\s+", 2));
9090
colorTheme.put(parts.get(0), parts.get(1));
9191
}
@@ -126,11 +126,11 @@ public static SyntaxHighlighter build(Path nanorc, String syntaxName) {
126126
SyntaxHighlighter out = new SyntaxHighlighter(nanorc, syntaxName);
127127
List<Path> syntaxFiles = new ArrayList<>();
128128
try {
129-
try (BufferedReader reader = new BufferedReader(new FileReader(nanorc.toFile()))) {
129+
try (BufferedReader reader = Files.newBufferedReader(nanorc)) {
130130
String line;
131131
while ((line = reader.readLine()) != null) {
132132
line = line.trim();
133-
if (line.length() > 0 && !line.startsWith("#")) {
133+
if (!line.isEmpty() && !line.startsWith("#")) {
134134
List<String> parts = RuleSplitter.split(line);
135135
if (parts.get(0).equals(COMMAND_INCLUDE)) {
136136
nanorcInclude(parts.get(1), syntaxFiles);
@@ -496,7 +496,7 @@ public void parse() throws IOException {
496496
while ((line = reader.readLine()) != null) {
497497
idx++;
498498
line = line.trim();
499-
if (line.length() > 0 && !line.startsWith("#")) {
499+
if (!line.isEmpty() && !line.startsWith("#")) {
500500
List<String> parts = RuleSplitter.split(fixRegexes(line));
501501
if (parts.get(0).equals("syntax")) {
502502
syntaxName = parts.get(1);

builtins/src/test/java/org/jline/example/ArgumentMaskCallbackReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static void main(String[] args) throws IOException {
4141
do {
4242
line = reader.readLine("prompt> ", null, maskingCallback, null);
4343
System.out.println("Got line: " + line);
44-
} while (line != null && line.length() > 0);
44+
} while (line != null && !line.isEmpty());
4545
}
4646

4747
private static class CommandArgumentMask implements MaskingCallback {

builtins/src/test/java/org/jline/example/OptionMaskCallbackReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void main(String[] args) throws IOException {
3939
do {
4040
line = reader.readLine("prompt> ", null, maskingCallback, null);
4141
System.out.println("Got line: " + line);
42-
} while (line != null && line.length() > 0);
42+
} while (line != null && !line.isEmpty());
4343
}
4444

4545
private static class OptionValueMask implements MaskingCallback {

builtins/src/test/java/org/jline/example/PasswordReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public static void main(String[] args) throws IOException {
3030
do {
3131
line = reader.readLine("Enter password> ", mask);
3232
System.out.println("Got password: " + line);
33-
} while (line != null && line.length() > 0);
33+
} while (line != null && !line.isEmpty());
3434
}
3535
}

console/src/main/java/org/jline/console/CmdDesc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,6 @@ public boolean optionWithValue(String option) {
124124
}
125125

126126
public AttributedString optionDescription(String key) {
127-
return optsDesc.get(key).size() > 0 ? optsDesc.get(key).get(0) : new AttributedString("");
127+
return !optsDesc.get(key).isEmpty() ? optsDesc.get(key).get(0) : new AttributedString("");
128128
}
129129
}

0 commit comments

Comments
 (0)