Skip to content

Commit

Permalink
Update to require Java 8, start using lambda and other Java 8 feature…
Browse files Browse the repository at this point in the history
…s, update some third party libraries
  • Loading branch information
centic9 committed Dec 17, 2017
1 parent a0aaf8c commit 9f1a792
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 227 deletions.
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply plugin: 'codenarc'
apply plugin: 'maven'
apply plugin: 'signing'

sourceCompatibility = 1.7
sourceCompatibility = 1.8
group = 'org.dstadler'
archivesBaseName = 'commons-dost'

Expand All @@ -29,15 +29,15 @@ eclipse {
}

dependencies {
compile 'commons-io:commons-io:2.5'
compile 'org.apache.commons:commons-lang3:3.6'
compile 'org.apache.httpcomponents:httpclient:4.5.2'
compile 'commons-io:commons-io:2.6'
compile 'org.apache.commons:commons-lang3:3.7'
compile 'org.apache.httpcomponents:httpclient:4.5.4'
compile 'org.apache.commons:commons-exec:1.3'
compile 'log4j:log4j:1.2.17'
compile 'net.java.dev.jna:jna:3.4.0'

testCompile 'junit:junit:4.12'
testCompile 'org.dstadler:commons-test:1.0.0.12'
testCompile 'org.dstadler:commons-test:1.0.0.13'
}

// work around unnecessary timestamp in generated file which always causes dirty files in version control
Expand Down Expand Up @@ -86,7 +86,7 @@ test {
}

jacoco {
toolVersion = '0.7.8'
toolVersion = '0.7.9'
}

jacocoTestReport {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/dstadler/commons/collections/MapUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -36,7 +35,7 @@ private MapUtils() {
*/
public static <K, V extends Comparable<V>> List<Entry<K, V>> sortByValue(Map<K, V> map) {
List<Entry<K, V>> entries = new ArrayList<>(map.entrySet());
Collections.sort(entries, new ByValue<K, V>());
entries.sort(new ByValue<>());
return entries;
}

Expand All @@ -58,7 +57,7 @@ public static <K, V extends Comparable<V>> List<Entry<K, V>> sortByValue(Map<K,
*/
public static <K extends Comparable<K>, V extends Comparable<V>> List<Map.Entry<K, V>> sortByValueAndKey(Map<K, V> map) {
List<Map.Entry<K, V>> entries = new ArrayList<>(map.entrySet());
Collections.sort(entries, new ByValueAndKey<K,V>());
entries.sort(new ByValueAndKey<>());
return entries;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void count(Collection<T> items) {

@Override
public int get(T k) {
return map.containsKey(k) ? map.get(k) : 0;
return map.getOrDefault(k, 0);
}

@Override
Expand All @@ -60,30 +60,26 @@ public void clear() {
@Override
public Map<T, Integer> sortedMap() {
List<Map.Entry<T, Integer>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<T, Integer>>() {

@Override
public int compare(Map.Entry<T, Integer> o1, Map.Entry<T, Integer> o2) {
// reverse ordering to get highest values first
int ret = (-1) * o1.getValue().compareTo(o2.getValue());
if(ret != 0) {
return ret;
}

final T key1 = o1.getKey();
final T key2 = o2.getKey();

// we use a HashMap which allows null-keys
if(key1 == null && key2 == null) {
return 0;
} else if(key1 == null) {
return -1;
} else if(key2 == null) {
return 1;
}

return key1.toString().compareTo(key2.toString());
list.sort((o1, o2) -> {
// reverse ordering to get highest values first
int ret = (-1) * o1.getValue().compareTo(o2.getValue());
if (ret != 0) {
return ret;
}

final T key1 = o1.getKey();
final T key2 = o2.getKey();

// we use a HashMap which allows null-keys
if (key1 == null && key2 == null) {
return 0;
} else if (key1 == null) {
return -1;
} else if (key2 == null) {
return 1;
}

return key1.toString().compareTo(key2.toString());
});

Map<T, Integer> result = new LinkedHashMap<>();
Expand Down
34 changes: 14 additions & 20 deletions src/main/java/org/dstadler/commons/http/HttpClientWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,13 @@ public CloseableHttpClient getHttpClient() {
*/
public String simpleGet(String url) throws IOException {
final AtomicReference<String> str = new AtomicReference<>();
simpleGet(url, new Consumer<InputStream>() {
@Override
public void accept(InputStream inputStream) {
try {
str.set(IOUtils.toString(inputStream, "UTF-8"));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
});
simpleGet(url, inputStream -> {
try {
str.set(IOUtils.toString(inputStream, "UTF-8"));
} catch (IOException e) {
throw new IllegalStateException(e);
}
});

return str.get();
}
Expand All @@ -136,16 +133,13 @@ public void accept(InputStream inputStream) {
*/
public byte[] simpleGetBytes(String url) throws IOException {
final AtomicReference<byte[]> bytes = new AtomicReference<>();
simpleGet(url, new Consumer<InputStream>() {
@Override
public void accept(InputStream inputStream) {
try {
bytes.set(IOUtils.toByteArray(inputStream));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
});
simpleGet(url, inputStream -> {
try {
bytes.set(IOUtils.toByteArray(inputStream));
} catch (IOException e) {
throw new IllegalStateException(e);
}
});

return bytes.get();
}
Expand Down
30 changes: 13 additions & 17 deletions src/main/java/org/dstadler/commons/svn/LogEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,19 @@ public String toString() {

public void addPath(String path, String action) {
if(paths == null) {
paths = new TreeSet<>(new Comparator<Pair<String,String>>() {

@Override
public int compare(Pair<String,String> o1, Pair<String,String> o2) {
// sort "..." at the end, otherwise sort alphabetically
String path1 = o1.getLeft();
if(path1 != null && path1.equals(MORE)) {
return 1;
}
String path2 = o2.getLeft();
if(path2 != null && path2.equals(MORE)) {
return -1;
}

return o1.compareTo(o2);
}
});
paths = new TreeSet<>((o1, o2) -> {
// sort "..." at the end, otherwise sort alphabetically
String path1 = o1.getLeft();
if(path1 != null && path1.equals(MORE)) {
return 1;
}
String path2 = o2.getLeft();
if(path2 != null && path2.equals(MORE)) {
return -1;
}

return o1.compareTo(o2);
});
}
paths.add(Pair.of(path, action));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,57 +34,51 @@ public void test() throws IOException {

@Test
public void testLargeData() {
TestHelpers.runTestWithDifferentLogLevel(new Runnable() {
@Override
public void run() {
try (BufferingLogOutputStream stream = new BufferingLogOutputStream()) {
TestHelpers.runTestWithDifferentLogLevel(() -> {
try (BufferingLogOutputStream stream = new BufferingLogOutputStream()) {

// sends everything to Level.INFO
for (int i = 0; i < 1000; i++) {
stream.processLine("some line", 0);
}
// sends everything to Level.INFO
for (int i = 0; i < 1000; i++) {
stream.processLine("some line", 0);
}

stream.close();
stream.close();

// try closing again, should not fail
stream.close();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}, BufferingLogOutputStream.class.getName(), Level.WARNING);
// try closing again, should not fail
stream.close();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}, BufferingLogOutputStream.class.getName(), Level.WARNING);
}

@Test
public void testMultipleThreads() throws Throwable {
TestHelpers.runTestWithDifferentLogLevel(new Runnable() {
@Override
public void run() {
try {
ThreadTestHelper helper =
new ThreadTestHelper(NUMBER_OF_THREADS, NUMBER_OF_TESTS);

try (final BufferingLogOutputStream stream = new BufferingLogOutputStream()) {
helper.executeTest(new ThreadTestHelper.TestRunnable() {

@Override
public void doEnd(int threadNum) throws Exception {
// do stuff at the end ...
}

@Override
public void run(int threadNum, int it) throws Exception {
for (int i = 0; i < 100; i++) {
stream.processLine("some line", 0);
}
}
});
}
} catch (Throwable e) {
throw new IllegalStateException(e);
}
}
}, BufferingLogOutputStream.class.getName(), Level.WARNING);
TestHelpers.runTestWithDifferentLogLevel(() -> {
try {
ThreadTestHelper helper =
new ThreadTestHelper(NUMBER_OF_THREADS, NUMBER_OF_TESTS);

try (final BufferingLogOutputStream stream = new BufferingLogOutputStream()) {
helper.executeTest(new ThreadTestHelper.TestRunnable() {

@Override
public void doEnd(int threadNum) throws Exception {
// do stuff at the end ...
}

@Override
public void run(int threadNum, int it) throws Exception {
for (int i = 0; i < 100; i++) {
stream.processLine("some line", 0);
}
}
});
}
} catch (Throwable e) {
throw new IllegalStateException(e);
}
}, BufferingLogOutputStream.class.getName(), Level.WARNING);
}

@Test
Expand Down
27 changes: 9 additions & 18 deletions src/test/java/org/dstadler/commons/http/HttpClientWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ public void testHttpClientWrapperSimpleGetStream() throws Exception {

try (MockRESTServer server = new MockRESTServer(NanoHTTPD.HTTP_OK, "text/plain", "ok")) {
final AtomicReference<String> str = new AtomicReference<>();
wrapper.simpleGet("http://localhost:" + server.getPort(), new Consumer<InputStream>() {
@Override
public void accept(InputStream inputStream) {
try {
str.set(IOUtils.toString(inputStream, "UTF-8"));
} catch (IOException e) {
throw new IllegalStateException(e);
}
wrapper.simpleGet("http://localhost:" + server.getPort(), inputStream -> {
try {
str.set(IOUtils.toString(inputStream, "UTF-8"));
} catch (IOException e) {
throw new IllegalStateException(e);
}
});

Expand Down Expand Up @@ -159,11 +156,8 @@ public void testSimpleGetBytesFails() throws Exception {
@Test
public void testSimpleGetException() throws Exception {
try (HttpClientWrapper wrapper = new HttpClientWrapper("", null, 10000)) {
try (MockRESTServer server = new MockRESTServer(new Runnable() {
@Override
public void run() {
throw new IllegalStateException("testexception");
}
try (MockRESTServer server = new MockRESTServer(() -> {
throw new IllegalStateException("testexception");
}, NanoHTTPD.HTTP_OK, "text/plain", "ok")) {
try {
wrapper.simpleGet("http://localhost:" + server.getPort());
Expand All @@ -179,11 +173,8 @@ public void run() {
@Test
public void testSimpleGetBytesException() throws Exception {
try (HttpClientWrapper wrapper = new HttpClientWrapper("", null, 10000)) {
try (MockRESTServer server = new MockRESTServer(new Runnable() {
@Override
public void run() {
throw new IllegalStateException("testexception");
}
try (MockRESTServer server = new MockRESTServer(() -> {
throw new IllegalStateException("testexception");
}, NanoHTTPD.HTTP_OK, "text/plain", "ok")) {
try {
wrapper.simpleGetBytes("http://localhost:" + server.getPort());
Expand Down
Loading

0 comments on commit 9f1a792

Please sign in to comment.