Skip to content

Commit 06d4dff

Browse files
authored
Merge pull request #11 from javaquery/v1.2.0
V1.2.0
2 parents dafd9a9 + 467c476 commit 06d4dff

File tree

12 files changed

+1100
-12
lines changed

12 files changed

+1100
-12
lines changed

README.md

Lines changed: 6 additions & 3 deletions
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,19 +39,22 @@ 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

4548
```
4649
<dependency>
4750
<groupId>com.javaquery</groupId>
4851
<artifactId>util</artifactId>
49-
<version>1.0.7</version>
52+
<version>1.2.0</version>
5053
</dependency>
5154
```
5255

5356
# Gradle
5457

5558
```
56-
implementation 'com.javaquery:util:1.0.7'
59+
implementation 'com.javaquery:util:1.2.0'
5760
```

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77
sourceCompatibility = 1.8
88
group 'com.javaquery'
9-
version '1.0.7'
9+
version '1.2.0'
1010

1111
repositories {
1212
mavenCentral()
@@ -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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.javaquery.util.collection;
2+
3+
import com.javaquery.util.Regex;
4+
5+
import java.util.HashMap;
6+
7+
/**
8+
* @author vicky.thakor
9+
* @since 1.2.0
10+
*/
11+
public class JHashMap<K, V> extends HashMap<K, V> {
12+
13+
/**
14+
* @param key the key whose associated value is to be returned
15+
* @return the value to which the specified key is mapped, or 0 if this map contains no mapping for the key
16+
*/
17+
public Integer optInt(K key){
18+
return optInt(key, 0);
19+
}
20+
21+
/**
22+
* @param key the key whose associated value is to be returned
23+
* @param defaultValue the default mapping of the key
24+
* @return the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key
25+
*/
26+
@SuppressWarnings("unchecked")
27+
public Integer optInt(K key, Integer defaultValue){
28+
Object value = getOrDefault(key, (V) defaultValue);
29+
if(value instanceof Number){
30+
return ((Number) value).intValue();
31+
}
32+
String strValue = String.valueOf(value);
33+
return Regex.isNumber(strValue) ? Integer.parseInt(strValue) : defaultValue;
34+
}
35+
36+
/**
37+
* @param key the key whose associated value is to be returned
38+
* @return the value to which the specified key is mapped, or 0 if this map contains no mapping for the key
39+
*/
40+
public Long optLong(K key){
41+
return optLong(key, 0L);
42+
}
43+
44+
/**
45+
* @param key the key whose associated value is to be returned
46+
* @param defaultValue the default mapping of the key
47+
* @return the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key
48+
*/
49+
@SuppressWarnings("unchecked")
50+
public Long optLong(K key, Long defaultValue){
51+
Object value = getOrDefault(key, (V) defaultValue);
52+
if(value instanceof Number){
53+
return ((Number) value).longValue();
54+
}
55+
String strValue = String.valueOf(value);
56+
return Regex.isNumber(strValue) ? Long.parseLong(strValue) : defaultValue;
57+
}
58+
59+
/**
60+
* @param key the key whose associated value is to be returned
61+
* @return the value to which the specified key is mapped, or 0 if this map contains no mapping for the key
62+
*/
63+
public Double optDouble(K key){
64+
return optDouble(key, 0D);
65+
}
66+
67+
/**
68+
* @param key the key whose associated value is to be returned
69+
* @param defaultValue the default mapping of the key
70+
* @return the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key
71+
*/
72+
@SuppressWarnings("unchecked")
73+
public Double optDouble(K key, Double defaultValue){
74+
Object value = getOrDefault(key, (V) defaultValue);
75+
if(value instanceof Number){
76+
return ((Number) value).doubleValue();
77+
}
78+
String strValue = String.valueOf(value);
79+
return Regex.isNumber(strValue) ? Double.parseDouble(strValue) : defaultValue;
80+
}
81+
}
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)