Skip to content

Commit 7d06995

Browse files
Adding files were forgotten in last commit
1 parent e1a6d9c commit 7d06995

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.split.client;
2+
3+
import com.google.common.collect.ConcurrentHashMultiset;
4+
import com.google.common.collect.Multiset;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
public class FactoryInstantiationsService {
9+
10+
private static final Logger _log = LoggerFactory.getLogger(FactoryInstantiationsService.class);
11+
private static volatile FactoryInstantiationsService _factoryInstantiationsService;
12+
private static final Multiset<String> USED_API_TOKENS = ConcurrentHashMultiset.create();
13+
14+
15+
private FactoryInstantiationsService() {}
16+
17+
public static FactoryInstantiationsService getFactoryInstantiationsServiceInstance() {
18+
if(_factoryInstantiationsService == null) {
19+
synchronized (FactoryInstantiationsService.class) {
20+
if (_factoryInstantiationsService == null) {
21+
_factoryInstantiationsService = new FactoryInstantiationsService();
22+
}
23+
}
24+
}
25+
26+
return _factoryInstantiationsService;
27+
}
28+
29+
public void addToken(String apiToken) {
30+
String message;
31+
if (USED_API_TOKENS.contains(apiToken)) {
32+
message = String.format("factory instantiation: You already have %s with this API Key. " +
33+
"We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing " +
34+
"it throughout your application.",
35+
USED_API_TOKENS.count(apiToken) == 1 ? "1 factory" : String.format("%s factories", USED_API_TOKENS.count(apiToken)));
36+
_log.warn(message);
37+
} else if (!USED_API_TOKENS.isEmpty()) {
38+
message = "factory instantiation: You already have an instance of the Split factory. " +
39+
"Make sure you definitely want this additional instance. We recommend keeping only one instance of " +
40+
"the factory at all times (Singleton pattern) and reusing it throughout your application.“";
41+
_log.warn(message);
42+
}
43+
USED_API_TOKENS.add(apiToken);
44+
}
45+
46+
public void removeToken(String apiToken) {
47+
USED_API_TOKENS.remove(apiToken);
48+
}
49+
50+
/**
51+
* Just for test
52+
* @param apiToken
53+
* @return
54+
*/
55+
public boolean isTokenPresent(String apiToken){
56+
return USED_API_TOKENS.contains(apiToken);
57+
}
58+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.split.client;
2+
3+
import junit.framework.TestCase;
4+
5+
public class FactoryInstantiationsServiceTest extends TestCase {
6+
7+
8+
}

0 commit comments

Comments
 (0)