Skip to content

Commit 293c319

Browse files
Fix duplicate code
1 parent ed6f165 commit 293c319

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

taskmaster-service-helper/src/main/java/com/github/bordertech/taskmaster/service/impl/ServiceHelperProviderDefault.java

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public <S extends Serializable, T extends Serializable> TaskFuture<ResultHolder<
4646

4747
// Check action provided
4848
if (action == null) {
49-
throw new IllegalArgumentException("No service action has been provided. ");
49+
throw new IllegalArgumentException("No service action has been provided for submit async call.");
5050
}
5151

5252
// Setup the bean to hold the service result
@@ -67,28 +67,14 @@ public <S extends Serializable, T extends Serializable> TaskFuture<ResultHolder<
6767

6868
// Check action provided
6969
if (action == null) {
70-
throw new IllegalArgumentException("No service action has been provided. ");
70+
throw new IllegalArgumentException("No service action has been provided for submit async cached call.");
7171
}
7272

73-
// Check cache and cache key provided
74-
if (cache == null) {
75-
throw new IllegalArgumentException("A cache must be provided for async processing.");
76-
}
77-
if (cacheKey == null) {
78-
throw new IllegalArgumentException("A cache key must be provided for async processing. ");
79-
}
80-
81-
// Maybe already in the result cache
82-
ResultHolder cached = cache.get(cacheKey);
73+
// Check already in cache
74+
ResultHolder cached = checkCache(cache, cacheKey, cacheException);
8375
if (cached != null) {
84-
// Check for a cached exception
85-
if (cached.isException() && !cacheException) {
86-
// Invalidate cache and continue onto service call
87-
cache.remove(cacheKey);
88-
} else {
89-
LOGGER.debug(buildCacheMessagePrefix(cache, cacheKey) + "Async service already in cache so Future will hold the result.");
90-
return new TaskFutureResult<>(cached);
91-
}
76+
LOGGER.debug(buildCacheMessagePrefix(cache, cacheKey) + "Async service already in cache so Future will hold the result.");
77+
return new TaskFutureResult<>(cached);
9278
}
9379

9480
// Check already in progress (if tracking enabled)
@@ -134,7 +120,7 @@ public <S extends Serializable, T extends Serializable> ResultHolder<S, T> invok
134120

135121
// Check action provided
136122
if (action == null) {
137-
throw new IllegalArgumentException("No service action has been provided. ");
123+
throw new IllegalArgumentException("No service action has been provided for invoke sync call.");
138124
}
139125

140126
// Do service call
@@ -151,25 +137,11 @@ public <S extends Serializable, T extends Serializable> ResultHolder<S, T> invok
151137
final Cache<String, ResultHolder> cache, final String cacheKey, final boolean cacheException)
152138
throws ServiceException {
153139

154-
// Check cache and cache key provided
155-
if (cache == null) {
156-
throw new IllegalArgumentException("A cache must be provided.");
157-
}
158-
if (cacheKey == null) {
159-
throw new IllegalArgumentException("A cache key must be provided.");
160-
}
161-
162-
// Check cache for result
163-
ResultHolder cached = cache.get(cacheKey);
140+
// Check already in cache
141+
ResultHolder cached = checkCache(cache, cacheKey, cacheException);
164142
if (cached != null) {
165-
// Check for a cached exception
166-
if (cached.isException() && !cacheException) {
167-
// Invalidate cache and continue onto service call
168-
cache.remove(cacheKey);
169-
} else {
170-
LOGGER.debug(buildCacheMessagePrefix(cache, cacheKey) + "Cached service call already in cache.");
171-
return cached;
172-
}
143+
LOGGER.debug(buildCacheMessagePrefix(cache, cacheKey) + "Cached service already in cache.");
144+
return cached;
173145
}
174146

175147
// Do service call
@@ -217,6 +189,38 @@ protected <S extends Serializable, T extends Serializable> TaskFuture<ResultHold
217189
}
218190
}
219191

192+
/**
193+
* Check if service result is already in the cache.
194+
*
195+
* @param <S> the criteria type
196+
* @param <T> the response type
197+
* @param cache the result holder cache
198+
* @param cacheKey the key for the result holder
199+
* @param cacheException true if cache exception
200+
* @return the cached result or null if not in cache
201+
*/
202+
protected <S extends Serializable, T extends Serializable> ResultHolder<S, T> checkCache(
203+
final Cache<String, ResultHolder> cache, final String cacheKey, final boolean cacheException) {
204+
205+
// Check cache and cache key provided
206+
if (cache == null) {
207+
throw new IllegalArgumentException("A cache must be provided.");
208+
}
209+
if (cacheKey == null) {
210+
throw new IllegalArgumentException("A cache key must be provided.");
211+
}
212+
213+
// Check cache for result
214+
ResultHolder cached = cache.get(cacheKey);
215+
// Check for a cached exception
216+
if (cached != null && cached.isException() && !cacheException) {
217+
// Invalidate cache and continue onto service call
218+
cache.remove(cacheKey);
219+
cached = null;
220+
}
221+
return cached;
222+
}
223+
220224
/**
221225
* Flag if tracking in progress ASync cached service calls which helps avoid multiple calls for the same service call.
222226
* <p>

0 commit comments

Comments
 (0)