Skip to content

Commit c6c2cc7

Browse files
committed
created a singleton to be used instead of using wrapper classes around the constants used by BreinEngine
1 parent 86a9bb1 commit c6c2cc7

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

src/com/brein/api/Brein.java

+5-21
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@
1010

1111
public class Brein {
1212
private BreinConfig config;
13-
private BreinEngine engine;
1413

1514
/**
1615
* Sets the configuration
1716
*
1817
* @param breinConfig config object
1918
*/
2019
public Brein setConfig(final BreinConfig breinConfig) {
21-
if (this.engine != null) {
22-
shutdown();
23-
}
24-
2520
this.config = breinConfig;
2621
return this;
2722
}
@@ -33,7 +28,7 @@ public Brein setConfig(final BreinConfig breinConfig) {
3328
* This request is asynchronous.
3429
*/
3530
public void activity(final BreinActivity data, final Consumer<BreinResult> callback) {
36-
getEngine().invokeAsync(this.config, data, callback);
31+
BreinEngine.instance().invokeAsync(this.config, data, callback);
3732
}
3833

3934
/**
@@ -46,7 +41,7 @@ public void activity(final BreinActivity data, final Consumer<BreinResult> callb
4641
* @return response from request wrapped in an object called BreinResponse
4742
*/
4843
public BreinResult lookup(final BreinLookup data) {
49-
return getEngine().invoke(this.config, data);
44+
return BreinEngine.instance().invoke(this.config, data);
5045
}
5146

5247
/**
@@ -58,7 +53,7 @@ public BreinResult lookup(final BreinLookup data) {
5853
* @return result from the Breinify engine
5954
*/
6055
public BreinTemporalDataResult temporalData(final BreinTemporalData data) {
61-
final BreinResult result = getEngine().invoke(this.config, data);
56+
final BreinResult result = BreinEngine.instance().invoke(this.config, data);
6257
return new BreinTemporalDataResult(result.getMap());
6358
}
6459

@@ -73,25 +68,14 @@ public BreinTemporalDataResult temporalData(final BreinTemporalData data) {
7368
* @return the recommended items
7469
*/
7570
public BreinRecommendationResult recommendation(final BreinRecommendation data) {
76-
final BreinResult result = getEngine().invoke(this.config, data);
71+
final BreinResult result = BreinEngine.instance().invoke(this.config, data);
7772
return new BreinRecommendationResult(result.getMap());
7873
}
7974

8075
/**
8176
* Shutdown Breinify services
8277
*/
8378
public void shutdown() {
84-
if (this.engine != null) {
85-
this.engine.terminate();
86-
this.engine = null;
87-
}
88-
}
89-
90-
public BreinEngine getEngine() {
91-
if (this.engine == null) {
92-
this.engine = new BreinEngine();
93-
}
94-
95-
return engine;
79+
BreinEngine.instance().terminate();
9680
}
9781
}

src/com/brein/engine/BreinEngine.java

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ public class BreinEngine {
1818
private static final Map<BreinEngineType, IRestEngine> ENGINES = new ConcurrentHashMap<>();
1919
private static final Lock ENGINES_LOCK = new ReentrantLock();
2020

21+
private static class SingletonHolder {
22+
static final BreinEngine INSTANCE = new BreinEngine();
23+
}
24+
25+
/**
26+
* The {@code BreinEngine} class is a singleton, which can actually be accessed by this method. Creating a new
27+
* instance of a {@code BreinEngine} does not actually make any differences, since all attributes are constants,
28+
* i.e., {@code static} and {@code final}.
29+
*
30+
* @return the singleton instance of the {@code BreinEngine}
31+
*/
32+
public static BreinEngine instance() {
33+
return SingletonHolder.INSTANCE;
34+
}
35+
2136
public BreinResult invoke(final BreinConfig config, final BreinBase data) {
2237
return getEngine(config).invokeRequest(config, data);
2338
}

0 commit comments

Comments
 (0)