Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ framework and objects.
<dependency>
<groupId>com.javaquery</groupId>
<artifactId>util</artifactId>
<version>1.2.4</version>
<version>1.2.6</version>
</dependency>
```

# Gradle

```
implementation 'com.javaquery:util:1.2.4'
implementation 'com.javaquery:util:1.2.6'
```
30 changes: 8 additions & 22 deletions src/main/java/com/javaquery/util/ExecutionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public ExecutionContext() {

public ExecutionContext(String requestId){
this.requestId = requestId;
this.meta = new HashMap<>();
this.createdAt = Dates.current();
}

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

public ExecutionContext(T referenceId, Action action) {
this.requestId = UniqueIdGenerator.generate();
this.referenceId = referenceId;
this.action = action;
this.meta = new HashMap<>();
this.createdAt = Dates.current();
}

public ExecutionContext(Action action) {
this.requestId = UniqueIdGenerator.generate();
this.action = action;
this.meta = new HashMap<>();
this.createdAt = Dates.current();
this(UniqueIdGenerator.generate(), referenceId, action);
}

public ExecutionContext(T referenceId, Action action, Integer maxRetries) {
this.requestId = UniqueIdGenerator.generate();
this.referenceId = referenceId;
this.action = action;
this(referenceId, action);
this.maxRetries = maxRetries;
this.meta = new HashMap<>();
this.createdAt = Dates.current();
}

public ExecutionContext(Action action, Integer maxRetries) {
this.requestId = UniqueIdGenerator.generate();
this.action = action;
this.maxRetries = maxRetries;
this.meta = new HashMap<>();
this.createdAt = Dates.current();
this(null, action, maxRetries);
}

public ExecutionContext(Action action) {
this(action, 5);
}

public String getRequestId() {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/javaquery/util/Is.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ public static boolean isNull(Object obj) {
return Objects.isNull(obj);
}

/**
* Execute code if the provided reference is {@code null}.
*
* @param obj a reference to be checked against {@code null}
* @param executableFunction lambda function given executed if the provided reference is {@code null}.
*/
public static void isNull(Object obj, ExecutableFunction executableFunction){
if(isNull(obj)){
executableFunction.execute();
}
}

/**
* Returns {@code true} if the provided reference is non-{@code null} otherwise returns {@code
* false}.
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/javaquery/util/collection/Collections.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ public static void nonNullNonEmpty(Collection<?> collection, ExecutableFunction
}
}

/**
* Returns a Stream of the provided Collection [List, Set] if the provided Collection [List, Set] is
* non-{@code null} and non-empty otherwise returns an empty Stream.
*
* @param collection a Collection [List, Set] to be checked against non-{@code null} and non-empty
* @return a Stream of the provided Collection [List, Set] if the provided Collection [List, Set] is
* non-{@code null} and non-empty otherwise returns an empty Stream
*/
public static Stream<?> notEmpty(Collection<?> collection) {
return nonNullNonEmpty(collection) ? collection.stream() : Stream.empty();
}

/**
* Returns {@code true} if the provided Map is {@code null} or empty otherwise returns {@code
* false}.
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/javaquery/util/http/CommonResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class CommonResponse<T> implements Serializable {
@JsonProperty("error_messages")
private final List<String> errorMessages;

private Long page;
private Long limit;
private Integer page;
private Integer limit;
private Long total;

private CommonResponse(int statusCode, String message, T payload, List<String> errorMessages) {
Expand Down Expand Up @@ -72,20 +72,20 @@ public List<String> getErrorMessages() {
return errorMessages;
}

public Long getPage() {
public Integer getPage() {
return page;
}

public CommonResponse<T> withPage(Long page){
public CommonResponse<T> withPage(Integer page){
this.page = page;
return this;
}

public Long getLimit() {
public Integer getLimit() {
return limit;
}

public CommonResponse<T> withLimit(Long limit){
public CommonResponse<T> withLimit(Integer limit){
this.limit = limit;
return this;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/javaquery/util/time/DateRange.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.javaquery.util.time;

import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
Expand Down Expand Up @@ -29,4 +30,8 @@ public Date getStartDate() {
public Date getEndDate() {
return endDate;
}

public long days(){
return ChronoUnit.DAYS.between(startDate.toInstant(), endDate.toInstant());
}
}
99 changes: 51 additions & 48 deletions src/test/java/com/javaquery/util/TestExecutionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import com.javaquery.util.logging.Action;
import com.javaquery.util.logging.ActivityStatus;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author vicky.thakor
* @since 1.2.0
Expand Down Expand Up @@ -44,99 +45,101 @@ public void defaultConstructor(){
executionContext.setRequestId(UniqueIdGenerator.generate());

UserContext userContext = (UserContext) executionContext.getUserContext();
Assertions.assertEquals(50L, userContext.getUserId());
Assertions.assertNotNull(executionContext.getCreatedAt());
Assertions.assertNotNull(executionContext.getRequestId());
assertEquals(50L, userContext.getUserId());
assertNotNull(executionContext.getCreatedAt());
assertNotNull(executionContext.getRequestId());
}

@Test
public void constructorWithRequestId(){
ExecutionContext<Long, Long> executionContext = new ExecutionContext<>(UniqueIdGenerator.generate());
Assertions.assertNotNull(executionContext.getRequestId());
assertNotNull(executionContext.getRequestId());
assertNotNull(executionContext.getMeta());
assertNotNull(executionContext.getCreatedAt());
}

@Test
public void constructorWithRequestIdReferenceIdAction(){
ExecutionContext<Long, Long> executionContext = new ExecutionContext<>(UniqueIdGenerator.generate(), 1L, ExecutionContextAction.ONE);
executionContext.setUserContext(50L);
executionContext.setActivityStatus(ActivityStatus.STARTED);
Assertions.assertNotNull(executionContext.getRequestId());
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
Assertions.assertEquals(1L, executionContext.getReferenceId());
Assertions.assertNotNull(executionContext.getMeta());
Assertions.assertNotNull(executionContext.getCreatedAt());

Assertions.assertEquals(50L, executionContext.getUserContext());
Assertions.assertEquals(ActivityStatus.STARTED, executionContext.getActivityStatus());
Assertions.assertNotNull(executionContext.getCreatedAt());
assertNotNull(executionContext.getRequestId());
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
assertEquals(1L, executionContext.getReferenceId());
assertNotNull(executionContext.getMeta());
assertNotNull(executionContext.getCreatedAt());

assertEquals(50L, executionContext.getUserContext());
assertEquals(ActivityStatus.STARTED, executionContext.getActivityStatus());
assertNotNull(executionContext.getCreatedAt());
}

@Test
public void constructorWithReferenceIdAction(){
ExecutionContext<String, Void> executionContext = new ExecutionContext<>("test", ExecutionContextAction.ONE);
Assertions.assertNotNull(executionContext.getRequestId());
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
Assertions.assertEquals("test", executionContext.getReferenceId());
Assertions.assertNotNull(executionContext.getMeta());
Assertions.assertNotNull(executionContext.getCreatedAt());
assertNotNull(executionContext.getRequestId());
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
assertEquals("test", executionContext.getReferenceId());
assertNotNull(executionContext.getMeta());
assertNotNull(executionContext.getCreatedAt());
}

@Test
public void constructorWithAction(){
ExecutionContext<String, Void> executionContext = new ExecutionContext<>(ExecutionContextAction.ONE);
Assertions.assertNotNull(executionContext.getRequestId());
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
Assertions.assertNull(executionContext.getReferenceId());
Assertions.assertNotNull(executionContext.getMeta());
Assertions.assertNotNull(executionContext.getCreatedAt());
assertNotNull(executionContext.getRequestId());
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
assertNull(executionContext.getReferenceId());
assertNotNull(executionContext.getMeta());
assertNotNull(executionContext.getCreatedAt());
}

@Test
public void constructorWithActionAndMeta(){
ExecutionContext<String, Void> executionContext = new ExecutionContext<>(ExecutionContextAction.ONE);
executionContext.addMeta("key", "value");
Assertions.assertNotNull(executionContext.getRequestId());
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
Assertions.assertNull(executionContext.getReferenceId());
Assertions.assertEquals("value", executionContext.getMeta("key", null));
Assertions.assertNotNull(executionContext.getCreatedAt());
assertNotNull(executionContext.getRequestId());
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
assertNull(executionContext.getReferenceId());
assertEquals("value", executionContext.getMeta("key", null));
assertNotNull(executionContext.getCreatedAt());

/* set meta */
executionContext.setMeta(new HashMap<>());
Assertions.assertNull(executionContext.getMeta("key", null));
assertNull(executionContext.getMeta("key", null));
}

@Test
public void constructorWithActionAndRetriesAttempted(){
ExecutionContext<String, Void> executionContext = new ExecutionContext<>(ExecutionContextAction.ONE);
executionContext.addRetriesAttempted(1);
Assertions.assertNotNull(executionContext.getRequestId());
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
Assertions.assertNull(executionContext.getReferenceId());
Assertions.assertNotNull(executionContext.getMeta());
Assertions.assertEquals(1, executionContext.getRetriesAttempted());
Assertions.assertNotNull(executionContext.getCreatedAt());
assertNotNull(executionContext.getRequestId());
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
assertNull(executionContext.getReferenceId());
assertNotNull(executionContext.getMeta());
assertEquals(1, executionContext.getRetriesAttempted());
assertNotNull(executionContext.getCreatedAt());
}

@Test
public void constructorWithReferenceIdActionMaxRetries(){
ExecutionContext<Long, Void> executionContext = new ExecutionContext<>(1L, ExecutionContextAction.ONE, 3);
Assertions.assertNotNull(executionContext.getRequestId());
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
Assertions.assertEquals(1L, executionContext.getReferenceId());
Assertions.assertEquals(3, executionContext.getMaxRetries());
Assertions.assertNotNull(executionContext.getMeta());
Assertions.assertNotNull(executionContext.getCreatedAt());
assertNotNull(executionContext.getRequestId());
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
assertEquals(1L, executionContext.getReferenceId());
assertEquals(3, executionContext.getMaxRetries());
assertNotNull(executionContext.getMeta());
assertNotNull(executionContext.getCreatedAt());
}

@Test
public void constructorWithActionMaxRetries(){
ExecutionContext<Long, Void> executionContext = new ExecutionContext<>(ExecutionContextAction.ONE, 3);
Assertions.assertNotNull(executionContext.getRequestId());
Assertions.assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
Assertions.assertNull(executionContext.getReferenceId());
Assertions.assertEquals(3, executionContext.getMaxRetries());
Assertions.assertNotNull(executionContext.getMeta());
Assertions.assertNotNull(executionContext.getCreatedAt());
assertNotNull(executionContext.getRequestId());
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
assertNull(executionContext.getReferenceId());
assertEquals(3, executionContext.getMaxRetries());
assertNotNull(executionContext.getMeta());
assertNotNull(executionContext.getCreatedAt());
}
}
20 changes: 16 additions & 4 deletions src/test/java/com/javaquery/util/collection/TestCollections.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,39 @@ public void test_nullOrEmpty_2() {
}

@Test
public void test_nonNullNonEmpty() {
public void test_nonNullNotEmpty() {
Assertions.assertTrue(Collections.nonNullNonEmpty(Collections.singletonList("A")));
Assertions.assertFalse(Collections.nonNullNonEmpty(NULL_LIST));
Assertions.assertFalse(Collections.nonNullNonEmpty(EMPTY_SET));
}

@Test
public void test_nonNullNonEmpty_ExecutableFunction(){
public void test_nonNullNotEmpty_ExecutableFunction(){
Collections.nonNullNonEmpty(Collections.singletonList("A"), () -> Assertions.assertTrue(true));
}

@Test
public void test_nonNullNonEmptyMap() {
public void test_notEmptyStream(){
Collections.notEmpty(Collections.singletonList("A"))
.forEach(Assertions::assertNotNull);
}

@Test
public void test_notEmptyStream_emptyCollection(){
Stream<?> stream = Collections.notEmpty(EMPTY_LIST);
Assertions.assertEquals(0, stream.count());
}

@Test
public void test_nonNullNotEmptyMap() {
Assertions.assertTrue(Collections.nonNullNonEmpty(Collections.singletonMap("A", "B")));

Assertions.assertFalse(Collections.nonNullNonEmpty(NULL_MAP));
Assertions.assertFalse(Collections.nonNullNonEmpty(EMPTY_MAP));
}

@Test
public void test_nonNullNonEmptyMap_ExecutableFunction() {
public void test_nonNullNotEmptyMap_ExecutableFunction() {
Collections.nonNullNonEmpty(Collections.singletonMap("A", "B"), () -> Assertions.assertTrue(true));
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/javaquery/util/http/TestCommonResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public void ofWithStatusCodeErrorMessages(){
@Test
public void okWithPaging(){
CommonResponse<Long> commonResponse = CommonResponse.of(HttpStatus.CREATED, 1L);
commonResponse.withPage(1L).withLimit(10L).withTotal(100L);
commonResponse.withPage(1).withLimit(10).withTotal(100L);
Assertions.assertEquals(HttpStatus.CREATED.value(), commonResponse.getStatusCode());
Assertions.assertEquals(1L, commonResponse.getPayload());
Assertions.assertEquals(1L, commonResponse.getPage());
Assertions.assertEquals(10L, commonResponse.getLimit());
Assertions.assertEquals(1, commonResponse.getPage());
Assertions.assertEquals(10, commonResponse.getLimit());
Assertions.assertEquals(100L, commonResponse.getTotal());
}
}
9 changes: 9 additions & 0 deletions src/test/java/com/javaquery/util/time/TestDateRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ public void test_constructor_1() {
Assertions.assertEquals(startDate, dateRange.getStartDate());
Assertions.assertNotNull(dateRange.getEndDate());
}

@Test
public void test_days(){
Date startDate = Dates.getDate(2024, 11, 1);
Date endDate = Dates.getDate(2024, 11, 18);

DateRange dateRange = new DateRange(startDate, endDate);
Assertions.assertEquals(17, dateRange.days());
}
}