Skip to content

Commit 467c476

Browse files
committed
release: 1.2.0
1 parent b47fe7c commit 467c476

File tree

10 files changed

+963
-9
lines changed

10 files changed

+963
-9
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Welcome to the JavaQuery util library
66

77
# Overview
88

9-
Goal is to remove repeated boler plate utility code from your project. This library offers util classes of following
9+
Goal is to remove repeated boilerplate utility code from your project. This library offers util classes of following
1010
framework and objects.
1111

1212
- <b>Collections</b>: Provides wide range of operation you perform on collection (List, Set and Map) interfaces
@@ -39,6 +39,9 @@ framework and objects.
3939
, <code>isAlphaNumeric(String value)</code>, <code>isValidEmail(String value)</code>.
4040
- <b>UniqueIdGenerator</b>: Generate unique time based random alphanumeric string like Firebase keys.
4141
- <b>LogBuilder</b>: Help you to build Map for Markers used in logging with optional execution time of code or function.
42+
- <b>JHashMap</b>: Build on top of HashMap. Provide you extra functionality to opt values.
43+
- <b>ExecutionContext</b>: Help you to transfer data between method calls and log extra details.
44+
- <b>CommonResponse</b>: Build common http response object.
4245

4346
# Maven
4447

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ repositories {
1515
dependencies {
1616
implementation('org.slf4j:slf4j-api:+')
1717
implementation('org.json:json:+')
18+
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.13.2'
1819

1920
testImplementation 'net.logstash.logback:logstash-logback-encoder:6.6'
2021
testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '+'
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.javaquery.util;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.javaquery.util.logging.Action;
5+
import com.javaquery.util.time.Dates;
6+
7+
import java.util.Date;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
/**
12+
* @author vicky.thakor
13+
* @since 1.2.0
14+
*/
15+
public class ExecutionContext<T> {
16+
17+
@JsonProperty("request_id")
18+
private final String requestId;
19+
20+
@JsonProperty("reference_id")
21+
private T referenceId;
22+
private final Action action;
23+
private Map<String, Object> meta;
24+
25+
@JsonProperty("max_retries")
26+
private Integer maxRetries = 5;
27+
28+
@JsonProperty("retries_attempted")
29+
private Integer retriesAttempted = 0;
30+
31+
@JsonProperty("created_at")
32+
private final Date createdAt;
33+
34+
public ExecutionContext(String requestId, T referenceId, Action action) {
35+
this.requestId = requestId;
36+
this.referenceId = referenceId;
37+
this.action = action;
38+
this.meta = new HashMap<>();
39+
this.createdAt = Dates.current();
40+
}
41+
42+
public ExecutionContext(T referenceId, Action action) {
43+
this.requestId = UniqueIdGenerator.generate();
44+
this.referenceId = referenceId;
45+
this.action = action;
46+
this.meta = new HashMap<>();
47+
this.createdAt = Dates.current();
48+
}
49+
50+
public ExecutionContext(Action action) {
51+
this.requestId = UniqueIdGenerator.generate();
52+
this.action = action;
53+
this.meta = new HashMap<>();
54+
this.createdAt = Dates.current();
55+
}
56+
57+
public ExecutionContext(T referenceId, Action action, Integer maxRetries) {
58+
this.requestId = UniqueIdGenerator.generate();
59+
this.referenceId = referenceId;
60+
this.action = action;
61+
this.maxRetries = maxRetries;
62+
this.meta = new HashMap<>();
63+
this.createdAt = Dates.current();
64+
}
65+
66+
public ExecutionContext(Action action, Integer maxRetries) {
67+
this.requestId = UniqueIdGenerator.generate();
68+
this.action = action;
69+
this.maxRetries = maxRetries;
70+
this.meta = new HashMap<>();
71+
this.createdAt = Dates.current();
72+
}
73+
74+
public String getRequestId() {
75+
return requestId;
76+
}
77+
78+
public T getReferenceId() {
79+
return referenceId;
80+
}
81+
82+
public Action getAction() {
83+
return action;
84+
}
85+
86+
public Map<String, Object> getMeta() {
87+
return meta;
88+
}
89+
90+
public void setMeta(Map<String, Object> meta) {
91+
this.meta = meta;
92+
}
93+
94+
public Object getMeta(String key, Object defaultValue){
95+
return meta.getOrDefault(key, defaultValue);
96+
}
97+
98+
public void addMeta(String key, Object value){
99+
this.meta.put(key, value);
100+
}
101+
102+
public Integer getMaxRetries() {
103+
return maxRetries;
104+
}
105+
106+
public Integer getRetriesAttempted() {
107+
return retriesAttempted;
108+
}
109+
110+
public Date getCreatedAt() {
111+
return createdAt;
112+
}
113+
114+
public void addRetriesAttempted(Integer retriesAttempted) {
115+
this.retriesAttempted += retriesAttempted;
116+
}
117+
118+
@Override
119+
public String toString() {
120+
return "ExecutionContext{" +
121+
"requestId='" + requestId + '\'' +
122+
", referenceId=" + referenceId +
123+
", action=" + action +
124+
", maxRetries=" + maxRetries +
125+
", retriesAttempted=" + retriesAttempted +
126+
", createdAt=" + createdAt +
127+
'}';
128+
}
129+
}

src/main/java/com/javaquery/util/collection/Collections.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public final class Collections {
2323
private Collections() {}
2424

2525
/**
26-
* Provide access to {@link java.util.Collections} method singleton.<br/>
26+
* Provide access to {@link java.util.Collections} method singleton.<br>
2727
*
2828
* Returns an immutable set containing only the specified object. The returned set is serializable.
2929
*
@@ -36,7 +36,7 @@ public static <T> Set<T> singleton(T o) {
3636
}
3737

3838
/**
39-
* Provide access to {@link java.util.Collections} method singletonList.<br/>
39+
* Provide access to {@link java.util.Collections} method singletonList.<br>
4040
* Returns an immutable list containing only the specified object. The returned list is serializable.
4141
* @param o the class of the objects in the list
4242
* @param <T> the class of the objects in the list
@@ -47,7 +47,7 @@ public static <T> List<T> singletonList(T o) {
4747
}
4848

4949
/**
50-
* Provide access to {@link java.util.Collections} method singletonMap.<br/>
50+
* Provide access to {@link java.util.Collections} method singletonMap.<br>
5151
* Returns an immutable map, mapping only the specified key to the specified value. The returned map is serializable.
5252
*
5353
* @param key the sole key to be stored in the returned map.
@@ -61,7 +61,7 @@ public static <K, V> Map<K, V> singletonMap(K key, V value) {
6161
}
6262

6363
/**
64-
* Provide access to {@link java.util.Collections} method emptySet.<br/>
64+
* Provide access to {@link java.util.Collections} method emptySet.<br>
6565
* Returns an empty set (immutable). This set is serializable. Unlike the like-named field, this method is parameterized.
6666
* @param <T> the class of the objects in the set
6767
* @return the empty set
@@ -71,14 +71,14 @@ public static <T> Set<T> emptySet() {
7171
}
7272

7373
/**
74-
* Provide access to {@link java.util.Collections} method emptyList.<br/>
74+
* Provide access to {@link java.util.Collections} method emptyList.<br>
7575
* @param <T> type of elements, if there were any, in the list
7676
* @return an empty immutable list
7777
*/
7878
public static <T> List<T> emptyList() {return EMPTY_LIST; }
7979

8080
/**
81-
* Provide access to {@link java.util.Collections} method emptyMap.<br/>
81+
* Provide access to {@link java.util.Collections} method emptyMap.<br>
8282
* Returns an empty map (immutable). This map is serializable.
8383
* @param <K> the class of the map keys
8484
* @param <V> the class of the map values
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.javaquery.util.http;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
import java.io.Serializable;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
/**
11+
* @author vicky.thakor
12+
* @since 1.2.0
13+
*/
14+
@JsonInclude(JsonInclude.Include.NON_NULL)
15+
public class CommonResponse<T> implements Serializable {
16+
17+
@JsonProperty("status_code")
18+
private final int statusCode;
19+
20+
@JsonProperty("message")
21+
private final String message;
22+
23+
private final T payload;
24+
25+
@JsonProperty("error_messages")
26+
private final List<String> errorMessages;
27+
28+
private CommonResponse(int statusCode, String message, T payload, List<String> errorMessages) {
29+
this.statusCode = statusCode;
30+
this.message = message;
31+
this.payload = payload;
32+
this.errorMessages = errorMessages;
33+
}
34+
35+
public static <T> CommonResponse<T> ok(T payload){
36+
return CommonResponse.of(HttpStatus.OK, payload);
37+
}
38+
39+
public static <T> CommonResponse<T> of(HttpStatus statusCode, String message, T payload){
40+
return new CommonResponse<>(statusCode.value(), message, payload, Collections.emptyList());
41+
}
42+
43+
public static <T> CommonResponse<T> of(HttpStatus statusCode, String message){
44+
return new CommonResponse<>(statusCode.value(), message, null, Collections.emptyList());
45+
}
46+
47+
public static <T> CommonResponse<T> of(HttpStatus statusCode, T payload){
48+
return new CommonResponse<>(statusCode.value(), null, payload, Collections.emptyList());
49+
}
50+
51+
public static <T> CommonResponse<T> of(HttpStatus statusCode, List<String> errorMessages){
52+
return new CommonResponse<>(statusCode.value(), null, null, errorMessages);
53+
}
54+
55+
public int getStatusCode() {
56+
return statusCode;
57+
}
58+
59+
public String getMessage() {
60+
return message;
61+
}
62+
63+
public T getPayload() {
64+
return payload;
65+
}
66+
67+
public List<String> getErrorMessages() {
68+
return errorMessages;
69+
}
70+
}

0 commit comments

Comments
 (0)