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.6</version>
<version>1.2.7</version>
</dependency>
```

# Gradle

```
implementation 'com.javaquery:util:1.2.6'
implementation 'com.javaquery:util:1.2.7'
```
13 changes: 7 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ plugins {
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
group 'com.javaquery'
archivesBaseName = 'util'
version '1.2.6'
version '1.2.7'

repositories {
mavenCentral()
}

dependencies {
implementation('org.slf4j:slf4j-api:2.0.11')
implementation('org.json:json:20231013')
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.16.1'
implementation 'org.slf4j:slf4j-api:2.0.16'
implementation 'org.json:json:20250107'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.18.2'

testImplementation 'net.logstash.logback:logstash-logback-encoder:7.4'
testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.3.14'
testImplementation('org.junit.jupiter:junit-jupiter:5.7.0')
testImplementation 'ch.qos.logback:logback-classic:1.3.15'
testImplementation 'org.junit.jupiter:junit-jupiter:5.11.4'
}

test {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/javaquery/util/ExecutionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class ExecutionContext<T, V> {

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

public ExecutionContext(String requestId){
Expand Down Expand Up @@ -123,6 +124,10 @@ public Object getMeta(String key, Object defaultValue){
return meta.getOrDefault(key, defaultValue);
}

public String optString(String key, String defaultValue){
return String.valueOf(getMeta(key, defaultValue));
}

public void addMeta(String key, Object value){
this.meta.put(key, value);
}
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 @@ -47,6 +47,18 @@ public static boolean nonNull(Object obj) {
return Objects.nonNull(obj);
}

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

/**
* Returns {@code true} if the provided String is {@code null} or empty otherwise returns {@code
* false}.
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/javaquery/util/collection/JList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.javaquery.util.collection;

import java.util.Arrays;
import java.util.List;

/**
* @author javaquery
* @since 1.2.7
*/
public class JList {

@SafeVarargs
public static <E> List<E> of(E... elements) {
return Arrays.asList(elements);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/javaquery/util/collection/JSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.javaquery.util.collection;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
* @author javaquery
* @since 1.2.7
*/
public class JSet {
public static <E> Set<E> of(E... elements) {
return new HashSet<>(Arrays.asList(elements));
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/javaquery/util/number/Numbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class Numbers {

/**
* Round the decimal number.
* example: roundDecimal(10.123456789, 2) => 10.12
* example: roundDecimal(10.123456789, 4) => 10.1235
* example: roundDecimal(10.576, 2) => 10.58
* example: roundDecimal(10.123456789, 2) = 10.12
* example: roundDecimal(10.123456789, 4) = 10.1235
* example: roundDecimal(10.576, 2) = 10.58
*
* @param number number to round
* @param decimalPlaces decimal places to round
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/javaquery/util/TestExecutionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void defaultConstructor(){
executionContext.setRequestId(UniqueIdGenerator.generate());

UserContext userContext = (UserContext) executionContext.getUserContext();
assertNotNull(executionContext.getMeta());
assertEquals(50L, userContext.getUserId());
assertNotNull(executionContext.getCreatedAt());
assertNotNull(executionContext.getRequestId());
Expand Down Expand Up @@ -102,6 +103,7 @@ public void constructorWithActionAndMeta(){
assertEquals(ExecutionContextAction.ONE, executionContext.getAction());
assertNull(executionContext.getReferenceId());
assertEquals("value", executionContext.getMeta("key", null));
assertEquals("not found", executionContext.optString("key2", "not found"));
assertNotNull(executionContext.getCreatedAt());

/* set meta */
Expand Down Expand Up @@ -142,4 +144,15 @@ public void constructorWithActionMaxRetries(){
assertNotNull(executionContext.getMeta());
assertNotNull(executionContext.getCreatedAt());
}

@Test
public void metaDataTest(){
ExecutionContext<String, Void> executionContext = new ExecutionContext<>();
executionContext.addMeta("key", "value");
assertEquals("value", executionContext.getMeta("key", null));
assertEquals("value", executionContext.optString("key", null));
assertEquals("not found", executionContext.optString("key2", "not found"));
executionContext.addMeta("key", "value2");
assertEquals("value2", executionContext.getMeta("key", null));
}
}
24 changes: 24 additions & 0 deletions src/test/java/com/javaquery/util/collection/TestJList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.javaquery.util.collection;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author javaquery
* @since 2025-01-20
*/
public class TestJList {

@Test
public void test_of(){
List<String> list = JList.of("a", "b", "c");
assertEquals(3, list.size());
assertTrue(list.contains("a"));
assertTrue(list.contains("b"));
assertTrue(list.contains("c"));
}
}
24 changes: 24 additions & 0 deletions src/test/java/com/javaquery/util/collection/TestJSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.javaquery.util.collection;

import org.junit.jupiter.api.Test;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author javaquery
* @since 2025-01-20
*/
public class TestJSet {

@Test
public void test_of(){
Set<String> set = JSet.of("a", "b", "c");
assertEquals(3, set.size());
assertTrue(set.contains("a"));
assertTrue(set.contains("b"));
assertTrue(set.contains("c"));
}
}