Commit e47c18a2 authored by Rajiv Prabhakar's avatar Rajiv Prabhakar
Browse files

v1.10.0 release:

- Streamc.stream added: converts an iterable into a stream
- Webutilc.getHtmlLink added: construct a html-ref-link
- ThreadUtilc.fork added: given a callable, creates a new thread for it, starts it, and returns the FutureTask
- JsonUtilc: internal refactoring and code cleanup
- Validatec.notEquals log-message fixed
- Misc unit tests and cleanups
parent b53fdf54
Pipeline #13301999 passed with stage
in 1 minute and 44 seconds
......@@ -6,7 +6,7 @@
<groupId>com.rajivprab</groupId>
<artifactId>cava</artifactId>
<version>1.9.0</version>
<version>1.10.0</version>
<name>Cava: Clean Java</name>
<description>A library that enables users to write minimal, clean and simple Java code</description>
......
......@@ -36,12 +36,12 @@ public class JsonUtilc {
}
public static boolean equals(JSONObject object1, JSONObject object2) {
Set keys1 = object1.keySet();
Set keys2 = object2.keySet();
Set<String> keys1 = object1.keySet();
Set<String> keys2 = object2.keySet();
if (!keys1.equals(keys2)) { return false; }
for (Object key : keys1) {
Object value1 = object1.get((String) key);
Object value2 = object2.get((String) key);
for (String key : keys1) {
Object value1 = object1.get(key);
Object value2 = object2.get(key);
if (!equals(value1, value2)) { return false; }
}
return true;
......@@ -90,7 +90,8 @@ public class JsonUtilc {
@Override
public boolean hasNext() {
Validatec.equals(length, array.length(), ConcurrentModificationException.class);
Validatec.equals(length, array.length(),
"Array length has changed", ConcurrentModificationException.class);
return length > nextIndex;
}
......
package org.rajivprab.cava;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class Streamc {
public static <T> Stream<T> stream(Iterable<T> iterable) {
https://stackoverflow.com/questions/23932061/convert-iterable-to-stream-using-java-8-jdk
return StreamSupport.stream(iterable.spliterator(), false);
}
}
......@@ -4,10 +4,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.*;
/**
* Library that provides user-friendly Thread class access
......@@ -25,6 +22,13 @@ public class ThreadUtilc {
}
}
public static <T> FutureTask<T> fork(Callable<T> callable) {
FutureTask<T> futureTask = new FutureTask<>(callable);
Thread thread = new Thread(futureTask);
thread.start();
return futureTask;
}
public static <T> T get(Future<T> future) {
return get(future, Duration.ofDays(100000));
}
......
......@@ -298,7 +298,7 @@ public class Validatec {
public static void notEquals(Object a, Object b, Runnable runnable) {
if (a == null || b == null || a.equals(b)) {
log.error(equalsErrorMessage(a, b));
log.error(notEqualsErrorMessage(a, b));
runnable.run();
}
}
......
package org.rajivprab.cava;
/**
* Created by rajivprab on 7/20/17.
*/
public class WebUtilc {
public static String getHtmlLink(String text, String link) {
return String.format("<a href=\"%s\" target=\"_blank\">%s</a>", link, text);
}
}
package org.rajivprab.cava;
import com.google.common.collect.Lists;
import com.google.common.truth.Truth;
import org.junit.Test;
import java.util.stream.Collectors;
public class StreamcTest extends TestBase {
@Test
public void streamSingle() {
Truth.assertThat(Streamc.stream(Lists.newArrayList("abc")).collect(Collectors.toSet()))
.containsExactly("abc");
}
@Test
public void streamMultiple() {
Truth.assertThat(Streamc.stream(Lists.newArrayList("abc", "123", "xyz")).collect(Collectors.toList()))
.containsExactly("abc", "123", "xyz");
}
@Test
public void streamEmpty() {
Truth.assertThat(Streamc.stream(Lists.newArrayList()).collect(Collectors.toSet())).isEmpty();
}
}
......@@ -11,9 +11,12 @@ import org.rajivprab.cava.ThreadUtilc.TimeException;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* Unit tests for ThreadUtil library
......@@ -103,7 +106,8 @@ public class ThreadUtilcTest extends TestBase {
try {
ThreadUtilc.get(future, SLEEP_TIME.toMillis());
Assert.fail("Should have hit timeout");
} catch (TimeException ignored) {}
} catch (TimeException ignored) {
}
Duration duration = Duration.between(start, Instant.now());
Truth.assertThat(duration).isAtLeast(SLEEP_TIME);
......@@ -112,7 +116,9 @@ public class ThreadUtilcTest extends TestBase {
@Test
public void threadUtilcGet_executionException_causedByRunTimeException_shouldThrowCause() {
FutureTask<Integer> future = new FutureTask<>(() -> {throw new ArithmeticException("test");});
FutureTask<Integer> future = new FutureTask<>(() -> {
throw new ArithmeticException("test");
});
new Thread(future).start();
try {
ThreadUtilc.get(future);
......@@ -124,7 +130,9 @@ public class ThreadUtilcTest extends TestBase {
@Test
public void threadUtilcGet_executionException_causedByCheckedException_shouldThrowCauseWithWrapper() {
FutureTask<Integer> future = new FutureTask<>(() -> {throw new IOException("test");});
FutureTask<Integer> future = new FutureTask<>(() -> {
throw new IOException("test");
});
new Thread(future).start();
try {
ThreadUtilc.get(future);
......@@ -164,7 +172,29 @@ public class ThreadUtilcTest extends TestBase {
try {
ThreadUtilc.get(getTask);
Assert.fail("Expecting exception to be thrown");
} catch (InterruptException ignored) {}
} catch (InterruptException ignored) {
}
}
@Test
public void fork_shouldRunInParallel() {
Duration sleep = Duration.ofMillis(1000);
Instant start = Instant.now();
// Getting rid of the intermediate collect will result in all sleeps happening sequentially, because of lazy streams
List<Integer> result = IntStream.range(0, 5)
.mapToObj(i -> ThreadUtilc.fork(() -> {
ThreadUtilc.sleep(sleep.toMillis());
return i;
}))
.collect(Collectors.toList()).stream()
.map(ThreadUtilc::get)
.collect(Collectors.toList());
Instant end = Instant.now();
Truth.assertThat(result).containsExactly(0, 1, 2, 3, 4).inOrder();
Truth.assertThat(Duration.between(start, end)).isGreaterThan(sleep);
Truth.assertThat(Duration.between(start, end)).isLessThan(sleep.multipliedBy(2));
}
// --
......
......@@ -458,6 +458,16 @@ public class ValidatecTest extends TestBase {
}
}
@Test
public void stringsNotEmpty_arrayIsNull_constructGivenException() {
try {
Validatec.notEmpty("my custom message", ArithmeticException.class, null);
Assert.fail();
} catch (ArithmeticException expected) {
Truth.assertThat(expected).hasMessageThat().contains("my custom message");
}
}
// -------- Map contains
@Test
......
package org.rajivprab.cava;
import com.google.common.truth.Truth;
import org.junit.Test;
public class WebUtilcTest extends TestBase {
@Test
public void getHtmlLink() {
Truth.assertThat(WebUtilc.getHtmlLink("caucus", "www.thecaucus.net"))
.isEqualTo("<a href=\"www.thecaucus.net\" target=\"_blank\">caucus</a>");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment