Browse Source

Java 8: lambdas, instance methods, sorting

master
Gintas Grigelionis 7 years ago
parent
commit
aa01cc4bdb
16 changed files with 62 additions and 108 deletions
  1. +1
    -6
      src/main/org/apache/tools/ant/filters/ExpandProperties.java
  2. +4
    -6
      src/main/org/apache/tools/ant/filters/SortFilter.java
  3. +1
    -1
      src/main/org/apache/tools/ant/launch/Locator.java
  4. +1
    -6
      src/main/org/apache/tools/ant/taskdefs/Concat.java
  5. +2
    -3
      src/main/org/apache/tools/ant/taskdefs/Replace.java
  6. +1
    -2
      src/main/org/apache/tools/ant/taskdefs/Zip.java
  7. +7
    -10
      src/main/org/apache/tools/ant/taskdefs/launcher/VmsCommandLauncher.java
  8. +2
    -4
      src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java
  9. +3
    -6
      src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java
  10. +2
    -10
      src/main/org/apache/tools/ant/types/resources/URLResource.java
  11. +3
    -5
      src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java
  12. +4
    -6
      src/tests/junit/org/apache/tools/ant/ProjectTest.java
  13. +8
    -10
      src/tests/junit/org/apache/tools/ant/taskdefs/ExecuteWatchdogTest.java
  14. +16
    -20
      src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java
  15. +6
    -8
      src/tests/junit/org/apache/tools/ant/taskdefs/XmlPropertyTest.java
  16. +1
    -5
      src/tests/junit/org/example/junit/ThreadedOutput.java

+ 1
- 6
src/main/org/apache/tools/ant/filters/ExpandProperties.java View File

@@ -99,12 +99,7 @@ public final class ExpandProperties
getProperty = PropertyHelper.getPropertyHelper(project);
} else {
final Properties props = propertySet.getProperties();
getProperty = new GetProperty() {

public Object getProperty(String name) {
return props.getProperty(name);
}
};
getProperty = props::getProperty;
}
Object expanded = new ParseProperties(project, PropertyHelper
.getPropertyHelper(project)


+ 4
- 6
src/main/org/apache/tools/ant/filters/SortFilter.java View File

@@ -354,16 +354,14 @@ public final class SortFilter extends BaseParamFilterReader
private void sort() {
if (comparator == null) {
if (reverse) {
Collections.sort(lines, new Comparator<String>() {
public int compare(String s1, String s2) {
return (-s1.compareTo(s2)); //NOSONAR
}
});
lines.sort((s1, s2) -> {
return -s1.compareTo(s2); //NOSONAR
});
} else {
Collections.sort(lines);
}
} else {
Collections.sort(lines, comparator);
lines.sort(comparator);
}
}
}

+ 1
- 1
src/main/org/apache/tools/ant/launch/Locator.java View File

@@ -492,7 +492,7 @@ public final class Locator {
}
File[] matches = location.listFiles((dir, name) -> {
String littleName = name.toLowerCase(Locale.ENGLISH);
return Stream.of(extensions).anyMatch(x -> littleName.endsWith(x));
return Stream.of(extensions).anyMatch(littleName::endsWith);
});
urls = new URL[matches.length];
for (int i = 0; i < matches.length; ++i) {


+ 1
- 6
src/main/org/apache/tools/ant/taskdefs/Concat.java View File

@@ -503,12 +503,7 @@ public class Concat extends Task implements ResourceCollection {
}
};

private ReaderFactory<Reader> identityReaderFactory = new ReaderFactory<Reader>() {
@Override
public Reader getReader(Reader o) {
return o;
}
};
private ReaderFactory<Reader> identityReaderFactory = o -> o;

/**
* Construct a new Concat task.


+ 2
- 3
src/main/org/apache/tools/ant/taskdefs/Replace.java View File

@@ -30,7 +30,6 @@ import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
@@ -940,8 +939,8 @@ public class Replace extends MatchingTask {
*/
private Iterator<Object> getOrderedIterator(Properties props) {
List<Object> keys = new ArrayList<>(props.keySet());
Collections.sort(keys, Comparator
.comparingInt(o -> Objects.toString(o, "").length()).reversed());
keys.sort(Comparator.comparingInt(o -> Objects.toString(o, "").length())
.reversed());
return keys.iterator();
}
}

+ 1
- 2
src/main/org/apache/tools/ant/taskdefs/Zip.java View File

@@ -26,7 +26,6 @@ import java.nio.file.Files;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Hashtable;
@@ -1632,7 +1631,7 @@ public class Zip extends MatchingTask {
}
// make sure directories are in alpha-order - this also
// ensures parents come before their children
Collections.sort(dirs, Comparator.comparing(Resource::getName));
dirs.sort(Comparator.comparing(Resource::getName));
final List<Resource> rs = new ArrayList<>(dirs);
rs.addAll(files);
result[i] = rs.toArray(new Resource[rs.size()]);


+ 7
- 10
src/main/org/apache/tools/ant/taskdefs/launcher/VmsCommandLauncher.java View File

@@ -122,16 +122,13 @@ public class VmsCommandLauncher extends Java13CommandLauncher {
}

private void deleteAfter(final File f, final Process p) {
new Thread() {
@Override
public void run() {
try {
p.waitFor();
} catch(InterruptedException e) {
// ignore
}
FileUtils.delete(f);
new Thread(() -> {
try {
p.waitFor();
} catch(InterruptedException e) {
// ignore
}
}.start();
FileUtils.delete(f);
}).start();
}
}

+ 2
- 4
src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java View File

@@ -185,10 +185,8 @@ public class XMLResultAggregator extends Task implements XMLConstants {
DirectoryScanner ds = fs.getDirectoryScanner(p);
ds.scan();
return Stream.of(ds.getIncludedFiles())
.filter(pathname -> pathname.endsWith(".xml")).map(pathname -> {
return p.resolveFile(
new File(ds.getBasedir(), pathname).getPath());
});
.filter(pathname -> pathname.endsWith(".xml"))
.map(pathname -> p.resolveFile(new File(ds.getBasedir(), pathname).getPath()));
}).toArray(File[]::new);
}



+ 3
- 6
src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java View File

@@ -395,21 +395,18 @@ public class SSHExec extends SSHBase {
channel.connect();
// wait for it to finish
thread =
new Thread() {
@Override
public void run() {
new Thread(() -> {
while (!channel.isClosed()) {
if (thread == null) {
return;
}
try {
sleep(RETRY_INTERVAL);
Thread.sleep(RETRY_INTERVAL);
} catch (final Exception e) {
// ignored
}
}
}
};
});

thread.start();
thread.join(maxwait);


+ 2
- 10
src/main/org/apache/tools/ant/types/resources/URLResource.java View File

@@ -267,11 +267,7 @@ public class URLResource extends Resource implements URLProvider {
if (!isExists(false)) {
return UNKNOWN_DATETIME;
}
return withConnection(new ConnectionUser() {
public long useConnection(URLConnection c) {
return conn.getLastModified();
}
}, UNKNOWN_DATETIME);
return withConnection(c -> conn.getLastModified(), UNKNOWN_DATETIME);
}

/**
@@ -296,11 +292,7 @@ public class URLResource extends Resource implements URLProvider {
if (!isExists(false)) {
return 0L;
}
return withConnection(new ConnectionUser() {
public long useConnection(URLConnection c) {
return conn.getContentLength();
}
}, UNKNOWN_SIZE);
return withConnection(c -> conn.getContentLength(), UNKNOWN_SIZE);
}

/**


+ 3
- 5
src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java View File

@@ -179,12 +179,10 @@ public class JavaxScriptRunner extends ScriptRunnerBase {

if ("FX".equalsIgnoreCase(getLanguage())) {
source = source.entrySet().stream()
.collect(Collectors.toMap(
e -> String.format("%s:%s", e.getKey(),
e.getValue().getClass().getName()),
Map.Entry::getValue));
.collect(Collectors.toMap(e -> String.format("%s:%s", e.getKey(),
e.getValue().getClass().getName()), Map.Entry::getValue));
}
source.forEach(target::accept);
source.forEach(target);
}

private ScriptEngine createEngine() {


+ 4
- 6
src/tests/junit/org/apache/tools/ant/ProjectTest.java View File

@@ -297,12 +297,10 @@ public class ProjectTest {
}
});
final boolean[] done = new boolean[] {false};
Thread t = new Thread() {
public void run() {
p.log(FOO, Project.MSG_INFO);
done[0] = true;
}
};
Thread t = new Thread(() -> {
p.log(FOO, Project.MSG_INFO);
done[0] = true;
});
t.start();
t.join(2000);
assertTrue("Expected logging thread to finish successfully", done[0]);


+ 8
- 10
src/tests/junit/org/apache/tools/ant/taskdefs/ExecuteWatchdogTest.java View File

@@ -136,16 +136,14 @@ public class ExecuteWatchdogTest {
watchdog.start(process);

// I assume that starting this takes less than TIME_OUT/2 ms...
Thread thread = new Thread() {
public void run() {
try {
process.waitFor();
} catch(InterruptedException e) {
// not very nice but will do the job
throw new AssumptionViolatedException("process interrupted in thread", e);
}
}
};
Thread thread = new Thread(() -> {
try {
process.waitFor();
} catch(InterruptedException e) {
// not very nice but will do the job
throw new AssumptionViolatedException("process interrupted in thread", e);
}
});
thread.start();

// wait for TIME_OUT / 2, there should be about TIME_OUT / 2 ms remaining before timeout


+ 16
- 20
src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java View File

@@ -383,13 +383,11 @@ public class JavaTest {
// reader will be read
java.execute();

Thread inputThread = new Thread(new Runnable() {
public void run() {
Input input = new Input();
input.setProject(buildRule.getProject());
input.setAddproperty("input.value");
input.execute();
}
Thread inputThread = new Thread(() -> {
Input input = new Input();
input.setProject(buildRule.getProject());
input.setAddproperty("input.value");
input.execute();
});
inputThread.start();

@@ -425,19 +423,17 @@ public class JavaTest {
final boolean[] timeout = new boolean[1];
timeout[0] = false;

Thread writingThread = new Thread(new Runnable() {
public void run() {
try {
// wait a little bit to have the target executed
Thread.sleep(500);
} catch (InterruptedException e) {
throw new AssumptionViolatedException("Thread interrupted", e);
}
try {
out.write("foo-FlushedInput\n".getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
Thread writingThread = new Thread(() -> {
try {
// wait a little bit to have the target executed
Thread.sleep(500);
} catch (InterruptedException e) {
throw new AssumptionViolatedException("Thread interrupted", e);
}
try {
out.write("foo-FlushedInput\n".getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
writingThread.setDaemon(true);


+ 6
- 8
src/tests/junit/org/apache/tools/ant/taskdefs/XmlPropertyTest.java View File

@@ -343,14 +343,12 @@ public class XmlPropertyTest {
* and below.
*/
private static void getFiles (final File startingDir, Vector<File> collect) {
FileFilter filter = new FileFilter() {
public boolean accept (File file) {
if (file.isDirectory()) {
return true;
} else {
return (file.getPath().indexOf("taskdefs") > 0 &&
file.getPath().toLowerCase().endsWith(".xml"));
}
FileFilter filter = file -> {
if (file.isDirectory()) {
return true;
} else {
return (file.getPath().indexOf("taskdefs") > 0 &&
file.getPath().toLowerCase().endsWith(".xml"));
}
};



+ 1
- 5
src/tests/junit/org/example/junit/ThreadedOutput.java View File

@@ -32,11 +32,7 @@ public class ThreadedOutput extends TestCase {
}

public void testOutput() throws InterruptedException {
Thread t = new Thread(new Runnable() {
public void run() {
System.out.println("foo");
}
});
Thread t = new Thread(() -> System.out.println("foo"));
t.start();
t.join();
}


Loading…
Cancel
Save