Skip to content

Commit 066b051

Browse files
authored
release: 1.2.6 (#16)
* release: 1.2.6 - fix #15 * release: 1.2.6 - update README.md
1 parent de6b476 commit 066b051

File tree

10 files changed

+124
-85
lines changed

10 files changed

+124
-85
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ framework and objects.
4949
<dependency>
5050
<groupId>com.javaquery</groupId>
5151
<artifactId>util</artifactId>
52-
<version>1.2.4</version>
52+
<version>1.2.6</version>
5353
</dependency>
5454
```
5555

5656
# Gradle
5757

5858
```
59-
implementation 'com.javaquery:util:1.2.4'
59+
implementation 'com.javaquery:util:1.2.6'
6060
```

src/main/java/com/javaquery/util/ExecutionContext.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public ExecutionContext() {
4646

4747
public ExecutionContext(String requestId){
4848
this.requestId = requestId;
49+
this.meta = new HashMap<>();
4950
this.createdAt = Dates.current();
5051
}
5152

@@ -58,35 +59,20 @@ public ExecutionContext(String requestId, T referenceId, Action action) {
5859
}
5960

6061
public ExecutionContext(T referenceId, Action action) {
61-
this.requestId = UniqueIdGenerator.generate();
62-
this.referenceId = referenceId;
63-
this.action = action;
64-
this.meta = new HashMap<>();
65-
this.createdAt = Dates.current();
66-
}
67-
68-
public ExecutionContext(Action action) {
69-
this.requestId = UniqueIdGenerator.generate();
70-
this.action = action;
71-
this.meta = new HashMap<>();
72-
this.createdAt = Dates.current();
62+
this(UniqueIdGenerator.generate(), referenceId, action);
7363
}
7464

7565
public ExecutionContext(T referenceId, Action action, Integer maxRetries) {
76-
this.requestId = UniqueIdGenerator.generate();
77-
this.referenceId = referenceId;
78-
this.action = action;
66+
this(referenceId, action);
7967
this.maxRetries = maxRetries;
80-
this.meta = new HashMap<>();
81-
this.createdAt = Dates.current();
8268
}
8369

8470
public ExecutionContext(Action action, Integer maxRetries) {
85-
this.requestId = UniqueIdGenerator.generate();
86-
this.action = action;
87-
this.maxRetries = maxRetries;
88-
this.meta = new HashMap<>();
89-
this.createdAt = Dates.current();
71+
this(null, action, maxRetries);
72+
}
73+
74+
public ExecutionContext(Action action) {
75+
this(action, 5);
9076
}
9177

9278
public String getRequestId() {

src/main/java/com/javaquery/util/Is.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ public static boolean isNull(Object obj) {
2424
return Objects.isNull(obj);
2525
}
2626

27+
/**
28+
* Execute code if the provided reference is {@code null}.
29+
*
30+
* @param obj a reference to be checked against {@code null}
31+
* @param executableFunction lambda function given executed if the provided reference is {@code null}.
32+
*/
33+
public static void isNull(Object obj, ExecutableFunction executableFunction){
34+
if(isNull(obj)){
35+
executableFunction.execute();
36+
}
37+
}
38+
2739
/**
2840
* Returns {@code true} if the provided reference is non-{@code null} otherwise returns {@code
2941
* false}.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ public static void nonNullNonEmpty(Collection<?> collection, ExecutableFunction
134134
}
135135
}
136136

137+
/**
138+
* Returns a Stream of the provided Collection [List, Set] if the provided Collection [List, Set] is
139+
* non-{@code null} and non-empty otherwise returns an empty Stream.
140+
*
141+
* @param collection a Collection [List, Set] to be checked against non-{@code null} and non-empty
142+
* @return a Stream of the provided Collection [List, Set] if the provided Collection [List, Set] is
143+
* non-{@code null} and non-empty otherwise returns an empty Stream
144+
*/
145+
public static Stream<?> notEmpty(Collection<?> collection) {
146+
return nonNullNonEmpty(collection) ? collection.stream() : Stream.empty();
147+
}
148+
137149
/**
138150
* Returns {@code true} if the provided Map is {@code null} or empty otherwise returns {@code
139151
* false}.

src/main/java/com/javaquery/util/http/CommonResponse.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public class CommonResponse<T> implements Serializable {
2525
@JsonProperty("error_messages")
2626
private final List<String> errorMessages;
2727

28-
private Long page;
29-
private Long limit;
28+
private Integer page;
29+
private Integer limit;
3030
private Long total;
3131

3232
private CommonResponse(int statusCode, String message, T payload, List<String> errorMessages) {
@@ -72,20 +72,20 @@ public List<String> getErrorMessages() {
7272
return errorMessages;
7373
}
7474

75-
public Long getPage() {
75+
public Integer getPage() {
7676
return page;
7777
}
7878

79-
public CommonResponse<T> withPage(Long page){
79+
public CommonResponse<T> withPage(Integer page){
8080
this.page = page;
8181
return this;
8282
}
8383

84-
public Long getLimit() {
84+
public Integer getLimit() {
8585
return limit;
8686
}
8787

88-
public CommonResponse<T> withLimit(Long limit){
88+
public CommonResponse<T> withLimit(Integer limit){
8989
this.limit = limit;
9090
return this;
9191
}

src/main/java/com/javaquery/util/time/DateRange.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.javaquery.util.time;
22

3+
import java.time.temporal.ChronoUnit;
34
import java.util.Date;
45

56
/**
@@ -29,4 +30,8 @@ public Date getStartDate() {
2930
public Date getEndDate() {
3031
return endDate;
3132
}
33+
34+
public long days(){
35+
return ChronoUnit.DAYS.between(startDate.toInstant(), endDate.toInstant());
36+
}
3237
}

src/test/java/com/javaquery/util/TestExecutionContext.java

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import com.javaquery.util.logging.Action;
44
import com.javaquery.util.logging.ActivityStatus;
5-
import org.junit.jupiter.api.Assertions;
65
import org.junit.jupiter.api.Test;
76

87
import java.util.HashMap;
98

9+
import static org.junit.jupiter.api.Assertions.*;
10+
1011
/**
1112
* @author vicky.thakor
1213
* @since 1.2.0
@@ -44,99 +45,101 @@ public void defaultConstructor(){
4445
executionContext.setRequestId(UniqueIdGenerator.generate());
4546

4647
UserContext userContext = (UserContext) executionContext.getUserContext();
47-
Assertions.assertEquals(50L, userContext.getUserId());
48-
Assertions.assertNotNull(executionContext.getCreatedAt());
49-
Assertions.assertNotNull(executionContext.getRequestId());
48+
assertEquals(50L, userContext.getUserId());
49+
assertNotNull(executionContext.getCreatedAt());
50+
assertNotNull(executionContext.getRequestId());
5051
}
5152

5253
@Test
5354
public void constructorWithRequestId(){
5455
ExecutionContext<Long, Long> executionContext = new ExecutionContext<>(UniqueIdGenerator.generate());
55-
Assertions.assertNotNull(executionContext.getRequestId());
56+
assertNotNull(executionContext.getRequestId());
57+
assertNotNull(executionContext.getMeta());
58+
assertNotNull(executionContext.getCreatedAt());
5659
}
5760

5861
@Test
5962
public void constructorWithRequestIdReferenceIdAction(){
6063
ExecutionContext<Long, Long> executionContext = new ExecutionContext<>(UniqueIdGenerator.generate(), 1L, ExecutionContextAction.ONE);
6164
executionContext.setUserContext(50L);
6265
executionContext.setActivityStatus(ActivityStatus.STARTED);
63-
Assertions.assertNotNull(executionContext.getRequestId());
64-
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
65-
Assertions.assertEquals(1L, executionContext.getReferenceId());
66-
Assertions.assertNotNull(executionContext.getMeta());
67-
Assertions.assertNotNull(executionContext.getCreatedAt());
68-
69-
Assertions.assertEquals(50L, executionContext.getUserContext());
70-
Assertions.assertEquals(ActivityStatus.STARTED, executionContext.getActivityStatus());
71-
Assertions.assertNotNull(executionContext.getCreatedAt());
66+
assertNotNull(executionContext.getRequestId());
67+
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
68+
assertEquals(1L, executionContext.getReferenceId());
69+
assertNotNull(executionContext.getMeta());
70+
assertNotNull(executionContext.getCreatedAt());
71+
72+
assertEquals(50L, executionContext.getUserContext());
73+
assertEquals(ActivityStatus.STARTED, executionContext.getActivityStatus());
74+
assertNotNull(executionContext.getCreatedAt());
7275
}
7376

7477
@Test
7578
public void constructorWithReferenceIdAction(){
7679
ExecutionContext<String, Void> executionContext = new ExecutionContext<>("test", ExecutionContextAction.ONE);
77-
Assertions.assertNotNull(executionContext.getRequestId());
78-
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
79-
Assertions.assertEquals("test", executionContext.getReferenceId());
80-
Assertions.assertNotNull(executionContext.getMeta());
81-
Assertions.assertNotNull(executionContext.getCreatedAt());
80+
assertNotNull(executionContext.getRequestId());
81+
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
82+
assertEquals("test", executionContext.getReferenceId());
83+
assertNotNull(executionContext.getMeta());
84+
assertNotNull(executionContext.getCreatedAt());
8285
}
8386

8487
@Test
8588
public void constructorWithAction(){
8689
ExecutionContext<String, Void> executionContext = new ExecutionContext<>(ExecutionContextAction.ONE);
87-
Assertions.assertNotNull(executionContext.getRequestId());
88-
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
89-
Assertions.assertNull(executionContext.getReferenceId());
90-
Assertions.assertNotNull(executionContext.getMeta());
91-
Assertions.assertNotNull(executionContext.getCreatedAt());
90+
assertNotNull(executionContext.getRequestId());
91+
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
92+
assertNull(executionContext.getReferenceId());
93+
assertNotNull(executionContext.getMeta());
94+
assertNotNull(executionContext.getCreatedAt());
9295
}
9396

9497
@Test
9598
public void constructorWithActionAndMeta(){
9699
ExecutionContext<String, Void> executionContext = new ExecutionContext<>(ExecutionContextAction.ONE);
97100
executionContext.addMeta("key", "value");
98-
Assertions.assertNotNull(executionContext.getRequestId());
99-
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
100-
Assertions.assertNull(executionContext.getReferenceId());
101-
Assertions.assertEquals("value", executionContext.getMeta("key", null));
102-
Assertions.assertNotNull(executionContext.getCreatedAt());
101+
assertNotNull(executionContext.getRequestId());
102+
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
103+
assertNull(executionContext.getReferenceId());
104+
assertEquals("value", executionContext.getMeta("key", null));
105+
assertNotNull(executionContext.getCreatedAt());
103106

104107
/* set meta */
105108
executionContext.setMeta(new HashMap<>());
106-
Assertions.assertNull(executionContext.getMeta("key", null));
109+
assertNull(executionContext.getMeta("key", null));
107110
}
108111

109112
@Test
110113
public void constructorWithActionAndRetriesAttempted(){
111114
ExecutionContext<String, Void> executionContext = new ExecutionContext<>(ExecutionContextAction.ONE);
112115
executionContext.addRetriesAttempted(1);
113-
Assertions.assertNotNull(executionContext.getRequestId());
114-
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
115-
Assertions.assertNull(executionContext.getReferenceId());
116-
Assertions.assertNotNull(executionContext.getMeta());
117-
Assertions.assertEquals(1, executionContext.getRetriesAttempted());
118-
Assertions.assertNotNull(executionContext.getCreatedAt());
116+
assertNotNull(executionContext.getRequestId());
117+
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
118+
assertNull(executionContext.getReferenceId());
119+
assertNotNull(executionContext.getMeta());
120+
assertEquals(1, executionContext.getRetriesAttempted());
121+
assertNotNull(executionContext.getCreatedAt());
119122
}
120123

121124
@Test
122125
public void constructorWithReferenceIdActionMaxRetries(){
123126
ExecutionContext<Long, Void> executionContext = new ExecutionContext<>(1L, ExecutionContextAction.ONE, 3);
124-
Assertions.assertNotNull(executionContext.getRequestId());
125-
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
126-
Assertions.assertEquals(1L, executionContext.getReferenceId());
127-
Assertions.assertEquals(3, executionContext.getMaxRetries());
128-
Assertions.assertNotNull(executionContext.getMeta());
129-
Assertions.assertNotNull(executionContext.getCreatedAt());
127+
assertNotNull(executionContext.getRequestId());
128+
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
129+
assertEquals(1L, executionContext.getReferenceId());
130+
assertEquals(3, executionContext.getMaxRetries());
131+
assertNotNull(executionContext.getMeta());
132+
assertNotNull(executionContext.getCreatedAt());
130133
}
131134

132135
@Test
133136
public void constructorWithActionMaxRetries(){
134137
ExecutionContext<Long, Void> executionContext = new ExecutionContext<>(ExecutionContextAction.ONE, 3);
135-
Assertions.assertNotNull(executionContext.getRequestId());
136-
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
137-
Assertions.assertNull(executionContext.getReferenceId());
138-
Assertions.assertEquals(3, executionContext.getMaxRetries());
139-
Assertions.assertNotNull(executionContext.getMeta());
140-
Assertions.assertNotNull(executionContext.getCreatedAt());
138+
assertNotNull(executionContext.getRequestId());
139+
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
140+
assertNull(executionContext.getReferenceId());
141+
assertEquals(3, executionContext.getMaxRetries());
142+
assertNotNull(executionContext.getMeta());
143+
assertNotNull(executionContext.getCreatedAt());
141144
}
142145
}

src/test/java/com/javaquery/util/collection/TestCollections.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,39 @@ public void test_nullOrEmpty_2() {
8787
}
8888

8989
@Test
90-
public void test_nonNullNonEmpty() {
90+
public void test_nonNullNotEmpty() {
9191
Assertions.assertTrue(Collections.nonNullNonEmpty(Collections.singletonList("A")));
9292
Assertions.assertFalse(Collections.nonNullNonEmpty(NULL_LIST));
9393
Assertions.assertFalse(Collections.nonNullNonEmpty(EMPTY_SET));
9494
}
9595

9696
@Test
97-
public void test_nonNullNonEmpty_ExecutableFunction(){
97+
public void test_nonNullNotEmpty_ExecutableFunction(){
9898
Collections.nonNullNonEmpty(Collections.singletonList("A"), () -> Assertions.assertTrue(true));
9999
}
100100

101101
@Test
102-
public void test_nonNullNonEmptyMap() {
102+
public void test_notEmptyStream(){
103+
Collections.notEmpty(Collections.singletonList("A"))
104+
.forEach(Assertions::assertNotNull);
105+
}
106+
107+
@Test
108+
public void test_notEmptyStream_emptyCollection(){
109+
Stream<?> stream = Collections.notEmpty(EMPTY_LIST);
110+
Assertions.assertEquals(0, stream.count());
111+
}
112+
113+
@Test
114+
public void test_nonNullNotEmptyMap() {
103115
Assertions.assertTrue(Collections.nonNullNonEmpty(Collections.singletonMap("A", "B")));
104116

105117
Assertions.assertFalse(Collections.nonNullNonEmpty(NULL_MAP));
106118
Assertions.assertFalse(Collections.nonNullNonEmpty(EMPTY_MAP));
107119
}
108120

109121
@Test
110-
public void test_nonNullNonEmptyMap_ExecutableFunction() {
122+
public void test_nonNullNotEmptyMap_ExecutableFunction() {
111123
Collections.nonNullNonEmpty(Collections.singletonMap("A", "B"), () -> Assertions.assertTrue(true));
112124
}
113125

src/test/java/com/javaquery/util/http/TestCommonResponse.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public void ofWithStatusCodeErrorMessages(){
4848
@Test
4949
public void okWithPaging(){
5050
CommonResponse<Long> commonResponse = CommonResponse.of(HttpStatus.CREATED, 1L);
51-
commonResponse.withPage(1L).withLimit(10L).withTotal(100L);
51+
commonResponse.withPage(1).withLimit(10).withTotal(100L);
5252
Assertions.assertEquals(HttpStatus.CREATED.value(), commonResponse.getStatusCode());
5353
Assertions.assertEquals(1L, commonResponse.getPayload());
54-
Assertions.assertEquals(1L, commonResponse.getPage());
55-
Assertions.assertEquals(10L, commonResponse.getLimit());
54+
Assertions.assertEquals(1, commonResponse.getPage());
55+
Assertions.assertEquals(10, commonResponse.getLimit());
5656
Assertions.assertEquals(100L, commonResponse.getTotal());
5757
}
5858
}

src/test/java/com/javaquery/util/time/TestDateRange.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ public void test_constructor_1() {
2929
Assertions.assertEquals(startDate, dateRange.getStartDate());
3030
Assertions.assertNotNull(dateRange.getEndDate());
3131
}
32+
33+
@Test
34+
public void test_days(){
35+
Date startDate = Dates.getDate(2024, 11, 1);
36+
Date endDate = Dates.getDate(2024, 11, 18);
37+
38+
DateRange dateRange = new DateRange(startDate, endDate);
39+
Assertions.assertEquals(17, dateRange.days());
40+
}
3241
}

0 commit comments

Comments
 (0)