From 04d52fafa040601759b034d3973dc1e190a59f1c Mon Sep 17 00:00:00 2001 From: javaquery Date: Mon, 18 Nov 2024 10:43:59 +0530 Subject: [PATCH 1/2] release: 1.2.6 - fix #15 --- .../com/javaquery/util/ExecutionContext.java | 30 ++---- src/main/java/com/javaquery/util/Is.java | 12 +++ .../util/collection/Collections.java | 12 +++ .../javaquery/util/http/CommonResponse.java | 12 +-- .../com/javaquery/util/time/DateRange.java | 5 + .../javaquery/util/TestExecutionContext.java | 99 ++++++++++--------- .../util/collection/TestCollections.java | 20 +++- .../util/http/TestCommonResponse.java | 6 +- .../javaquery/util/time/TestDateRange.java | 9 ++ 9 files changed, 122 insertions(+), 83 deletions(-) diff --git a/src/main/java/com/javaquery/util/ExecutionContext.java b/src/main/java/com/javaquery/util/ExecutionContext.java index d9e08ea..cb32032 100644 --- a/src/main/java/com/javaquery/util/ExecutionContext.java +++ b/src/main/java/com/javaquery/util/ExecutionContext.java @@ -46,6 +46,7 @@ public ExecutionContext() { public ExecutionContext(String requestId){ this.requestId = requestId; + this.meta = new HashMap<>(); this.createdAt = Dates.current(); } @@ -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() { diff --git a/src/main/java/com/javaquery/util/Is.java b/src/main/java/com/javaquery/util/Is.java index 65928a7..d789b92 100644 --- a/src/main/java/com/javaquery/util/Is.java +++ b/src/main/java/com/javaquery/util/Is.java @@ -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}. diff --git a/src/main/java/com/javaquery/util/collection/Collections.java b/src/main/java/com/javaquery/util/collection/Collections.java index 07cf5c1..a27608f 100644 --- a/src/main/java/com/javaquery/util/collection/Collections.java +++ b/src/main/java/com/javaquery/util/collection/Collections.java @@ -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}. diff --git a/src/main/java/com/javaquery/util/http/CommonResponse.java b/src/main/java/com/javaquery/util/http/CommonResponse.java index 37ed89b..9117ed1 100644 --- a/src/main/java/com/javaquery/util/http/CommonResponse.java +++ b/src/main/java/com/javaquery/util/http/CommonResponse.java @@ -25,8 +25,8 @@ public class CommonResponse implements Serializable { @JsonProperty("error_messages") private final List 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 errorMessages) { @@ -72,20 +72,20 @@ public List getErrorMessages() { return errorMessages; } - public Long getPage() { + public Integer getPage() { return page; } - public CommonResponse withPage(Long page){ + public CommonResponse withPage(Integer page){ this.page = page; return this; } - public Long getLimit() { + public Integer getLimit() { return limit; } - public CommonResponse withLimit(Long limit){ + public CommonResponse withLimit(Integer limit){ this.limit = limit; return this; } diff --git a/src/main/java/com/javaquery/util/time/DateRange.java b/src/main/java/com/javaquery/util/time/DateRange.java index 34aa3fe..20d71d5 100644 --- a/src/main/java/com/javaquery/util/time/DateRange.java +++ b/src/main/java/com/javaquery/util/time/DateRange.java @@ -1,5 +1,6 @@ package com.javaquery.util.time; +import java.time.temporal.ChronoUnit; import java.util.Date; /** @@ -29,4 +30,8 @@ public Date getStartDate() { public Date getEndDate() { return endDate; } + + public long days(){ + return ChronoUnit.DAYS.between(startDate.toInstant(), endDate.toInstant()); + } } diff --git a/src/test/java/com/javaquery/util/TestExecutionContext.java b/src/test/java/com/javaquery/util/TestExecutionContext.java index d638a92..34fb60e 100644 --- a/src/test/java/com/javaquery/util/TestExecutionContext.java +++ b/src/test/java/com/javaquery/util/TestExecutionContext.java @@ -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 @@ -44,15 +45,17 @@ 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 executionContext = new ExecutionContext<>(UniqueIdGenerator.generate()); - Assertions.assertNotNull(executionContext.getRequestId()); + assertNotNull(executionContext.getRequestId()); + assertNotNull(executionContext.getMeta()); + assertNotNull(executionContext.getCreatedAt()); } @Test @@ -60,83 +63,83 @@ public void constructorWithRequestIdReferenceIdAction(){ ExecutionContext 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 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 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 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 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 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 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()); } } diff --git a/src/test/java/com/javaquery/util/collection/TestCollections.java b/src/test/java/com/javaquery/util/collection/TestCollections.java index f82da37..cc9a6ec 100644 --- a/src/test/java/com/javaquery/util/collection/TestCollections.java +++ b/src/test/java/com/javaquery/util/collection/TestCollections.java @@ -87,19 +87,31 @@ 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)); @@ -107,7 +119,7 @@ public void test_nonNullNonEmptyMap() { } @Test - public void test_nonNullNonEmptyMap_ExecutableFunction() { + public void test_nonNullNotEmptyMap_ExecutableFunction() { Collections.nonNullNonEmpty(Collections.singletonMap("A", "B"), () -> Assertions.assertTrue(true)); } diff --git a/src/test/java/com/javaquery/util/http/TestCommonResponse.java b/src/test/java/com/javaquery/util/http/TestCommonResponse.java index 73c897d..ac0e2dc 100644 --- a/src/test/java/com/javaquery/util/http/TestCommonResponse.java +++ b/src/test/java/com/javaquery/util/http/TestCommonResponse.java @@ -48,11 +48,11 @@ public void ofWithStatusCodeErrorMessages(){ @Test public void okWithPaging(){ CommonResponse 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()); } } diff --git a/src/test/java/com/javaquery/util/time/TestDateRange.java b/src/test/java/com/javaquery/util/time/TestDateRange.java index b35e4df..41a6afe 100644 --- a/src/test/java/com/javaquery/util/time/TestDateRange.java +++ b/src/test/java/com/javaquery/util/time/TestDateRange.java @@ -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()); + } } From 2210522238540dde707825f3ee42fb3baa5784ac Mon Sep 17 00:00:00 2001 From: javaquery Date: Mon, 18 Nov 2024 10:46:17 +0530 Subject: [PATCH 2/2] release: 1.2.6 - update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e3936d8..957f8f1 100644 --- a/README.md +++ b/README.md @@ -49,12 +49,12 @@ framework and objects. com.javaquery util - 1.2.4 + 1.2.6 ``` # Gradle ``` -implementation 'com.javaquery:util:1.2.4' +implementation 'com.javaquery:util:1.2.6' ```