Skip to content

Commit de6b476

Browse files
committed
release 1.2.5
- fix #14
1 parent f3e8fa8 commit de6b476

File tree

15 files changed

+337
-21
lines changed

15 files changed

+337
-21
lines changed

build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ plugins {
66

77
sourceCompatibility = 1.8
88
group 'com.javaquery'
9-
version '1.2.4'
9+
archivesBaseName = 'util'
10+
version '1.2.5'
1011

1112
repositories {
1213
mavenCentral()
@@ -85,8 +86,8 @@ publishing {
8586
def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots"
8687
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
8788
credentials {
88-
username "javaquery"
89-
password "a2a@\$ASDF\$777"
89+
username "$mavenCentralUsername"
90+
password "$mavenCentralPassword"
9091
}
9192
}
9293
}

gradle.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
signing.keyId=******
22
signing.password=******
3-
signing.secretKeyRingFile=******
3+
signing.secretKeyRingFile=******
4+
5+
mavenCentralUsername=****
6+
mavenCentralPassword=****
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package com.javaquery.util;
2+
3+
import com.javaquery.util.collection.Collections;
4+
import com.javaquery.util.collection.function.ExecutableFunction;
5+
import com.javaquery.util.string.Strings;
6+
7+
import java.util.Collection;
8+
import java.util.Map;
9+
10+
/**
11+
* Wrapper class for {@link Objects}, {@link Strings} and {@link Collections} utility classes.
12+
* @author javaquery
13+
* @since 1.2.5
14+
*/
15+
public class Is {
16+
17+
/**
18+
* Returns {@code true} if the provided reference is {@code null} otherwise returns {@code false}.
19+
*
20+
* @param obj a reference to be checked against {@code null}
21+
* @return {@code true} if the provided reference is {@code null} otherwise {@code false}
22+
*/
23+
public static boolean isNull(Object obj) {
24+
return Objects.isNull(obj);
25+
}
26+
27+
/**
28+
* Returns {@code true} if the provided reference is non-{@code null} otherwise returns {@code
29+
* false}.
30+
*
31+
* @param obj a reference to be checked against {@code null}
32+
* @return {@code true} if the provided reference is non-{@code null} otherwise {@code false}
33+
*/
34+
public static boolean nonNull(Object obj) {
35+
return Objects.nonNull(obj);
36+
}
37+
38+
/**
39+
* Returns {@code true} if the provided String is {@code null} or empty otherwise returns {@code
40+
* false}.
41+
*
42+
* @param str a String to be checked against {@code null} or empty
43+
* @return {@code true} if the provided String is {@code null} or empty otherwise {@code false}
44+
*/
45+
public static boolean nullOrEmpty(String str) {
46+
return Strings.nullOrEmpty(str);
47+
}
48+
49+
/**
50+
* Execute code if the provided String is {@code null} or empty.
51+
* @param str a String to be checked against {@code null} or empty
52+
* @param executableFunction lambda function given executed if the provided String is {@code null} or empty.
53+
*/
54+
public static void nullOrEmpty(String str, ExecutableFunction executableFunction){
55+
Strings.nullOrEmpty(str, executableFunction);
56+
}
57+
58+
/**
59+
* Checks if the provided String is {@code null} or empty.
60+
* @param str a String to be checked against {@code null} or empty
61+
* @param defaultValue - if the provided String is {@code null} or empty then this value will be returned.
62+
* @return provided String if non-{@code null} and non-empty otherwise defaultValue
63+
*/
64+
public static String nullOrEmpty(String str, String defaultValue){
65+
return Strings.nullOrEmpty(str, defaultValue);
66+
}
67+
68+
/**
69+
* Returns {@code true} if the provided String is non-{@code null} and non-empty otherwise returns
70+
* {@code false}.
71+
*
72+
* @param str a String to be checked against non-{@code null} and non-empty
73+
* @return {@code true} if the provided String is non-{@code null} and non-empty otherwise {@code
74+
* false}
75+
*/
76+
public static boolean nonNullNonEmpty(String str) {
77+
return Strings.nonNullNonEmpty(str);
78+
}
79+
80+
/**
81+
* Execute code if the provided String is non-{@code null} and non-empty.
82+
* @param str a String to be checked against non-{@code null} and non-empty
83+
* @param executableFunction lambda function given executed if the provided String is non-{@code null} and non-empty.
84+
*/
85+
public static void nonNullNonEmpty(String str, ExecutableFunction executableFunction) {
86+
Strings.nonNullNonEmpty(str, executableFunction);
87+
}
88+
89+
/**
90+
* Checks if the provided String is non-{@code null} and non-empty.
91+
* @param str a String to be checked against non-{@code null} and non-empty
92+
* @param defaultValue - if the provided String is non-{@code null} and non-empty then this value will be returned.
93+
* @return provided String if non-{@code null} and non-empty otherwise defaultValue
94+
*/
95+
public static String nonNullNonEmpty(String str, String defaultValue){
96+
return Strings.nonNullNonEmpty(str, defaultValue);
97+
}
98+
99+
/**
100+
* Returns {@code true} if the provided Collection [List, Set] is {@code null} or empty otherwise
101+
* returns {@code false}.
102+
*
103+
* @param collection a Collection [List, Set] to be checked against {@code null} or empty
104+
* @return {@code true} if the provided Collection [List, Set] is {@code null} or empty otherwise
105+
* {@code false}
106+
*/
107+
public static boolean nullOrEmpty(Collection<?> collection) {
108+
return Collections.nullOrEmpty(collection);
109+
}
110+
111+
/**
112+
* Execute code if the provided Collection [List, Set] is {@code null} or empty.
113+
* @param collection a Collection [List, Set] to be checked against {@code null} or empty
114+
* @param executableFunction lambda function given executed if the provided Collection [List, Set] is {@code null} or empty.
115+
*/
116+
public static void nullOrEmpty(Collection<?> collection, ExecutableFunction executableFunction) {
117+
Collections.nullOrEmpty(collection, executableFunction);
118+
}
119+
120+
/**
121+
* Returns {@code true} if the provided Collection [List, Set] is non-{@code null} and non-empty
122+
* otherwise returns {@code false}.
123+
*
124+
* @param collection a Collection [List, Set] to be checked against non-{@code null} and non-empty
125+
* @return {@code true} if the provided Collection [List, Set] is non-{@code null} and non-empty
126+
* otherwise {@code false}
127+
*/
128+
public static boolean nonNullNonEmpty(Collection<?> collection) {
129+
return Collections.nonNullNonEmpty(collection);
130+
}
131+
132+
/**
133+
* Execute code if the provided Collection [List, Set] is non-{@code null} and non-empty.
134+
* @param collection collection a Collection [List, Set] to be checked against non-{@code null} and non-empty
135+
* @param executableFunction lambda function given executed if the provided Collection [List, Set] is non-{@code null} and non-empty.
136+
*/
137+
public static void nonNullNonEmpty(Collection<?> collection, ExecutableFunction executableFunction){
138+
Collections.nonNullNonEmpty(collection, executableFunction);
139+
}
140+
141+
/**
142+
* Returns {@code true} if the provided Map is {@code null} or empty otherwise returns {@code
143+
* false}.
144+
*
145+
* @param map a Map to be checked against {@code null} or empty
146+
* @return {@code true} if the provided Map is {@code null} and empty otherwise * returns {@code
147+
* false}
148+
*/
149+
public static boolean nullOrEmpty(Map<?, ?> map) {
150+
return Collections.nullOrEmpty(map);
151+
}
152+
153+
/**
154+
* Execute code if the provided Map is {@code null} or empty
155+
* @param map a Map to be checked against {@code null} or empty
156+
* @param executableFunction lambda function given executed if the provided Map is {@code null} or empty
157+
*/
158+
public static void nullOrEmpty(Map<?, ?> map, ExecutableFunction executableFunction){
159+
Collections.nullOrEmpty(map, executableFunction);
160+
}
161+
162+
/**
163+
* Returns {@code true} if the provided Map is non-{@code null} and non-empty otherwise returns
164+
* {@code false}.
165+
*
166+
* @param map a Map to be checked against non-{@code null} and non-empty
167+
* @return {@code true} if the provided Map is non-{@code null} and non-empty otherwise {@code
168+
* false}
169+
*/
170+
public static boolean nonNullNonEmpty(Map<?, ?> map) {
171+
return Collections.nonNullNonEmpty(map);
172+
}
173+
174+
/**
175+
* Execute code if the provided Map is non-{@code null} and non-empty
176+
* @param map a Map to be checked against non-{@code null} and non-empty
177+
* @param executableFunction lambda function given executed if the provided Map is non-{@code null} and non-empty
178+
*/
179+
public static void nonNullNonEmpty(Map<?, ?> map, ExecutableFunction executableFunction){
180+
Collections.nonNullNonEmpty(map, executableFunction);
181+
}
182+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static boolean nonNullNonEmpty(Collection<?> collection) {
128128
* @param collection collection a Collection [List, Set] to be checked against non-{@code null} and non-empty
129129
* @param executableFunction lambda function given executed if the provided Collection [List, Set] is non-{@code null} and non-empty.
130130
*/
131-
public static void nonNullNonEmpty(Collection<?> collection, ExecutableFunction executableFunction){
131+
public static void nonNullNonEmpty(Collection<?> collection, ExecutableFunction executableFunction){
132132
if(nonNullNonEmpty(collection)){
133133
executableFunction.execute();
134134
}

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

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

28-
private int page;
29-
private int limit;
30-
private int total;
28+
private Long page;
29+
private Long limit;
30+
private Long total;
3131

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

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

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

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

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

93-
public int getTotal() {
93+
public Long getTotal() {
9494
return total;
9595
}
9696

97-
public CommonResponse<T> setTotal(int total) {
97+
public CommonResponse<T> withTotal(Long total){
9898
this.total = total;
9999
return this;
100100
}

src/main/java/com/javaquery/util/io/Files.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public final class Files {
2626
*/
2727
public static final String SYSTEM_TMP_DIR = System.getProperty("java.io.tmpdir");
2828

29+
/**
30+
* returns the current working directory location
31+
*/
32+
public static final String CURRENT_WORKING_DIR = System.getProperty("user.dir");
33+
2934
/**
3035
* Create new, empty file at specified path in {@link File} object. This method will also creates
3136
* folder structure if not exists.
@@ -50,6 +55,15 @@ public static <T extends File> boolean createNewFile(T file) {
5055
return false;
5156
}
5257

58+
/**
59+
* Delete file if exists.
60+
* @param file - file to delete
61+
* @return <code>true</code> if and only if the file or directory is successfully deleted; <code>false</code> otherwise
62+
*/
63+
public static <T extends File> boolean deleteIfExists(T file) {
64+
return file.exists() && file.delete();
65+
}
66+
5367
/**
5468
* Delete existing file and then Create new, empty file at specified path in {@link File} object.
5569
*

src/main/java/com/javaquery/util/io/JFile.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,12 @@ public void addAllAttribute(Map<String, String> attributes) {
9999
public String optAttribute(String key, String defaultValue) {
100100
return attributes.getOrDefault(key, defaultValue);
101101
}
102+
103+
/**
104+
* delete file if exists.
105+
* @return <code>true</code> if and only if the file or directory is successfully deleted; <code>false</code> otherwise
106+
*/
107+
public boolean deleteIfExists() {
108+
return exists() && delete();
109+
}
102110
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.javaquery.util.number;
2+
3+
import java.math.BigDecimal;
4+
import java.math.RoundingMode;
5+
6+
/**
7+
* @author javaquery
8+
* @since 1.2.5
9+
*/
10+
public class Numbers {
11+
12+
/**
13+
* Round the decimal number.
14+
* example: roundDecimal(10.123456789, 2) => 10.12
15+
* example: roundDecimal(10.123456789, 4) => 10.1235
16+
* example: roundDecimal(10.576, 2) => 10.58
17+
*
18+
* @param number number to round
19+
* @param decimalPlaces decimal places to round
20+
* @return rounded number
21+
*/
22+
public static Double roundDecimal(Double number, int decimalPlaces){
23+
return BigDecimal.valueOf(number)
24+
.setScale(decimalPlaces, RoundingMode.HALF_UP)
25+
.doubleValue();
26+
}
27+
}

src/main/java/com/javaquery/util/string/Strings.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public static void nullOrEmpty(String str, ExecutableFunction executableFunction
4242
}
4343
}
4444

45+
/**
46+
* Checks if the provided String is {@code null} or empty.
47+
* @param str a String to be checked against {@code null} or empty
48+
* @param defaultValue - if the provided String is {@code null} or empty then this value will be returned.
49+
* @return provided String if non-{@code null} and non-empty otherwise defaultValue
50+
*/
51+
public static String nullOrEmpty(String str, String defaultValue){
52+
return nullOrEmpty(str) ? defaultValue : str;
53+
}
54+
4555
/**
4656
* Returns {@code true} if the provided String is non-{@code null} and non-empty otherwise returns
4757
* {@code false}.
@@ -65,6 +75,16 @@ public static void nonNullNonEmpty(String str, ExecutableFunction executableFunc
6575
}
6676
}
6777

78+
/**
79+
* Checks if the provided String is non-{@code null} and non-empty.
80+
* @param str a String to be checked against non-{@code null} and non-empty
81+
* @param defaultValue - if the provided String is non-{@code null} and non-empty then this value will be returned.
82+
* @return provided String if non-{@code null} and non-empty otherwise defaultValue
83+
*/
84+
public static String nonNullNonEmpty(String str, String defaultValue){
85+
return nonNullNonEmpty(str) ? str : defaultValue;
86+
}
87+
6888
/**
6989
* Returns trimmed
7090
*

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

Lines changed: 4 additions & 4 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.setPage(1).setLimit(10).setTotal(100);
51+
commonResponse.withPage(1L).withLimit(10L).withTotal(100L);
5252
Assertions.assertEquals(HttpStatus.CREATED.value(), commonResponse.getStatusCode());
5353
Assertions.assertEquals(1L, commonResponse.getPayload());
54-
Assertions.assertEquals(1, commonResponse.getPage());
55-
Assertions.assertEquals(10, commonResponse.getLimit());
56-
Assertions.assertEquals(100, commonResponse.getTotal());
54+
Assertions.assertEquals(1L, commonResponse.getPage());
55+
Assertions.assertEquals(10L, commonResponse.getLimit());
56+
Assertions.assertEquals(100L, commonResponse.getTotal());
5757
}
5858
}

0 commit comments

Comments
 (0)