Skip to content

Commit 04dd93f

Browse files
author
adkm
committed
2 parents 624a4ef + dfb4329 commit 04dd93f

6 files changed

Lines changed: 24 additions & 20 deletions

File tree

balking/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ incomplete or inappropriate state
1818
## Applicability
1919
Use the Balking pattern when
2020

21-
*you want to invoke an action on an object only when it is in a particular state
22-
*objects are generally only in a state that is prone to balking temporarily
21+
* you want to invoke an action on an object only when it is in a particular state
22+
* objects are generally only in a state that is prone to balking temporarily
2323
but for an unknown amount of time
2424

2525
## Related patterns
2626
* Guarded Suspension Pattern
27-
* Double Checked Locking Pattern
27+
* Double Checked Locking Pattern

factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void testElfBlacksmithWithSpear() {
9595
*/
9696
private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class<?> clazz) {
9797
assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon));
98-
assertEquals("Weapon must be of weaponType: " + clazz.getName(), expectedWeaponType,
98+
assertEquals("Weapon must be of weaponType: " + expectedWeaponType, expectedWeaponType,
9999
weapon.getWeaponType());
100100
}
101101
}

prototype/src/test/java/com/iluwatar/prototype/PrototypeTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static Collection<Object[]> data() {
5656
/**
5757
* The tested prototype instance
5858
*/
59-
private final Prototype testedPrototype;
59+
private final P testedPrototype;
6060

6161
/**
6262
* The expected {@link Prototype#toString()} value
@@ -69,7 +69,7 @@ public static Collection<Object[]> data() {
6969
* @param testedPrototype The tested prototype instance
7070
* @param expectedToString The expected {@link Prototype#toString()} value
7171
*/
72-
public PrototypeTest(final Prototype testedPrototype, final String expectedToString) {
72+
public PrototypeTest(final P testedPrototype, final String expectedToString) {
7373
this.expectedToString = expectedToString;
7474
this.testedPrototype = testedPrototype;
7575
}

throttling/src/main/java/com/iluwatar/throttling/B2BService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public B2BService(Throttler timer) {
4646
*/
4747
public int dummyCustomerApi(Tenant tenant) {
4848
String tenantName = tenant.getName();
49-
int count = CallsCount.getCount(tenantName);
49+
long count = CallsCount.getCount(tenantName);
5050
LOGGER.debug("Counter for {} : {} ", tenant.getName(), count);
5151
if (count >= tenant.getAllowedCallsPerSecond()) {
5252
LOGGER.error("API access per second limit reached for: {}", tenantName);

throttling/src/main/java/com/iluwatar/throttling/CallsCount.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,56 @@
2222
*/
2323
package com.iluwatar.throttling;
2424

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
2528
import java.util.Map;
2629
import java.util.Map.Entry;
2730
import java.util.concurrent.ConcurrentHashMap;
31+
import java.util.concurrent.atomic.AtomicLong;
2832

2933
/**
3034
* A class to keep track of the counter of different Tenants
3135
* @author drastogi
3236
*
3337
*/
3438
public final class CallsCount {
35-
private static Map<String, Integer> tenantCallsCount = new ConcurrentHashMap<>();
39+
40+
private static final Logger LOGGER = LoggerFactory.getLogger(CallsCount.class);
41+
private static Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>();
3642

3743
/**
3844
* Add a new tenant to the map.
3945
* @param tenantName name of the tenant.
4046
*/
4147
public static void addTenant(String tenantName) {
42-
if (!tenantCallsCount.containsKey(tenantName)) {
43-
tenantCallsCount.put(tenantName, 0);
44-
}
48+
tenantCallsCount.putIfAbsent(tenantName, new AtomicLong(0));
4549
}
4650

4751
/**
4852
* Increment the count of the specified tenant.
4953
* @param tenantName name of the tenant.
5054
*/
5155
public static void incrementCount(String tenantName) {
52-
tenantCallsCount.put(tenantName, tenantCallsCount.get(tenantName) + 1);
56+
tenantCallsCount.get(tenantName).incrementAndGet();
5357
}
5458

5559
/**
5660
*
5761
* @param tenantName name of the tenant.
5862
* @return the count of the tenant.
5963
*/
60-
public static int getCount(String tenantName) {
61-
return tenantCallsCount.get(tenantName);
64+
public static long getCount(String tenantName) {
65+
return tenantCallsCount.get(tenantName).get();
6266
}
6367

6468
/**
6569
* Resets the count of all the tenants in the map.
6670
*/
6771
public static void reset() {
68-
for (Entry<String, Integer> e : tenantCallsCount.entrySet()) {
69-
tenantCallsCount.put(e.getKey(), 0);
72+
LOGGER.debug("Resetting the map.");
73+
for (Entry<String, AtomicLong> e : tenantCallsCount.entrySet()) {
74+
tenantCallsCount.put(e.getKey(), new AtomicLong(0));
7075
}
7176
}
7277
}

throttling/src/test/java/com/iluwatar/throttling/B2BServiceTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ public class B2BServiceTest {
3636
@Test
3737
public void dummyCustomerApiTest() {
3838
Tenant tenant = new Tenant("testTenant", 2);
39-
Throttler timer = new ThrottleTimerImpl(10);
39+
Throttler timer = new ThrottleTimerImpl(100);
4040
B2BService service = new B2BService(timer);
41-
41+
4242
for (int i = 0; i < 5; i++) {
4343
service.dummyCustomerApi(tenant);
4444
}
45-
46-
int counter = CallsCount.getCount(tenant.getName());
45+
long counter = CallsCount.getCount(tenant.getName());
4746
Assert.assertTrue("Counter limit must be reached", counter == 2);
4847
}
4948
}

0 commit comments

Comments
 (0)