Skip to content

Commit a086b83

Browse files
authored
Merge pull request #6 from javaquery/v1.0.6
V1.0.6
2 parents 23602f3 + d4f47c7 commit a086b83

File tree

18 files changed

+523
-27
lines changed

18 files changed

+523
-27
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,20 @@ framework and objects.
3838
- <b>Regex</b>: Provides wide range of operation using regular expression like <code>isNumber(String value)</code>
3939
, <code>isAlphaNumeric(String value)</code>, <code>isValidEmail(String value)</code>.
4040
- <b>UniqueIdGenerator</b>: Generate unique time based random alphanumeric string like Firebase keys.
41+
- <b>LogBuilder</b>: Help you to build Map for Markers used in logging with optional execution time of code or function.
4142

4243
# Maven
4344

4445
```
4546
<dependency>
4647
<groupId>com.javaquery</groupId>
4748
<artifactId>util</artifactId>
48-
<version>1.0.5</version>
49+
<version>1.0.6</version>
4950
</dependency>
5051
```
5152

5253
# Gradle
5354

5455
```
55-
implementation 'com.javaquery:util:1.0.5'
56+
implementation 'com.javaquery:util:1.0.6'
5657
```

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group 'com.javaquery'
8-
version '1.0.5'
8+
version '1.0.6'
99

1010
repositories {
1111
mavenCentral()
@@ -15,6 +15,8 @@ dependencies {
1515
implementation('org.slf4j:slf4j-api:+')
1616
implementation('org.json:json:+')
1717

18+
testImplementation 'net.logstash.logback:logstash-logback-encoder:6.6'
19+
testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '+'
1820
testImplementation('org.junit.jupiter:junit-jupiter:5.7.0')
1921
}
2022

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

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

3+
import com.javaquery.util.collection.function.ExecutableFunction;
4+
35
/**
46
* @author vicky.thakor
57
* @since 1.0
@@ -18,6 +20,18 @@ public static boolean isNull(Object obj) {
1820
return obj == null;
1921
}
2022

23+
/**
24+
* Execute code if the provided reference is {@code null}.
25+
*
26+
* @param obj a reference to be checked against {@code null}
27+
* @param executableFunction lambda function given executed if the provided reference is {@code null}.
28+
*/
29+
public static void isNull(Object obj, ExecutableFunction executableFunction){
30+
if(isNull(obj)){
31+
executableFunction.execute();
32+
}
33+
}
34+
2135
/**
2236
* Returns {@code true} if the provided reference is non-{@code null} otherwise returns {@code
2337
* false}.
@@ -29,6 +43,18 @@ public static boolean nonNull(Object obj) {
2943
return obj != null;
3044
}
3145

46+
/**
47+
* Execute code if the provided reference is non-{@code null}.
48+
*
49+
* @param obj a reference to be checked against {@code null}
50+
* @param executableFunction lambda function given executed if the provided reference is non-{@code null}.
51+
*/
52+
public static void nonNull(Object obj, ExecutableFunction executableFunction){
53+
if(nonNull(obj)){
54+
executableFunction.execute();
55+
}
56+
}
57+
3258
/**
3359
* Returns {@code true} if the arguments are equal to each other and {@code false} otherwise.
3460
* Consequently, if both arguments are {@code null}, {@code true} is returned and if exactly one

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

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
import com.javaquery.util.Assert;
44
import com.javaquery.util.Objects;
5+
import com.javaquery.util.collection.function.ExecutableFunction;
56

67
import java.util.Collection;
78
import java.util.HashMap;
89
import java.util.List;
910
import java.util.Map;
11+
import java.util.Set;
1012
import java.util.stream.IntStream;
1113
import java.util.stream.Stream;
1214

15+
import static java.util.Collections.*;
16+
1317
/**
1418
* @author vicky.thakor
1519
* @since 1.0
@@ -18,6 +22,72 @@ public final class Collections {
1822

1923
private Collections() {}
2024

25+
/**
26+
* Provide access to {@link java.util.Collections} method singleton.<br/>
27+
*
28+
* Returns an immutable set containing only the specified object. The returned set is serializable.
29+
*
30+
* @param o the sole object to be stored in the returned set.
31+
* @param <T> the class of the objects in the set
32+
* @return an immutable set containing only the specified object.
33+
*/
34+
public static <T> Set<T> singleton(T o) {
35+
return java.util.Collections.singleton(o);
36+
}
37+
38+
/**
39+
* Provide access to {@link java.util.Collections} method singletonList.<br/>
40+
* Returns an immutable list containing only the specified object. The returned list is serializable.
41+
* @param o the class of the objects in the list
42+
* @param <T> the class of the objects in the list
43+
* @return an immutable list containing only the specified object.
44+
*/
45+
public static <T> List<T> singletonList(T o) {
46+
return java.util.Collections.singletonList(o);
47+
}
48+
49+
/**
50+
* Provide access to {@link java.util.Collections} method singletonMap.<br/>
51+
* Returns an immutable map, mapping only the specified key to the specified value. The returned map is serializable.
52+
*
53+
* @param key the sole key to be stored in the returned map.
54+
* @param value the value to which the returned map maps key.
55+
* @param <K> the class of the map keys
56+
* @param <V> the class of the map value
57+
* @return an immutable map containing only the specified key-value mapping.
58+
*/
59+
public static <K, V> Map<K, V> singletonMap(K key, V value) {
60+
return java.util.Collections.singletonMap(key, value);
61+
}
62+
63+
/**
64+
* Provide access to {@link java.util.Collections} method emptySet.<br/>
65+
* Returns an empty set (immutable). This set is serializable. Unlike the like-named field, this method is parameterized.
66+
* @param <T> the class of the objects in the set
67+
* @return the empty set
68+
*/
69+
public static <T> Set<T> emptySet() {
70+
return EMPTY_SET;
71+
}
72+
73+
/**
74+
* Provide access to {@link java.util.Collections} method emptyList.<br/>
75+
* @param <T> type of elements, if there were any, in the list
76+
* @return an empty immutable list
77+
*/
78+
public static <T> List<T> emptyList() {return EMPTY_LIST; }
79+
80+
/**
81+
* Provide access to {@link java.util.Collections} method emptyMap.<br/>
82+
* Returns an empty map (immutable). This map is serializable.
83+
* @param <K> the class of the map keys
84+
* @param <V> the class of the map values
85+
* @return an empty map
86+
*/
87+
public static <K, V> Map<K, V> emptyMap() {
88+
return EMPTY_MAP;
89+
}
90+
2191
/**
2292
* Returns {@code true} if the provided Collection [List, Set] is {@code null} or empty otherwise
2393
* returns {@code false}.
@@ -30,6 +100,17 @@ public static boolean nullOrEmpty(Collection<?> collection) {
30100
return Objects.isNull(collection) || collection.isEmpty();
31101
}
32102

103+
/**
104+
* Execute code if the provided Collection [List, Set] is {@code null} or empty.
105+
* @param collection a Collection [List, Set] to be checked against {@code null} or empty
106+
* @param executableFunction lambda function given executed if the provided Collection [List, Set] is {@code null} or empty.
107+
*/
108+
public static void nullOrEmpty(Collection<?> collection, ExecutableFunction executableFunction) {
109+
if(nullOrEmpty(collection)){
110+
executableFunction.execute();
111+
}
112+
}
113+
33114
/**
34115
* Returns {@code true} if the provided Collection [List, Set] is non-{@code null} and non-empty
35116
* otherwise returns {@code false}.
@@ -43,7 +124,18 @@ public static boolean nonNullNonEmpty(Collection<?> collection) {
43124
}
44125

45126
/**
46-
* Returns {@code true} if the provided Map is {@code null} and empty otherwise returns {@code
127+
* Execute code if the provided Collection [List, Set] is non-{@code null} and non-empty.
128+
* @param collection collection a Collection [List, Set] to be checked against non-{@code null} and non-empty
129+
* @param executableFunction lambda function given executed if the provided Collection [List, Set] is non-{@code null} and non-empty.
130+
*/
131+
public static void nonNullNonEmpty(Collection<?> collection, ExecutableFunction executableFunction){
132+
if(nonNullNonEmpty(collection)){
133+
executableFunction.execute();
134+
}
135+
}
136+
137+
/**
138+
* Returns {@code true} if the provided Map is {@code null} or empty otherwise returns {@code
47139
* false}.
48140
*
49141
* @param map a Map to be checked against {@code null} or empty
@@ -54,6 +146,17 @@ public static boolean nullOrEmpty(Map<?, ?> map) {
54146
return Objects.isNull(map) || map.isEmpty();
55147
}
56148

149+
/**
150+
* Execute code if the provided Map is {@code null} or empty
151+
* @param map a Map to be checked against {@code null} or empty
152+
* @param executableFunction lambda function given executed if the provided Map is {@code null} or empty
153+
*/
154+
public static void nullOrEmpty(Map<?, ?> map, ExecutableFunction executableFunction){
155+
if(nullOrEmpty(map)){
156+
executableFunction.execute();
157+
}
158+
}
159+
57160
/**
58161
* Returns {@code true} if the provided Map is non-{@code null} and non-empty otherwise returns
59162
* {@code false}.
@@ -66,6 +169,17 @@ public static boolean nonNullNonEmpty(Map<?, ?> map) {
66169
return Objects.nonNull(map) && !map.isEmpty();
67170
}
68171

172+
/**
173+
* Execute code if the provided Map is non-{@code null} and non-empty
174+
* @param map a Map to be checked against non-{@code null} and non-empty
175+
* @param executableFunction lambda function given executed if the provided Map is non-{@code null} and non-empty
176+
*/
177+
public static void nonNullNonEmpty(Map<?, ?> map, ExecutableFunction executableFunction){
178+
if(nonNullNonEmpty(map)){
179+
executableFunction.execute();
180+
}
181+
}
182+
69183
/**
70184
* Returns stream of batched List from original List by given batch size.
71185
*
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.javaquery.util.collection.function;
2+
3+
/**
4+
* @author vicky.thakor
5+
* @since 1.0.6
6+
*/
7+
@FunctionalInterface
8+
public interface ExecutableFunction {
9+
void execute();
10+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
public final class Files {
2222

2323
private static final Logger LOGGER = LoggerFactory.getLogger(Files.class);
24+
/**
25+
* returns the system temporary directory location
26+
*/
27+
public static final String SYSTEM_TMP_DIR = System.getProperty("java.io.tmpdir");
2428

2529
/**
2630
* Create new, empty file at specified path in {@link File} object. This method will also creates

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public String getExtension() {
4141
* access to either the old or new pathnames
4242
* @throws NullPointerException If parameter <code>dest</code> is <code>null</code>
4343
*/
44-
public final JFile rename(String name) {
44+
public JFile rename(String name) {
4545
Assert.nonNull(name, NullPointerException::new);
4646
if (exists()) {
4747
boolean result = renameTo(new File(getParent() + File.separatorChar + name));
@@ -53,12 +53,12 @@ public final JFile rename(String name) {
5353
}
5454

5555
/** @return String content of this file. */
56-
public final String read() {
56+
public String read() {
5757
return Files.readFromFile(this);
5858
}
5959

6060
/** @param data data to write to this file */
61-
public final void write(String data) {
61+
public void write(String data) {
6262
Files.writeToFile(this, data);
6363
}
6464

@@ -67,25 +67,25 @@ public final void write(String data) {
6767
* @param appendNewLine <code>true</code> to append new line at the end of data otherwise <code>
6868
* false</code>
6969
*/
70-
public final void append(String data, boolean appendNewLine) {
70+
public void append(String data, boolean appendNewLine) {
7171
Files.appendToFile(this, data, appendNewLine);
7272
}
7373

7474
/** @return attributes of file */
75-
public final Map<String, String> getAttributes() {
75+
public Map<String, String> getAttributes() {
7676
return attributes;
7777
}
7878

7979
/**
8080
* @param key a key of attribute
8181
* @param value a value of attribute
8282
*/
83-
public final void addAttribute(String key, String value) {
83+
public void addAttribute(String key, String value) {
8484
attributes.put(key, value);
8585
}
8686

8787
/** @param attributes add attributes map to file */
88-
public final void addAllAttribute(Map<String, String> attributes) {
88+
public void addAllAttribute(Map<String, String> attributes) {
8989
if (Collections.nonNullNonEmpty(attributes)) {
9090
this.attributes.putAll(attributes);
9191
}
@@ -96,7 +96,7 @@ public final void addAllAttribute(Map<String, String> attributes) {
9696
* @param defaultValue default value in case attribute not found
9797
* @return attribute value if found otherwise defaultValue
9898
*/
99-
public final String optAttribute(String key, String defaultValue) {
99+
public String optAttribute(String key, String defaultValue) {
100100
return attributes.getOrDefault(key, defaultValue);
101101
}
102102
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.javaquery.util.logging;
2+
3+
/**
4+
* @author vicky.thakor
5+
* @since 1.0.6
6+
*
7+
* Implement this interface for {@link LogBuilder} actions.
8+
*/
9+
public interface Action {
10+
}

0 commit comments

Comments
 (0)