Java 11 migration: patterns (t-v) (#1085)

* Moves visitor pattern to java 11

* Moves value-object pattern to java 11

* Moves unit-of-work pattern to java 11

* Moves typeobjectpattern pattern to java 11

* Moves twin pattern to java 11

* Moves trampoline pattern to java 11

* Moves tolerant-reader pattern to java 11

* Moves tls pattern to java 11

* Moves throttling pattern to java 11

* Moves thread-pool pattern to java 11

* Moves template-method pattern to java 11
This commit is contained in:
Anurag Agarwal
2019-11-14 11:12:05 +05:30
committed by Ilkka Seppälä
parent 160b737dcc
commit 50467c9e76
45 changed files with 379 additions and 422 deletions

View File

@ -26,6 +26,7 @@ package com.iluwatar.throttling;
import com.iluwatar.throttling.timer.ThrottleTimerImpl;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -73,14 +74,14 @@ public class App {
private static void makeServiceCalls(Tenant tenant, CallsCount callsCount) {
var timer = new ThrottleTimerImpl(10, callsCount);
var service = new B2BService(timer, callsCount);
for (int i = 0; i < 20; i++) {
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
IntStream.range(0, 20).forEach(i -> {
service.dummyCustomerApi(tenant);
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
try {
Thread.sleep(1);
} catch (InterruptedException e) {
LOGGER.error("Thread interrupted: {}", e.getMessage());
}
}
});
}
}

View File

@ -73,8 +73,6 @@ public final class CallsCount {
*/
public void reset() {
LOGGER.debug("Resetting the map.");
for (Entry<String, AtomicLong> e : tenantCallsCount.entrySet()) {
tenantCallsCount.put(e.getKey(), new AtomicLong(0));
}
tenantCallsCount.replaceAll((k, v) -> new AtomicLong(0));
}
}

View File

@ -32,7 +32,6 @@ public class AppTest {
@Test
public void test() {
final String[] args = {};
App.main(args);
App.main(new String[]{});
}
}

View File

@ -23,12 +23,12 @@
package com.iluwatar.throttling;
import com.iluwatar.throttling.timer.Throttler;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.iluwatar.throttling.timer.Throttler;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
/**
* B2BServiceTest class to test the B2BService
*/
@ -38,15 +38,14 @@ public class B2BServiceTest {
@Test
public void dummyCustomerApiTest() {
Tenant tenant = new Tenant("testTenant", 2, callsCount);
var tenant = new Tenant("testTenant", 2, callsCount);
// In order to assure that throttling limits will not be reset, we use an empty throttling implementation
Throttler timer = () -> { };
B2BService service = new B2BService(timer, callsCount);
var timer = (Throttler) () -> {
};
var service = new B2BService(timer, callsCount);
for (int i = 0; i < 5; i++) {
service.dummyCustomerApi(tenant);
}
long counter = callsCount.getCount(tenant.getName());
IntStream.range(0, 5).mapToObj(i -> tenant).forEach(service::dummyCustomerApi);
var counter = callsCount.getCount(tenant.getName());
assertEquals(2, counter, "Counter limit must be reached");
}
}

View File

@ -23,11 +23,10 @@
package com.iluwatar.throttling;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.security.InvalidParameterException;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/**
* TenantTest to test the creation of Tenant with valid parameters.
@ -37,7 +36,7 @@ public class TenantTest {
@Test
public void constructorTest() {
assertThrows(InvalidParameterException.class, () -> {
Tenant tenant = new Tenant("FailTenant", -1, new CallsCount());
new Tenant("FailTenant", -1, new CallsCount());
});
}
}